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.

One Comment to “WordPress + Invalid URLs = Extra Database Queries”

  1. 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

User contributed tags for this post:

WWWPRIVATE.COM (306) - wwwprivate (51) - wwwprivate com (45) - wwwprivatecom (29) - WWWprivate,com (19) - wordpress (11) - wwwprivate.com. (10) - wordpress invalid url (9) - wordpress parse_request (9) - parse_request wordpress (8) - wordpress query database (8) - wwwprivat.com (7) - wwwpribate.com (7) - wordpress reduce queries (5) - wwwprivate .com (5) - 404 wordpress (4) - wordpress query_vars (4) - wordpress queries (3) - invalid urls (3) - wordpress query url (3) - reduce wordpress queries (3) - wordpress invalid urls extra database queries (3) - wordpress media library database table (3) - reduce query wordpress (2) - database for wordpress (2) - WWWPRIBATE (2) - wordpress reduce mysql query (2) - wordpress media library database (2) - wordpress reduce 404 queries (2) - TV.com virgin (2) - wwwprivati.com (2) - how to reduce wordpress queries (2) - wordpress invalid url show page (2) - wordpress database query (2) - invalid url on wordpress (2) - query widget invalid url error (2) - wordpress parse_request 404 (2) - favicon 404 error wordpress (2) - wordpress query permalink (1) - wordpress query on permalink (1) - wordpress reducing queries (1) - wordpress Blog permalinks return 404 but still return page (1) - empty archives wordpress post not found (1) - wordpress 2.6 invalid rss url (1) - WWWPRIVATE;COM (1) - mysql permalinks wordpress (1) - wordpress media library url error (1) - wwwprivate@.com (1) - exstra. com (1) - wordpress medialibrary table db (1) - wordpress database libraries (1) - google wordpress Invalid URL (1) - database plugin wordpress query URL (1) - wordpress query_posts most commented post (1) - WWWPRIBATE .COM (1) - wwwprivat,com (1) - invalid requests 404 wordpress (1) - wordpress change url invalid (1) - optimize 404 url wordpress query (1) - wwwprivate.com othewise khowh as futanari (1) - inurl: Post a New Comment some html  (1) - Query was empty wordpress (1) - wordpress empty screen on invalid url (1) - wordpress homepage loads even with non existent URL? (1) - upgrade wordpress invalid url (1) - clean wordpress invalid urls in database (1) - send url request to db wordpress (1) - install wordpress invalid url (1) - wordpress return invalid search query (1) - wordpress invalid request url (1) - wwwprivatis.com (1) - invalid url in address bar in wordpress (1) - posting videos on wordpress invalid url (1) - google invalid url posts wordpress (1) - sitte:inurlblog url name (1) - invalid query: wordpress (1) - wordpress invalid urls (1) - wordpress POST URL DATABASE (1) - hack database from address bar on query (1) - url query_posts (1) - Database for Wordpress movies (1) - site:.com inurl inurl:blog “Comments” -”comments closed” -”you must be logged in” “screen “ (1) - inurl:"blog/post/“"Name”“Website”“Comment” (1) - permalink in wordpress invalid when posting (1) - WordPress-Datenbank-Fehler Query was empty für Abfrage wordpress (1) - wordpress non-existent posts (1) - sudden error WordPress database error: [Query was empty] (1) - wordpress reduce queries permalinks (1) - wordpress query optimization (1) - wordpress invalid rss Invalid URL (1) -