Note that there are some explanatory texts on larger screens.

plurals
  1. POSerious MySQL Performance Issue (Joins, Temporary Table, Filesort....)
    primarykey
    data
    text
    <p>I've got a users table and a votes table. The votes table stores votes toward other users. And for better or worse, a single row in the votes table, stores the votes in both directions between the two users.</p> <p>Now, the problem is when I wanna list for example all people someone has voted on.</p> <p>I'm no MySQL expert, but from what I've figured out, thanks to the OR condition in the join statement, it needs to look through the <em>whole</em> users table (currently +44,000 rows), and it creates a temporary table to do so.</p> <p>Currently, the bellow query takes about two minutes, yes, <strong>two minutes</strong> to complete. If I remove the OR condition, and everything after it in the join statement, it runs in less than half a second, as it only needs to look through about 17 of the 44,000 user rows (<em>explain</em> ftw!).</p> <p>The bellow example, the user ID is <em>9834</em>, and I'm trying to fetch his/her own <em>no</em> votes, and join the info from user who was voted on to the result.</p> <p>Is there a better, and faster way to do this query? Or should I restructure the tables? I seriously hope it can be fixed by modifying the query, cause there's already a lot of users (+44,000), and votes (+130,000) in the tables, which I'd have to migrate.</p> <p>thanks :)</p> <pre><code>SELECT *, votes.id as vote_id FROM `votes` LEFT JOIN users ON ( ( votes.user_id_1 = 9834 AND users.uid = votes.user_id_2 ) OR ( votes.user_id_2 = 9834 AND users.uid = votes.user_id_1 ) ) WHERE ( ( votes.user_id_1 = 9834 AND votes.vote_1 = 0 ) OR ( votes.user_id_2 = 9834 AND votes.vote_2 = 0 ) ) ORDER BY votes.updated_at DESC LIMIT 0, 10 </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload