wp-includes/canonical.php
/**
* Attempts to guess correct post based on query vars.
*
* @since 2.3.0
* @uses $wpdb
*
* @return bool|string Returns False, if it can't find post, returns correct
* location on success.
*/
function redirect_guess_404_permalink() {
global $wpdb;
return false; // 关闭此功能
if ( !get_query_var('name') )
return false;
$where = $wpdb->prepare("post_name LIKE %s", get_query_var('name') . '%');
// if any of year, monthnum, or day are set, use them to refine the query
if ( get_query_var('year') )
$where .= $wpdb->prepare(" AND YEAR(post_date) = %d", get_query_var('year'));
if ( get_query_var('monthnum') )
$where .= $wpdb->prepare(" AND MONTH(post_date) = %d", get_query_var('monthnum'));
if ( get_query_var('day') )
$where .= $wpdb->prepare(" AND DAYOFMONTH(post_date) = %d", get_query_var('day'));
$post_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE $where AND post_status = 'publish'");
if ( !$post_id )
return false;
return get_permalink($post_id);
}
问题 : 大数据量 并发访问量多
时,造成效率低:like查询;YEAR/MONTH/DAYOFMONTH等系统函数的调用使得查询没使用合适的索引(时间索引)
mysql> SELECT ID FROM wp_posts WHERE post_name LIKE '%e5%8d%9a%e5%a3%ab%e5%bc%9f%e5%bc%9f%e5%90%ab%e6%b3%aa%e6%84%9f%e6%81%a935%e5%b2%81%e5%93%a5%e5%93%a5%e9%87%8d%e8%af%bb%e9%ab%98%e4%b8%ad%e6%a0%a1%e5%9b%ad-271%' AND YEAR(post_date) = 2008 AND MONTH(post_date) = 3 AND DAYOFMONTH(post_date) = 17 AND post_status = 'publish';
Empty set (1.76 sec)
mysql> SELECT ID FROM wp_posts WHERE post_name LIKE '%e5%8d%9a%e5%a3%ab%e5%bc%9f%e5%bc%9f%e5%90%ab%e6%b3%aa%e6%84%9f%e6%81%a935%e5%b2%81%e5%93%a5%e5%93%a5%e9%87%8d%e8%af%bb%e9%ab%98%e4%b8%ad%e6%a0%a1%e5%9b%ad-271%' AND post_date>='2008-03-17' and post_date<'2008-03-18' AND post_status = 'publish';
Empty set (0.00 sec)
评论