WordPress + Invalid URLs = Extra Database Queries

After reporting weirdness last week I finally sat down with a completely clean and virgin install of WordPress 2.3.2 and traced what happens when you make a permalink request for a non-existent URL.

Here are two sets of URLs to use as examples and context:

  • These are valid URLs:
    • http://site.org/archives/101
    • http://site.org/page-name
  • These are _not_ valid URLs:
    • http://site.org/archivezorz/101
    • http://site.org/favicon.ico

Valid URLs get parsed, the expected MySQL queries get executed, and the results are processed and returned to the browser. Normal. The problem is that invalid URLs that get sent through WordPress still result in a query like the following being executed on the database. What?

SELECT SQL_CALC_FOUND_ROWS  test_posts.* FROM test_posts  WHERE 1=1  AND post_type = 'post' AND (post_status = 'pub
lish' OR post_status = 'private')  ORDER BY post_date DESC LIMIT 0, 10

That is, even after a URL is sent through WP->parse_request() and found to be invalid/404, WordPress marches on to WP->query_posts() and hits the database with a generic request for the X most recent posts. And because this is executed for every 404, it actually results in a lot of database activity.

In most cases MySQL has cached the result, and so it poses a minimal load on the server. And even if the cache is stale, for most sites it’s not a particularly resource intensive query.

But, if you’ve got 350,000 rows in the posts table, it’s incredibly resource intensive to order all those posts on the post_date (datetime) column. I’ve seen hundreds of them pile up and take forever to complete after writes to the table. It’s sufferable if write activity on the posts table is very low, but that’s not something I want to hope for.

So my question to the wp-hackers community is: Do we actually want to execute that query for every 404 under normal circumstances? If not, is the following (or something like it) the stupidest solution?

In wp-includes/classes.php:

	function query_posts() {
		global $wp_the_query;
		$this->build_query_string();
 
		// return if the request URI is a 404
		if( $this->did_permalink && $this->query_vars['error'] == '404' )
			return;
 
		$wp_the_query->query($this->query_vars);
	}

The above works, but there’s probably a better way to write it.

1 Comment(s)

  1. Comment by Sandra on May 19, 2008 5:46 am

    Hi,

    I found your post interesting and have implemented your code, but it does not seem to work on my version of wordpress.

    The problem I am trying to solve is showing blank screen when invalid URL is entered.
    I am using permalinks and here is what i would like:

    I have a site: http://www.mysite.com/myblog/

    If I type in address bar the following:

    http://www.mysite.com/myblog/somecrap

    This returns blank page.

    I have found out that the SQL query is built and posts table read with “somecrap” as post name.

    The number of affected rows returned is zero, but in classes.php the error is cleared (there is a comment in the code saying that as there is ’something’ in the url, they should clear the error) and hence the above code never executes.

    What I would expect to happen is that when no posts is returned as a result of this query, I am shown “post not found” or similar message.

    I do not really know php and even less on how in wordpress all hangs together, and am a bit concerned if I put some changes (such as condition on number of rows returned == 0 instead of checking for error, then that I will damage some other bits of wordpress such as posting, searching categories etc.

    Do you have any idea what I could do?

    Many thanks,

    Sandra

Comments RSS TrackBack Identifier URI

Leave a comment

 

User contributed tags for this post:

WWWPRIVATE.COM (211) - wwwprivate (39) - www.sexviwe.com (34) - wwwprivate com (24) - sexviwe tv (21) - wwwprivatecom (16) - WWWprivate,com (14) - wordpress (11) - WWW.SIX VIWE.COM (10) - wordpress query database (8) - wwwpribate.com (6) - wwwprivete.com (6) - sixviwe (5) - sex viwe.com (5) - parse_request wordpress (5) - wwwprivate.com. (4) - wwwPRIVETE (4) - wordpress invalid url (4) - wwwprivate .com (4) - 404 wordpress (4) - wordpress query_vars (4) - wordpress too many queries (4) - sexviwe.com (4) - sexviwe.tv (4) - invalid urls (3) - www.sexviwetv.com (3) - www.sexviwe.tv (3) - wordpress parse_request (3) - query_posts blank page (3) - www.sex viwe com (3) - wwwprivati.com (3) - wordpress reduce queries (3) - wordpress queries (3) - arabic wordpress username invalid (3) - wordpress SQL_CALC_FOUND_ROWS (3) - wwwprivates,com (2) - WordPress Invalid URLs = Extra Database Queries (2) - wordpress query url (2) - sexviwetv (2) - favicon 404 error wordpress (2) - www.sexviwetv.com.tr (2) - remove invalid url in comments wordpress (2) - wordpress media library mysql table (2) - wordpress reducing 404 errors (2) - http://www.sex viwe tv.com/ (2) - wordpress query_posts blank page (2) - WWWPRIVATE.,COM (2) - SQL_CALC_FOUND_ROWS wordpress (2) - google.fr @wwwprivate (2) - WordPress database error Query was empty for query made by trackback (2) - TV.com virgin (2) - why is wordpress executing so many queries (2) - wordpress media library database (2) - wordpress media library database table (2) - WWWPRIBATE (2) - wordpress database queries (2) - sexviwe tv 2009 (2) - wordpress media database table (2) - database for wordpress (2) - how to reduce wordpress queries (2) - wordpress database query (2) - www.sexviwetv.net (2) - reduce wordpress queries (2) - wordpress reduce mysql query (2) - 404 wordpress problem database (2) - six viwe tv (2) - wwwpirivate.com (1) - URL is invalid wordpress permalink (1) - sex viwe.tv (1) - ? www.sexviwe.info (1) - how to optimize wordpress database query (1) - wordpress query posts manually from database (1) - wordpress url invalid (1) - http//wwwprivate.com (1) - wwwpravite.com (1) - www.sex viwe.net (1) - wwwprivate:com (1) - Sociable Wordpress invalid link html (1) - related:www.purepineapple.info/visitors/search_200808_152.html (1) - What are extra queries on database (1) - wordpress permalink database table (1) - wordpress feed return 404 error (1) - sex tv urls (1) - www.sexviwe.tv.com (1) - wordpress media 404 (1) - www.sexviwe tv.com (1) - sexviwe video (1) - wordpress query_posts private pages (1) - wwwpirivate com (1) - wordpress sql media url (1) - query_posts orderby most commented (1) - query_posts optimize (1) - Invalid URL wordpress.com (1) - wordpress update media library urls (1) - wordpress pagename url not found (1) - wordpress invalid username problem (1) - www.sexviwe exstra (1) - wordpress i 404 error when taken account activity (1) - Error: Query was empty mysql arabic (1) - favicon creating wordpress 404 post errors (1) - wordpress no matching results permalinks (1) - wordpress many post database (1) - wordpress no posts found (1) - wordpress query most commented post (1) - URLError-1 (1) - cache:http://www.sex.tv1/ (1) - wordpress reduce db queries (1) - wordpress invalid username (1) - www.sex extra tv.com (1) - invalid URL RSS (1) -