提高大型数据库的SQL查询速度?

时间:2011-06-15 作者:Tal Galili

我正在使用一个名为feedwordpress 为了在wordpress上运行一个类似行星的网站(请参见here).

这个插件非常棒,除了一件事——它会占用我的(VPS)服务器,每周提交一次。

在最近与webadmin的电子邮件交流中,他写道:

看起来mysql资源使用量的增加是由r-bloggers运行的查询速度慢造成的。com。这是正在生成的一些日志的副本。您需要进一步优化此网站和数据库,以使其尽可能高效地运行。如果已经进行了这些更改,您最好的选择是考虑对VPS进行大规模升级,因为您的站点需要和看到高级别的资源和流量。

以下是日志:

# Time: 110614 16:11:35
# User@Host: rblogger_rblogr[rblogger_rblogr] @ localhost []
# Query_time: 104 Lock_time: 0 Rows_sent: 0 Rows_examined: 54616
SELECT SQL_CALC_FOUND_ROWS wp_rb_posts.* FROM wp_rb_posts WHERE 1=1 AND ((guid = \'235cbefa4424d0cdb7b6213f15a95ded\') OR (guid = \'http://www.r-bloggers.com/?guid=235cbefa4424d0cdb7b6213f15a95ded\') OR (guid = \'http://www.r-bloggers.com/?guid=235cbefa4424d0cdb7b6213f15a95ded\') OR (MD5(guid) = \'235cbefa4424d0cdb7b6213f15a95ded\')) AND wp_rb_posts.post_type IN (\'post\', \'page\', \'attachment\', \'revision\', \'nav_menu_item\') AND (wp_rb_posts.post_status = \'publish\' OR wp_rb_posts.post_status = \'future\' OR wp_rb_posts.post_status = \'draft\' OR wp_rb_posts.post_status = \'pending\' OR wp_rb_posts.post_status = \'trash\' OR wp_rb_posts.post_status = \'auto-draft\' OR wp_rb_posts.post_status = \'inherit\' OR wp_rb_posts.post_status = \'private\') ORDER BY wp_rb_posts.post_date DESC LIMIT 1570, 10;
# User@Host: rblogger_rblogr[rblogger_rblogr] @ localhost []
# Query_time: 237 Lock_time: 0 Rows_sent: 0 Rows_examined: 54616
SELECT SQL_CALC_FOUND_ROWS wp_rb_posts.* FROM wp_rb_posts WHERE 1=1 AND ((guid = \'235cbefa4424d0cdb7b6213f15a95ded\') OR (guid = \'http://www.r-bloggers.com/?guid=235cbefa4424d0cdb7b6213f15a95ded\') OR (guid = \'http://www.r-bloggers.com/?guid=235cbefa4424d0cdb7b6213f15a95ded\') OR (MD5(guid) = \'235cbefa4424d0cdb7b6213f15a95ded\')) AND wp_rb_posts.post_type IN (\'post\', \'page\', \'attachment\', \'revision\', \'nav_menu_item\') AND (wp_rb_posts.post_status = \'publish\' OR wp_rb_posts.post_status = \'future\' OR wp_rb_posts.post_status = \'draft\' OR wp_rb_posts.post_status = \'pending\' OR wp_rb_posts.post_status = \'trash\' OR wp_rb_posts.post_status = \'auto-draft\' OR wp_rb_posts.post_status = \'inherit\' OR wp_rb_posts.post_status = \'private\') ORDER BY wp_rb_posts.post_date DESC LIMIT 570, 10;
# Time: 110614 16:18:13
# User@Host: rblogger_rblogr[rblogger_rblogr] @ localhost []
# Query_time: 257 Lock_time: 0 Rows_sent: 0 Rows_examined: 54616
SELECT SQL_CALC_FOUND_ROWS wp_rb_posts.* FROM wp_rb_posts WHERE 1=1 AND ((guid = \'956e208f101562f6654e88e9711276e4\') OR (guid = \'http://www.r-bloggers.com/?guid=956e208f101562f6654e88e9711276e4\') OR (guid = \'http://www.r-bloggers.com/?guid=956e208f101562f6654e88e9711276e4\') OR (MD5(guid) = \'956e208f101562f6654e88e9711276e4\')) AND wp_rb_posts.post_type IN (\'post\', \'page\', \'attachment\', \'revision\', \'nav_menu_item\') AND (wp_rb_posts.post_status = \'publish\' OR wp_rb_posts.post_status = \'future\' OR wp_rb_posts.post_status = \'draft\' OR wp_rb_posts.post_status = \'pending\' OR wp_rb_posts.post_status = \'trash\' OR wp_rb_posts.post_status = \'auto-draft\' OR wp_rb_posts.post_status = \'inherit\' OR wp_rb_posts.post_status = \'private\') ORDER BY wp_rb_posts.post_date DESC LIMIT 570, 10;
# Time: 110614 16:19:02
# User@Host: rblogger_rblogr[rblogger_rblogr] @ localhost []
# Query_time: 83 Lock_time: 0 Rows_sent: 0 Rows_examined: 54616
SELECT SQL_CALC_FOUND_ROWS wp_rb_posts.* FROM wp_rb_posts WHERE 1=1 AND ((guid = \'6c589e661f03a67b0529fab2f080bfd3\') OR (guid = \'http://www.r-bloggers.com/?guid=6c589e661f03a67b0529fab2f080bfd3\') OR (guid = \'http://www.r-bloggers.com/?guid=6c589e661f03a67b0529fab2f080bfd3\') OR (MD5(guid) = \'6c589e661f03a67b0529fab2f080bfd3\')) AND wp_rb_posts.post_type IN (\'post\', \'page\', \'attachment\', \'revision\', \'nav_menu_item\') AND (wp_rb_posts.post_status = \'publish\' OR wp_rb_posts.post_status = \'future\' OR wp_rb_posts.post_status = \'draft\' OR wp_rb_posts.post_status = \'pending\' OR wp_rb_posts.post_status = \'trash\' OR wp_rb_posts.post_status = \'auto-draft\' OR wp_rb_posts.post_status = \'inherit\' OR wp_rb_posts.post_status = \'private\') ORDER BY wp_rb_posts.post_date DESC LIMIT 1440, 10;
这就引出了我的问题——日志中的哪些内容可能会向我表明发生了什么(为什么这样的查询需要这么长时间?)?是否可以优化这些?如果是,如何?

谢谢Tal

1 个回复
SO网友:anu

您的日志显示,MySQL正在对所有表记录执行线性扫描,以获取必要的行-这是因为没有索引guid

假设wp\\u rb\\u posts只是带有略微修改的前缀的普通posts表,那么您只需为该字段添加一个索引,就会看到显著的改进。

执行此操作的SQL语句是:

CREATE INDEX guid ON wp_rb_posts(guid);

结束

相关推荐

Integrating plugins in themes

我找不到讨论这个的帖子,所以开始这篇。我目前正在为3.1+开发一个相当复杂的主题,我的意思是,除了样式和常规的前端功能之外,我还在主题的核心包括后端和前端的插件。因此,为了使这一点更有条理,我将其分为三个问题:集成插件是一种常见的做法吗</自动更新主题/插件有什么影响/复杂之处</在不破坏现有功能的情况下,包含每个插件的最佳方式是什么