【发布时间】:2014-03-23 10:59:52
【问题描述】:
我有两台服务器(linode 3072 vps),一台(旧)有 ubuntu 11.04 + Mysql 5.5.32,另一台(新)有 centos 6.2。 + mysql 5.5.36。 My.cnf 文件也是一样的。但是,当我在同一个数据库(直接导出/导入)上运行相同的查询时,我从 2 个服务器获得了 2 个不同的响应时间和执行路径。
反应较快的年龄较大。
1 SIMPLE ch ref PRIMARY,channel_name channel_name 122 const 1 Using where; Using temporary; Using filesort
1 SIMPLE t ref PRIMARY,channel_id channel_id 4 bcc.ch.channel_id 1554
1 SIMPLE p ref PRIMARY PRIMARY 4 bcc.t.entry_id 1 Using index
1 SIMPLE c eq_ref PRIMARY,group_id PRIMARY 4 bcc.p.cat_id 1 Using where
较新的响应较慢。
1 SIMPLE ch ref PRIMARY,channel_name channel_name 122 const 1 Using where; Using temporary; Using filesort
1 SIMPLE p index PRIMARY PRIMARY 8 NULL 25385 Using index; Using join buffer
1 SIMPLE t eq_ref PRIMARY,channel_id PRIMARY 4 bcc.p.entry_id 1 Using where
1 SIMPLE c eq_ref PRIMARY,group_id PRIMARY 4 bcc.p.cat_id 1 Using where
最大的不同在于第二步。第一个服务器使用索引,只需扫描 1554 行,而第二个服务器使用索引 + 连接缓冲区,必须扫描 25385 行。有什么想法吗?
这样的查询和其他查询会导致某些页面上新服务器上的每个页面加载时间增加几秒钟。我正在使用 varnish 服务于前端,但仍想解决此问题。
这是正在运行的 sql
select SQL_NO_CACHE cat_name,cat_url_title, count(p.entry_id) as count
from exp_categories as c
join exp_category_posts as p on c.cat_id = p.cat_id
join exp_channel_titles as t on t.entry_id = p.entry_id
join exp_channels as ch on ch.channel_id = t.channel_id
where channel_name IN ('resources')
AND group_id = 2
group by cat_name
order by count desc
limit 5
【问题讨论】:
-
如果您发布查询以及相关的表定义,这将有所帮助。两台服务器上的字符集和表类型(innodb,...)是否相同?
-
查询在原帖中,类型还是MyISAM, UTF8 General。由于查询是针对 4 个不同的表,我可以发布 defs,但认为它会变得太长,并且不确定它会如何改变事情,因为它们是完全相同的 db(我在服务器上运行 db export,而不是db 导入,仅此而已)。我只是想弄清楚为什么在同一个 db + 查询上有 2 个不同的执行路径。
-
抱歉询问;我编辑了这个问题,使其更加突出。 group_id 是 exp_category_posts 中的一列吗?如果是,则第二个优化器似乎认为它可以使用此列快速减少行数。如果该猜测是错误的,请尝试 exp_category_posts 上的分析表 (dev.mysql.com/doc/refman/5.0/en/analyze-table.html) 是否发生变化。
-
谢谢。我进行了分析,结果有所不同。
标签: mysql sql linux ubuntu centos