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

Comments

lyricnz’s picture

StatusFileSize
new1.57 KB

Update - remove debugging code - sorry.

lyricnz’s picture

StatusFileSize
new1.58 KB

Update - coding style

lyricnz’s picture

Status: Active » Needs review
lyricnz’s picture

Version: 5.1 » 6.x-dev
Category: bug » feature

Assigning to 6.x-dev

david strauss’s picture

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.

lyricnz’s picture

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.

catch’s picture

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 » task

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

catch’s picture

Status: Needs review » Needs work

fatal error on admin/content/node

lyricnz’s picture

Status: Needs work » Closed (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.