低效的 SQL
我不是 MySQL 专家,但到目前为止我已经设法将一些可行的东西组合在一起。不幸的是,我最近的失败尝试导致服务器崩溃,所以显然我正在做一些效率极低的事情。谁能告诉我问题出在哪里以及如何在不每次都导致整个网站瘫痪的情况下获得相同的结果?
$sqlbest = "SELECT
wp_postmeta.meta_value
, wp_posts.post_title
, wp_posts.ID
, (TO_DAYS(CURDATE())- TO_DAYS(wp_posts.post_date))+1 AS days
FROM `wp_postmeta` , `wp_posts`
WHERE `wp_postmeta`.`post_id` = `wp_posts`.`ID`
AND `wp_posts`.`post_date` >= DATE_SUB( CURDATE( ) , INTERVAL 1 WEEK)
AND `wp_postmeta`.`meta_key` = 'views'
AND `wp_posts`.`post_status` = 'publish'
AND wp_posts.ID != '".$currentPostID."'
GROUP BY `wp_postmeta`.`post_id`
ORDER BY (CAST( `wp_postmeta`.`meta_value` AS UNSIGNED ) / days) DESC
LIMIT 0 , 4";
$results = $wpdb->get_results($sqlbest);
它使用帖子浏览次数来计算最近发布的帖子的浏览量/天,然后按该数字对它们进行排序,并获取前 4 个。
我想我发现它效率很低,因为它必须每次都计算浏览量/天几千个帖子,但我不知道如何做得更好。
提前致谢。
I'm no MySQL expert, but I've managed until now to hack together something that works. Unfortunately, my latest bodged attempt results in the server dying, so obviously I'm doing something that is massively inefficient. Can anyone give me a hint as to where the problem is and how I might get the same results without bringing the whole site down everytime?
$sqlbest = "SELECT
wp_postmeta.meta_value
, wp_posts.post_title
, wp_posts.ID
, (TO_DAYS(CURDATE())- TO_DAYS(wp_posts.post_date))+1 AS days
FROM `wp_postmeta` , `wp_posts`
WHERE `wp_postmeta`.`post_id` = `wp_posts`.`ID`
AND `wp_posts`.`post_date` >= DATE_SUB( CURDATE( ) , INTERVAL 1 WEEK)
AND `wp_postmeta`.`meta_key` = 'views'
AND `wp_posts`.`post_status` = 'publish'
AND wp_posts.ID != '".$currentPostID."'
GROUP BY `wp_postmeta`.`post_id`
ORDER BY (CAST( `wp_postmeta`.`meta_value` AS UNSIGNED ) / days) DESC
LIMIT 0 , 4";
$results = $wpdb->get_results($sqlbest);
It uses a post views count to calculate views/day for posts published in the last, then orders them by that number, and grabs the top 4.
I think I see that it's inefficient in that it has to calculate that views/day everytime for a few thousand posts, but I don't know how to do it any better.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以消除每次调用这些日期函数的需要,方法是将它们静态地传递到 PHP 服务器(可能不与数据库同步)的查询中,或者您可以编写一个存储过程并保存这些日期函数的结果到随后将在查询中使用的变量。
You could eliminate the need to call those date functions every time by either passing them statically into the query from your PHP server (which may not be synced with your database) or you can instead write a stored procedure and save the results of those date functions to variables that will then be used in the query.
我尝试做一些改变。
TO_DAYS
的两次调用替换为对DATEDIFF
的一次调用。inner join
替换了丑陋的隐式where join,这不会做任何事情,只是让事情变得更清晰。它显示的一件事是,如果wp_postmeta.post_id
是唯一的,那么您不需要分组依据,因为内部联接只会为每个wp_postmeta 提供一行。 post_id
。group by
只有当wp_postmeta.post_id
唯一时这才是正确的!I've tried to make a few changes.
TO_DAYS
with one call toDATEDIFF
.inner join
this does not do anything, just makes things clearer. One thing it shows, ifwp_postmeta.post_id
is unique, then you do not need the group by, because the inner join will only give one row perwp_postmeta.post_id
.group by
this is only right ifwp_postmeta.post_id
is unique!