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 (45) - to_ping (18) - wordpress query (13) - PING www.xxL.com (11) - wordpress queries (9) - wordpress reduce queries (8) - wordpress query comments (8) - optimize wordpress queries (6) - WORDPRESS query post (6) - wordpress query optimization (5) - reduce wordpress query (5) - PING WWW XXL COM (5) - wordpress search query (5) - reduce queries wordpress (5) - to_ping wordpress (5) - reducing wordpress queries (5) - wordpress (5) - ping.www.xxl (4) - reduce wordpress queries (4) - wordpress mysql optimization (4) - query optimization (4) - wordpress query log (4) - wordpress query database (3) - query (3) - reduce query wordpress (3) - to_ping in wordpress (3) - mysql ping query (3) - query wordpress (3) - how to reduce wordpress queries (3) - WORDPRESs queyr optimizer (2) - how to execute query in wordpress (2) - wordpress long queries (2) - wordpress mysql query optimization (2) - queries seconds wordpress (2) - category query wordpress (2) - wordpress query posts (2) - Apache optimization for wordpress (2) - query category wordpress (2) - to_ping, pinged,wordpress (2) - $wpdb->query : wordpress (2) - wordpress to_ping table (2) - wordpress full table scan (2) - wordpress mysql optimal index (2) - wordpress ping query (2) - wordpress latest posts optimize query (2) - optimize wordpress query (2) - optimize mysql queries wordpress (2) - how to find wordpress query long (2) - query post wordpress (2) - query wordpress table (2) - wordpress reduce mysql query (2) - optimize queries wordpress (2) - wordpress apache optimization (2) - mysql query optimization (2) - wordpress to_ping query optimization (2) - optimize wordpress (2) - wordpress url optimization (2) - wordpress query $post->ID (2) - wordpress optimization (2) - query optimization wordpress (2) - wordpress query per second (1) - wordpress union queries (1) - wordpress query by id (1) - reducing wordpress database queries (1) - get category name in query post (1) - wordpress,optimize query (1) - (query1 union query2) union (query 3 union query 4) not execute in MYSQL (1) - what is the wpdb query to retrieve latest post from wordpress database (1) - optimize wordpress mysql (1) - execute query wordpress (1) - mysql optimization ping (1) - lower queries on wordpress (1) - post pinged wordpresss table (1) - post to_ping (1) - wordpress no execute query in homepage (1) - wordpress mysql query category (1) - database query wordpress (1) - wordpress mysql first query long (1) - query to fetch category in wordpress (1) - wordpress mysql query to determine most popular posts (1) - category query in wordpress (1) - Query Post from Different Database Table wordpress (1) - wordpress optimization reduce query number (1) - wordpress query optimizer (1) - wordpress mysql query optimisation (1) - wordpress mysql queries optimization (1) - wordpress ping optimation (1) - wordpress query most commented posts (1) - category and post query in wordpress (1) - wordpress database querry tags for post (1) - wordpress select query with WHERE (1) - executar query no wordpress (1) - worepress to_ping (1) - query posts with images wordpress (1) - wordpress query select category by id (1) - wordpress query database where category (1) - wordpress search posts query (1) - ping queries wordpress (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 how to reduce queries (1) - wordpress category query (1) - select post query in wordpress (1) - optimize ping wordpress (1) - wordpress query m= (1) - get category id and name query in wordpress (1) - category name and id query in wordpress (1) - execute query in wordpress (1) - WordPress code optimization reduce queries (1) - QUERY IS OPTIMIZED BUT STILL IT TAKES 15 MINS (1) - wordpress pretty search query (1) - reduce wordpress query number (1) - mysql query to get category ids from wordpress (1) - url query mysql php wordpress select from where (1) - mysql optimization wordpress (1) - query posts only two categories by id (1) - optimizing wordpress query (1) - database queries wordpress (1) - mysql wordpress query (1) - wordpress query number reduce (1) - wordpress db to_ping (1) - wordpress queries per click (1) - reduce the Queries per second avg in mysql (1) - wordpress post_status= (1) - wordpress pingbacks to_ping (1) - big bear sex new xxl 6 index (1) - wordpress get to_ping from database (1) - get wordpress database name query (1) - wordpress queries. seconds. (1) - wordpress tables query (1) - wordpress lower queries (1) - how can i get a selected post in wordpress by id (1) - wordpress ping_status (1) - select query from two category (1) - wordpress url query log (1) - apache optimization wordpress (1) - select query in wordpress (1) -