MySql同一查询的不同执行路径

use*_*584 3 mysql sql linux ubuntu centos

我有两台服务器(linode 3072 vps),一台(较旧)有 ubuntu 11.04 + Mysql 5.5.32,另一台(较新)有 centos 6.2。+ MySQL 5.5.36。My.cnf 文件也相同。但是,当我在同一个数据库上运行相同的查询(直接导出/导入)时,我从两台服务器获得了 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
Run Code Online (Sandbox Code Playgroud)

较新的响应速度较慢。

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
Run Code Online (Sandbox Code Playgroud)

最大的区别在于第二步。第一个服务器使用索引,只需扫描 1554 行,而第二个服务器使用索引 + 连接缓冲区,只需扫描 25385 行。有什么想法吗?

像这样的查询和其他查询会导致新服务器上某些页面的每个页面加载时间增加几秒。我正在使用清漆来服务前端,但仍然想解决这个问题。

这是正在运行的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 
Run Code Online (Sandbox Code Playgroud)

noz*_*noz 5

MySQL 中的查询优化器根据索引和表的统计信息选择要使用的索引。有时索引的选择并不是最优的,并且查询执行也不同。

我们在数据库中发现,在一天中的某些时刻,MySQL 会更改其用于同一查询的执行路径。

你可以尝试

analyze table exp_categories,exp_category_posts,exp_channel_titles,exp_channels ;
Run Code Online (Sandbox Code Playgroud)

这有时会改善执行计划。或者使用索引提示来确定使用哪些索引。