WordPress to_ping Query Optimization

The WordPress team has taken up the issue of performance optimization pretty seriously, and I look forward to the fruits of their efforts, but I’m also casting a critical eye on my own code. Thanks to caching and a hugely optimized query architecture, Scriblio is now performing better than ever, and I’m now looking at the next tier of problems.

First among them is a WordPress query that runs to find which posts have pingbacks or trackbacks waiting to be processed. As it was originally written, it looked like this:

$trackbacks = $wpdb->get_results("
	 SELECT ID 
	 FROM $wpdb->posts 
	 WHERE CHAR_LENGTH(TRIM(to_ping)) > 7 
	 AND post_status = 'publish'
	");

The problem is that the query requires a full table scan, and it can’t be optimized simply by creating an index on to_ping because CHAR_LENGTH(TRIM(to_ping)) > 7 requires inspecting every row and applying the functions before the result can be tested. As written, the query could take as long as 15 minutes to execute on a busy table of 350,000 rows.

Once I read through the code and determined that it’s very unlikely that cruft would be left in to_ping, or that any that was there would get removed once processed through the ping functions, I decided to try the query below. (Hat tip also to Andy Skelton for sanity checking my reading of the code.)

$trackbacks = $wpdb->get_results("
	 SELECT ID 
	 FROM $wpdb->posts 
	 WHERE to_ping <> '' 
	 AND post_status = 'publish'
	");

A trac ticket has been created and patch added, but despite the optimized query, a table as large as I’m working with probably needs an index to keep things running smoothly. So here’s what I’ve got:

KEY ping_status ( to_ping(1), post_status )

I’ve not yet confirmed that this is the most optimal index, but queries now take a fraction of a second to complete.

No Comments Yet

No comments yet.

Comments RSS TrackBack Identifier URI

Leave a comment

 

User contributed tags for this post:

wordpress to_ping (36) - to_ping (14) - wordpress query (13) - wordpress queries (9) - PING www.xxL.com (9) - wordpress query comments (8) - wordpress reduce queries (6) - reducing wordpress queries (5) - PING WWW XXL COM (5) - reduce wordpress query (5) - optimize wordpress queries (5) - wordpress search query (5) - WORDPRESS query post (5) - wordpress query optimization (5) - wordpress (5) - ping.www.xxl (4) - query optimization (4) - reduce wordpress queries (4) - wordpress mysql optimization (4) - reduce queries wordpress (4) - query wordpress (3) - to_ping in wordpress (3) - mysql ping query (3) - to_ping wordpress (3) - how to reduce wordpress queries (3) - query (3) - to_ping, pinged,wordpress (2) - queries seconds wordpress (2) - WORDPRESs queyr optimizer (2) - wordpress ping query (2) - wordpress query $post->ID (2) - wordpress url optimization (2) - wordpress query posts (2) - wordpress to_ping table (2) - wordpress reduce mysql query (2) - query optimization wordpress (2) - wordpress mysql optimal index (2) - $wpdb->query : wordpress (2) - wordpress mysql query optimization (2) - wordpress full table scan (2) - query post wordpress (2) - how to find wordpress query long (2) - wordpress to_ping query optimization (2) - query category wordpress (2) - wordpress query log (2) - optimize mysql queries wordpress (2) - mysql query optimization (2) - wordpress optimization (2) - category query wordpress (2) - wordpress latest posts optimize query (2) - query wordpress table (2) - wordpress apache optimization (2) - reduce query wordpress (2) - wordpress query database (2) - optimize wordpress (2) - wordpress select query with WHERE (1) - url query mysql php wordpress select from where (1) - category name and id query in wordpress (1) - mysql query to get category ids from wordpress (1) - executar query no wordpress (1) - execute query in wordpress (1) - select post query in wordpress (1) - wordpress pretty search query (1) - WordPress code optimization reduce queries (1) - wordpress how to reduce queries (1) - optimizing wordpress query (1) - wordpress category query (1) - reduce wordpress query number (1) - QUERY IS OPTIMIZED BUT STILL IT TAKES 15 MINS (1) - worepress to_ping (1) - query posts with images wordpress (1) - reducir ping wordpress (1) - two queries wordpress (1) - wordpress tables indexes post_status (1) - wordpress db query get comment where category is (1) - wordpress query for most popular post (1) - Apache optimization for wordpress (1) - query per pingare il db (1) - optimze wordpress query (1) - mysql query Wordpress comments (1) - wordpress optimization queries (1) - $wpdb->query query tag in wordpress (1) - optimized wordpress query.php (1) - query post category wordpress (1) - wordpress query tag (1) - wordpress to_ping,pinged (1) - wordpress counter mysql queries (1) - wordpress query select category by id (1) - wordpress query database where category (1) - wordpress search posts query (1) - reduce mysql queries wordpress (1) - wordpress database query optimization (1) - wordpress QUERY FIND POST TAGS (1) - what are queries wordpress (1) - how to lower wordpress database queries (1) - wordpress queries optimizer (1) - wordpress queries seconds (1) - wordpress ping selected category (1) - reducir queries en wordpress (1) - wordpress query post_status (1) - optimize wordpress indexes (1) - query posts only two categories by id (1) - wordpress: query problems (1) - wordpress query by seconds (1) - REDUCING mysql queries in wordpress (1) - wordpress query posts get post ID (1) - wordpress search wpdb query (1) - query post category (1) - how to reduce mysql queries per second (1) - number of wordpress queries (1) - wordpress WHERE to_ping <> (1) - wordpress quries optimize (1) - db ping query (1) - mysql wordpress query (1) - wordpress query most commented posts (1) - how to write select query in wordpress (1) - wordpress optimization -seo (1) - wordpress reduce wordpress queries (1) - wordpress select post query by category (1) - wordpress select post query (1) - mysql wordpress select id from category -"server has gone away (1) - wordPress big database query problem (1) - mysql query wordpress wpdb (1) - big bear sex new xxl4 index (1) - wordpress optimization ping (1) - wordpress query tagged photos (1) - wordpress to_ping pinged (1) - fix number of wordpress queries (1) - wordpress post_status (1) - optimize mysql 350000 rows (1) - wordpress long queries (1) - post to_ping (1) - wordpress no execute query in homepage (1) - wordpress,optimize query (1) - (query1 union query2) union (query 3 union query 4) not execute in MYSQL (1) - execute query wordpress (1) - mysql optimization ping (1) - wordpress query per second (1) - what is the wpdb query to retrieve latest post from wordpress database (1) - get category name in query post (1) - wordpress query by id (1) -