Performance: SELECT MAX(comment_count) FROM node_comment_statistics does full table scan

Wesley Tanaka - November 19, 2008 - 18:38
Project:Drupal
Version:7.x-dev
Component:search.module
Category:bug report
Priority:normal
Assigned:Unassigned
Status:needs work
Issue tags:Performance
Description

An issue when running Drupal with millions of nodes:

This query in node_update_index() does a full table scan

mysql> EXPLAIN SELECT MAX(comment_count) FROM d_node_comment_statistics \G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: d_node_comment_statistics
type: ALL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: 2435440
Extra:

#1

Wesley Tanaka - November 19, 2008 - 18:42

The addition of this index will optimize the query away:

ALTER TABLE node_comment_statistics ADD INDEX (comment_count);

#2

brianV - July 29, 2009 - 04:35
Version:6.6» 7.x-dev
Status:active» needs review

Patch attached adds a hook_update_70XX() implementation which adds this index.

AttachmentSizeStatusTest resultOperations
336483-add-index-for-comment-count.patch909 bytesIdlePassed: 11905 passes, 0 fails, 0 exceptionsView details | Re-test

#3

FiReaNG3L - July 29, 2009 - 17:52

Can we see the explain after the index addition Welsey?

#4

brianV - July 29, 2009 - 18:02

I'm not Wesley, but...

explain MAX(comment_count)
FROM d_node_comment_statistics \G
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: NULL
         type: NULL
possible_keys: NULL
          key: NULL
      key_len: NULL
          ref: NULL
         rows: NULL
        Extra: Select tables optimized away

#5

brianV - July 29, 2009 - 18:05

Just tagging

#6

Damien Tournoud - July 29, 2009 - 18:19
Status:needs review» reviewed & tested by the community

#7

FiReaNG3L - July 29, 2009 - 18:45

brianV, was that EXPLAIN on a table with a significant amount of nodes / comments? To me it looks like on a brand new installation, which doesnt mean much?

#8

brianV - July 29, 2009 - 19:22

Fireangel,

That was taken from Wesley Tanaka's page on the subject. I am providing his data, which he says is based upon a node database of 2.4million nodes.

See http://wtanaka.com/drupal/million-nodes-6 - there is an entry for this bug.

#9

FiReaNG3L - July 29, 2009 - 20:08

Excellent! No further objections / comments here, and it's already RTBC

#10

Dries - July 30, 2009 - 08:37

Well, but shouldn't we update the schema definition in modules/comment/comment.install too? The index is not being created for new installs?

#11

Damien Tournoud - July 30, 2009 - 08:44
Status:reviewed & tested by the community» needs work

Oups.

#12

brianV - July 30, 2009 - 13:44
Status:needs work» needs review

Big time oops. Rerolled with the proper addition to comment_schema().

AttachmentSizeStatusTest resultOperations
336483-add-index-for-comment-count-rev2.patch1.68 KBIdlePassed: 11876 passes, 0 fails, 0 exceptionsView details | Re-test

#13

Damien Tournoud - July 30, 2009 - 13:48
Status:needs review» needs work

Thanks Brian, but please remove the useless "&".

#14

brianV - July 30, 2009 - 13:51
Status:needs work» needs review

Wow - I am off my game this morning...

AttachmentSizeStatusTest resultOperations
336483-add-index-for-comment-count-rev3.patch1.68 KBIdlePassed: 11905 passes, 0 fails, 0 exceptionsView details | Re-test

#15

moshe weitzman - July 30, 2009 - 18:56
Status:needs review» reviewed & tested by the community

#16

Dries - July 30, 2009 - 19:27
Status:reviewed & tested by the community» fixed

Committed to CVS HEAD. Thanks!

#17

Damien Tournoud - July 30, 2009 - 22:48
Status:fixed» patch (to be ported)

Let's backport this.

#18

Damien Tournoud - July 30, 2009 - 23:03
Status:patch (to be ported)» needs work

Why is the update function in system.install?

#19

brianV - July 31, 2009 - 00:00

Looks like I am more off my game than I thought today.

There is absolutely no good reason why I put this in system.install instead of comment.install.

What's the best way to correct this? Move the hook_update_N to the correct file in another patch?

 
 

Drupal is a registered trademark of Dries Buytaert.