EXPLAIN
SELECT n.nid, n.title, u.uid, u.name
FROM node n
INNER JOIN node_counter s ON n.nid = s.nid
INNER JOIN users u ON n.uid = u.uid
WHERE s.totalcount !=0
AND n.status =1
ORDER BY s.totalcount DESC
LIMIT 0 , 5
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE s ALL PRIMARY NULL NULL NULL 645288 Using where; Using temporary; Using filesort
1 SIMPLE u ALL PRIMARY NULL NULL NULL 3 Using join buffer
1 SIMPLE n eq_ref PRIMARY,node_status_type,uid PRIMARY 4 s.nid 1 Using where
table s & table u do not get a key thus it takes a very long time to do this operation with a lot of nodes. I was seeing this take over 300 seconds to complete. It can be cut down by creating an index on daycount; totalcount; in node_counter table. Also creating an index of uid and name in users helps as well.
Any other ideas on how to optimize this SQL?
| Comment | File | Size | Author |
|---|---|---|---|
| #1 | explain-out.png | 10.07 KB | mikeytown2 |
Comments
Comment #1
mikeytown2 commentedAnother Query that needs an index, at least on my server. taxonomy_get_tree()
Add index to term_hierarchy tid tid & problem fixed.
One more where I can't figure out what to index; would a rewrite of this be the best?
node_admin_nodes()
Copying to tmp table on disk is what's eating up all the time for the query.
Looks like this last one is getting rewritten only by node_db_rewrite_sql.
Screenshot of the above explain. statistics is the name of the index form the original post. Either way this takes forever to do.
Comment #2
Greg Go commentedThanks mikeytown2! Views that sort nodes by popularity are a lot faster after adding indexes to daycount and totalcount.
For other admins who stumble on to this, here's the sql:
Comment #3
mikeytown2 commented@Gregory Go
I have a module that does this now
http://drupal.org/project/dbtuner
Comment #4
jcisio commentedIt is only useful if we bulk update node_counter.
If not, increase the index size by 4x for a table of which index needs to be rebuilt every second for a high traffic website is not really a good idea ;-) 88% (either total time or total count) of my slow queries are "UPDATE node_counter" with those extra index. I'm removing them and observe the consequence.
Comment #5
mikeytown2 commentedHaving the node stats block take 30 seconds to render due to slow SQL queries isn't good either (500k+ nodes). With modules like http://drupal.org/project/jstats having writes take a little bit longer for faster reads is a good move.
Comment #6
jcisio commentedYes, let's make jstats bulk update node_counter, then we can safely add more index to it without any worry.
Comment #7
timmillwoodindexes don't do much good on node_counter as the data is updated so often.
What it needs is a rewrite, queue, or cache.
#1446932: Improve statistics performance by adding a swappable backend