Opened 22 months ago

Closed 8 months ago

#18120 closed defect (bug) (fixed)

Slow SQL in get_adjacent_post()

Reported by: javert03 Owned by: ryan
Priority: normal Milestone: 3.5
Component: Query Version: 3.2.1
Severity: normal Keywords: has-patch commit
Cc:

Description

First, thanks for making WordPress what it is. I've gotten a lot of milage out of it.

I help run a blog that gets a few thousand hits per day. I've done some optimizing to cut page load times, but keep noticing one particular query in the slow_queries log. Something like:

SELECT p.* FROM wp_posts AS p  WHERE p.post_date < '2010-01-25 18:04:53' AND p.post_type = 'post' AND p.post_status = 'publish'  ORDER BY p.post_date DESC LIMIT 1

The query is made on line 1146 of wp-includes/link-template.php, in the function get_adjacent_post(). The query rarely takes more than 2 seconds, so it's not an end-of-the-world type of slow, but since it seems to be used constantly I'd like to make it faster. On our blog, it usually examines 3-4k rows.

To speed things up, I added an index on the column post_date, but an EXPLAIN shows that the index is not being used. For whatever reason, use of the comparison operators causes MySQL to not use an index. Replacing comparison operators with BETWEEN...AND allows the key to be used.

...  WHERE p.post_date BETWEEN '2008-03-09 09:00:00' AND '9999-12-31 00:00:00' ...

However, WordPress also appears to only be using ID, post_title, post_status, post_date from the adjacent posts. So I made the changes in the attached link_template.php. I changed 3 lines and marked each line with my username, javert03.

Thanks!

Attachments (4)

link-template.php (72.5 KB) - added by javert03 22 months ago.
includes/link_template.php
link-template.diff (1.4 KB) - added by javert03 22 months ago.
SVN diff
get_adjacent_post.diff (1.1 KB) - added by apokalyptik 13 months ago.
Select p.id and then get_post(). Avoids temp table *and* filesort which occurs on large posts tables where cats are (at least) excluded)
18120.diff (1.4 KB) - added by nacin 8 months ago.

Download all attachments as: .zip

Change History (13)

includes/link_template.php

Thanks for taking care. Please provide a diff/patch file with the changes you've made, see http://codex.wordpress.org/Reporting_Bugs#Patching_Bugs

SVN diff

Select p.id and then get_post(). Avoids temp table *and* filesort which occurs on large posts tables where cats are (at least) excluded)

I am having this exact same problem, I have over 20 million custom posts in wp_posts table and this query takes over 30 seconds to execute.

  • Milestone changed from Awaiting Review to 3.5

get_adjacent_post.diff looks good

  • Keywords commit added

Agreed.

Looks like we should not be calling get_post() on '' (if nothing was found).

nacin8 months ago

18120.diff feels better. Also only now caches a post ID rather than an object.

New query.

SQL query: EXPLAIN SELECT p.id FROM wp_trunk_posts AS p WHERE p.post_date < '2012-09-19 18:01:23' AND p.post_type = 'post' AND p.post_status = 'publish' ORDER BY p.post_date DESC LIMIT 1; 
Rows: 1

id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	p	range	type_status_date	type_status_date	132	NULL	1	Using where; Using index

Old query.

SQL query: EXPLAIN SELECT p.* FROM wp_trunk_posts AS p WHERE p.post_date < '2012-09-19 18:05:11' AND p.post_type = 'post' AND p.post_status = 'publish' ORDER BY p.post_date DESC LIMIT 1; 
Rows: 1

id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	p	range	type_status_date	type_status_date	132	NULL	1	Using where
Version 0, edited 8 months ago by ryan (next)

Passes Tests_URL::test_get_adjacent_post()

  • Owner set to ryan
  • Resolution set to fixed
  • Status changed from new to closed

In [21919]:

Improve performance of query in get_adjacent_post(). Get the ID and then call get_post(). Props apokalyptik, nacin, javert03. fixes #18120

Note: See TracTickets for help on using tickets.