我有一个数据库“代数”,其中包含一个包含 1,033,990 行的“问题”表。记录有一个属性“已解决”,即 INT。我有一个简单的查询
select count(*) from questions where solved = 0
Run Code Online (Sandbox Code Playgroud)
我有两台 CPU 相似的服务器。在两台服务器上,表是相同的。(数据库是生产的副本)。它们在 SSD 上。一台服务器有 Ubuntu 14.04 和 MySQL 5.5.49,另一台服务器有 Ubuntu 16.04 和 MySQL 5.7.12。
问题是这个查询在 MySQL 5.5 上只需要 0.009s,但在 MySQL 5.7 上需要 0.304s。慢了 34 倍!!!
查询计划大致相似:
服务器慢:
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
1 SIMPLE questions NULL index NULL by_topic_solved 97 NULL 1033990 10.00 Using where; Using index
Run Code Online (Sandbox Code Playgroud)
快速服务器:
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE …Run Code Online (Sandbox Code Playgroud)