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 (188) - wwwprivate (13) - exstra sex (11) - www.sex view extra (9) - sex view extra (9) - wordpress (8) - invalid sex (6) - wwwprivate com (6) - parse_request wordpress (5) - sex viwe (5) - invalid porno (5) - exstra sexs (5) - wordpress invalid url (4) - WWWprivate,com (4) - WWW.SEX VIEW TV.COM (3) - invalid urls (3) - invalid sex com (3) - porno invalid (3) - invalide sex (3) - wordpress reduce queries (3) - 404 wordpress (3) - wordpress reduce mysql query (2) - invalidensex (2) - wordpress query_vars (2) - sex mit invaliden video (2) - wwwprivate.com. (2) - reduce wordpress queries (2) - wordpress query database (2) - sexs extra (2) - www sex view extra (2) - database for wordpress (2) - Xtra Arab Sex (2) - favicon 404 error wordpress (2) - extra sex video (2) - invalid sex.com (2) - invalidesex (2) - invalidi i sex (2) - free invalide sex video (2) - www.sex exstra 18 tv (2) - TV.com virgin (2) - www.sex.view.extra (2) - exstra sex.com (2) - www.sex.view (2) - wordpress non existing category 404 (1) - Wordpress feed invalid (1) - wordpress reduce 404 queries (1) - invalid sexs (1) - sex viwe exstra (1) - query, post type, URL, WordPRess (1) - optimize SQL_CALC_FOUND_ROWS wordpress (1) - reducing wordpress queries (1) - parse_request() wordpress (1) - Invalid database URL (1) - wordpress media library invalid url (1) - wordpress query_posts post_status (1) - invalid 404 (1) - favicon.ico error wordpress (1) - wordpress 404 showing blank (1) - wordpress permalinks pagename (1) - wordpress parse_request (1) - clear query wordpress (1) - sex kod invalida (1) - sex met invalid (1) - sex invalid (1) - changed wordpress url to invalid url (1) - extra sex view (1) - free invalid sex videos (1) - wordpress medialibrary table db (1) - wordpress Blog permalinks return 404 but still return page (1) - sex mit invaliden free video (1) - wordpress query on permalink (1) - invalid sex videos (1) - sex exstra com (1) - www.sex viwe.com (1) - invalid sex video (1) - wordpress 2.6 invalid rss url (1) - wordpress query_posts most commented post (1) - sex viwe .com (1) - wordpress media library database table (1) - ? www.sex viwe.com (1) - ? www.sex vew tv.com (1) - empty archives wordpress post not found (1) - exstra. com (1) - WWWPRIVATE;COM (1) - mysql permalinks wordpress (1) - wordpress media library database (1) - wordpress media library urls (1) - wordpress get posts database query (1) - permalink from db wordpress (1) - wordpress and invalid rss (1) - The requested URL /", is invalid. wordpress (1) - wordpress reducing queries (1) - sexs invalid (1) - wordpress query permalink (1) - database plugin wordpress query URL (1) - google wordpress Invalid URL (1) - wordpress database libraries (1) - wwwprivate@.com (1) - wwwprivate sexs tv com (1) - wordpress media library url error (1) - sexe exstra com (1) - www.sex view.tv (1) - www google earth com free virgin (1) - Invalid URI in request in yahoo (1) - how queries wordpress (1) - [error] => 404 wordpress query_vars (1) - wordpress query db (1) - wordpress query (1) - why 1=1 in wordpress query (1) - wordpress post database queries (1) - permalink extra stuff wordpress (1) - wordpress invalid permalink (1) - wordpress music database library (1) - wordpress tag union causing 404 errors (1) - wordpress urls in database (1) - wordpress plugin reduce database queries (1) - query_vars wordpress (1) - reduce queries in wordpress (1) - bluetooth hacks database (1) - database url wordpress (1) - wordpress database query (1) - reduce queries wordpress (1) - extra sex (1) - o (1) - how to reduce wordpress queries (1) - http://www google dk/reader/view/ (1) - wordpress Invalid Request (1) - mysql wordpress queries (1) - extra URL wordpress (1) - permalink wordpress query (1) - i nvalid urls (1) - wordpress urls (1) - how wordpress urls are processed (1) - get for invalid urls (1) - database permalink wordpress (1) - wordpress permalink error (1) - wordpress rss invalid url (1) - wordpress get queries (1) - 2008 wordpress permalink Error 404: Page that you've requested could not be found. (1) - invalide sex movie (1) - wordpress query url (1) - database permalinks google (1) - reduce mysql wordpress queries (1) - google rss error invalid url wordpress (1) - wordpress permalink returns blank page (1) - wordpress feed invalid url (1) - Error 404 - Not Found wordpress update posts admin (1) - wwwprivate.c501 (1) - wordpress get a posts full url from the database (1) - Reducing mysql queries Wordpress (1) -