两个相同的MYSQL表之间的性能差异

时间:2018-02-19 16:40:49

标签: mysql performance indexing innodb

我已将数据库(InnoDB)从我的数据库复制到另一个数据库,包括所有内容和索引。两个数据库都已经过优化,因此碎片不是问题。所有都位于相同的VPS上。

我注意到我要解决的两者之间的性能差异(10-20%)。

database1.table(使用索引条件;使用where)

EXPLAIN SELECT price_range FROM database1.table WHERE category = 'seating' AND visibility = 'show' GROUP BY price_range ORDER BY price_range ASC   

[id] => 1 [select_type] => SIMPLE [table] => items [type] => ref [possible_keys] => visibility_producer,price_range_visibility,category_visibility,visibility_category_price_range [key] => visibility_category_price_range [key_len] => 228 [ref] => const,const [rows] => 10106 [Extra] => Using index condition; Using where

database2.table(使用where;使用索引)

EXPLAIN SELECT price_range FROM database2.table WHERE category = 'seating' AND visibility = 'show' GROUP BY price_range ORDER BY price_range ASC    

[id] => 1 [select_type] => SIMPLE [table] => items [type] => ref [possible_keys] => visibility_producer,price_range_visibility,category_visibility,visibility_category_price_range [key] => visibility_category_price_range [key_len] => 228 [ref] => const,const [rows] => 10106 [Extra] => Using where; Using index

我认为性能差异是因为database1.table使用索引条件而database2.table使用索引。相同的查询,相同的索引,相同的服务器。

这怎么可能?关于在哪里看的任何提示?

1 个答案:

答案 0 :(得分:1)

如有疑问,请确认!

这些表实际上并不完全相同。在使用以下内容检查表时会显示:

SHOW CREATE TABLE <tablename>

在这种情况下,检查发现表重复时没有复制两个外键约束。

任何外键都需要索引,因此如果不使用现有索引,它可以隐式构建一个索引。如果没有外键定义,在这种情况下两个表之间存在索引差异。