Remove unnecessary queries to count number of results in pager_query

lyricnz - April 12, 2007 - 09:56
Project:Drupal
Version:6.x-dev
Component:database system
Category:task
Priority:normal
Assigned:Unassigned
Status:won't fix
Description

When using pager_query, we don't need a seperate query to ask the database for the number of rows, if we run the main query itself first and it returns less than $limit. This will happen on the last page of a paginated query (which probably happens a bit more often than you expect)

Example (user search):

20.31 1 pager_query SELECT COUNT(*) FROM users WHERE LOWER(name) LIKE LOWER('%ab%')
0.67 1 pager_query SELECT name, uid FROM users WHERE LOWER(name) LIKE LOWER('%ab%') LIMIT 0, 15

See attached patch - applies to both 5.1 and HEAD

AttachmentSizeStatusTest resultOperations
page-optimization.patch1.6 KBIgnoredNoneNone

#1

lyricnz - April 12, 2007 - 10:00

Update - remove debugging code - sorry.

AttachmentSizeStatusTest resultOperations
page-optimization_0.patch1.57 KBIgnoredNoneNone

#2

lyricnz - April 12, 2007 - 10:05

Update - coding style

AttachmentSizeStatusTest resultOperations
page-optimization_1.patch1.58 KBIgnoredNoneNone

#3

lyricnz - April 12, 2007 - 10:42
Status:active» needs review

#4

lyricnz - May 4, 2007 - 12:11
Version:5.1» 6.x-dev
Category:bug report» feature request

Assigning to 6.x-dev

#5

David Strauss - May 27, 2007 - 06:43

I haven't tested it, but the code looks good to me. I'm not sure I buy the benchmark of the current code, but this change could only make things faster.

#6

lyricnz - May 28, 2007 - 12:17

Yeah, the query cache is likely to make the second query pretty fast, no matter which order they're in - but the less queries the better.

#7

catch - October 24, 2007 - 13:59
Title:(Sometimes) Unnecessary query to count number of results in pager_query» Remove unnecessary queries to count number of results in pager_query
Category:feature request» task

This still applies but I'm not sure how to test it. Seems like it could be a decent performance boost though.

#8

catch - October 24, 2007 - 14:39
Status:needs review» needs work

fatal error on admin/content/node

#9

lyricnz - October 25, 2007 - 11:32
Status:needs work» won't fix

Now that db_num_rows() has been removed, I don't think this optimization is possible - we don't fetch each of the rows in the query here, so we no longer know how many rows are returned.

 
 

Drupal is a registered trademark of Dries Buytaert.