From 4bbee1b73c502edae1a0f60607487a3679e98729 Mon Sep 17 00:00:00 2001 From: Ryan Boren Date: Sun, 2 Apr 2006 00:33:10 +0000 Subject: [PATCH] Some optimization of the post query. #2604 git-svn-id: https://develop.svn.wordpress.org/trunk@3678 602fd350-edb4-49c9-b593-d223f7449a82 --- wp-admin/upgrade-functions.php | 2 +- wp-admin/upgrade-schema.php | 5 +++-- wp-includes/query.php | 18 ++++-------------- wp-includes/template-functions-links.php | 4 ++-- wp-includes/version.php | 2 +- 5 files changed, 11 insertions(+), 20 deletions(-) diff --git a/wp-admin/upgrade-functions.php b/wp-admin/upgrade-functions.php index 9c5d57f489..160360b2c4 100644 --- a/wp-admin/upgrade-functions.php +++ b/wp-admin/upgrade-functions.php @@ -166,7 +166,7 @@ function upgrade_all() { if ( $wp_current_db_version < 3308 ) upgrade_160(); - if ( $wp_current_db_version < 3582 ) + if ( $wp_current_db_version < 3672 ) upgrade_210(); $wp_rewrite->flush_rules(); diff --git a/wp-admin/upgrade-schema.php b/wp-admin/upgrade-schema.php index c4cefffac5..3bb399a37a 100644 --- a/wp-admin/upgrade-schema.php +++ b/wp-admin/upgrade-schema.php @@ -113,11 +113,12 @@ CREATE TABLE $wpdb->posts ( post_parent bigint(20) NOT NULL default '0', guid varchar(255) NOT NULL default '', menu_order int(11) NOT NULL default '0', - post_type varchar(100) NOT NULL default 'post', + post_type varchar(20) NOT NULL default 'post', post_mime_type varchar(100) NOT NULL default '', comment_count bigint(20) NOT NULL default '0', PRIMARY KEY (ID), - KEY post_name (post_name) + KEY post_name (post_name), + KEY type_status_date (post_type, post_status, post_date) ); CREATE TABLE $wpdb->users ( ID bigint(20) unsigned NOT NULL auto_increment, diff --git a/wp-includes/query.php b/wp-includes/query.php index e40a2e5e80..240b51d756 100644 --- a/wp-includes/query.php +++ b/wp-includes/query.php @@ -505,7 +505,6 @@ class WP_Query { $result = ''; $where = ''; $limits = ''; - $distinct = ''; $join = ''; if ( !isset($q['post_type']) ) @@ -822,15 +821,6 @@ class WP_Query { $q['orderby'] = 'post_date '.$q['order']; } - //$now = gmdate('Y-m-d H:i:59'); - - //only select past-dated posts, except if a logged in user is viewing a single: then, if they - //can edit the post, we let them through - //if ($pagenow != 'post.php' && $pagenow != 'edit.php' && !($this->is_single && $user_ID)) { - // $where .= " AND post_date_gmt <= '$now'"; - // $distinct = 'DISTINCT'; - //} - if ( $this->is_attachment ) { $where .= ' AND (post_type = "attachment")'; } elseif ($this->is_page) { @@ -842,8 +832,6 @@ class WP_Query { if ( is_admin() ) $where .= " OR post_status = 'future' OR post_status = 'draft'"; - else - $distinct = 'DISTINCT'; if ( is_user_logged_in() ) $where .= " OR post_author = $user_ID AND post_status = 'private'))"; @@ -884,11 +872,13 @@ class WP_Query { // Apply post-paging filters on where and join. Only plugins that // manipulate paging queries should use these hooks. $where = apply_filters('posts_where_paged', $where); - $groupby = " $wpdb->posts.ID "; + $groupby = ''; $groupby = apply_filters('posts_groupby', $groupby); + if ( ! empty($groupby) ) + $groupby = 'GROUP BY ' . $groupby; $join = apply_filters('posts_join_paged', $join); $orderby = apply_filters('posts_orderby', $q['orderby']); - $request = " SELECT $distinct * FROM $wpdb->posts $join WHERE 1=1" . $where . " GROUP BY " . $groupby . " ORDER BY " . $orderby . " $limits"; + $request = " SELECT * FROM $wpdb->posts $join WHERE 1=1 $where $groupby ORDER BY $orderby $limits"; $this->request = apply_filters('posts_request', $request); $this->posts = $wpdb->get_results($this->request); diff --git a/wp-includes/template-functions-links.php b/wp-includes/template-functions-links.php index 1d991f0d2f..d2beb5ffd4 100644 --- a/wp-includes/template-functions-links.php +++ b/wp-includes/template-functions-links.php @@ -470,13 +470,13 @@ function _max_num_pages() { if (isset($max_num_pages)) return $max_num_pages; if ( 'posts' == get_query_var('what_to_show') ) { - preg_match('#FROM\s(.*)\sGROUP BY#siU', $wp_query->request, $matches); + preg_match('#FROM\s(.*)\sORDER BY#siU', $wp_query->request, $matches); $fromwhere = $matches[1]; $numposts = $wpdb->get_var("SELECT COUNT(DISTINCT ID) FROM $fromwhere"); return ceil($numposts / get_option('posts_per_page')); } else { $posts = $wp_query->posts; - preg_match('#WHERE\s(.*)\sGROUP BY#siU', $wp_query->request, $matches); + preg_match('#WHERE\s(.*)\sORDER BY#siU', $wp_query->request, $matches); $where = preg_replace('/( AND )?post_date >= (\'|\")(.*?)(\'|\")( AND post_date <= (\'\")(.*?)(\'\"))?/siU', '', $matches[1]); $num_days = $wpdb->query("SELECT DISTINCT post_date FROM $wpdb->posts WHERE $where GROUP BY year(post_date), month(post_date), dayofmonth(post_date)"); return ceil($num_days / get_option('posts_per_page')); diff --git a/wp-includes/version.php b/wp-includes/version.php index 7430ed79e5..c91a6bafdb 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -3,6 +3,6 @@ // This just holds the version number, in a separate file so we can bump it without cluttering the SVN $wp_version = '2.1-alpha1'; -$wp_db_version = 3582; +$wp_db_version = 3672; ?> \ No newline at end of file