【发布时间】:2016-09-14 03:03:06
【问题描述】:
我有两个表,user和sku_pro,两个表中的count查询如下
```
mysql> SELECT COUNT(id) FROM user WHERE user.is_active = 1 \G;
计数(id):239568
一组中的 1 行(0.20 秒)
mysql> SELECT COUNT(id) FROM sku_pro WHERE is_agent=1 \G;
计数(id):1254286
集合中的 1 行(0.11 秒)
```
user有61列,运行show table status in user table,结果是
mysql> show table status like 'user' \G;
*************************** 1. row ***************************
Name: user
Engine: MyISAM
Version: 10
Row_format: Dynamic
Rows: 239606
Avg_row_length: 252
Data_length: 60483836
Max_data_length: 281474976710655
Index_length: 34577408
Data_free: 0
Auto_increment: 239623
Create_time: 2016-08-24 12:01:55
Update_time: 2016-09-14 10:48:10
Check_time: 2016-08-24 12:02:04
Collation: utf8_bin
Checksum: NULL
Create_options:
Comment:
sku_pro 有 16 列,运行 show table status in sku_pro table,结果是
mysql> show table status like 'sku_pro' \G;
*************************** 1. row ***************************
Name: sku_pro
Engine: MyISAM
Version: 10
Row_format: Fixed
Rows: 1281901
Avg_row_length: 53
Data_length: 67940753
Max_data_length: 14918173765664767
Index_length: 52064256
Data_free: 0
Auto_increment: 1988051
Create_time: 2016-09-09 14:06:37
Update_time: 2016-09-14 10:19:39
Check_time: 2016-09-09 14:06:44
Collation: utf8_general_ci
Checksum: NULL
Create_options:
Comment:
1 row in set (0.01 sec)
我不知道为什么 user 中的 count(id) 比 sku_pro 中的 count(id) 慢。
【问题讨论】:
-
我要做的第一件事是比较每个查询的 EXPLAIN 计划。
user.is_active和sku_pro.is_agent列是否都已编入索引? -
@Phil 没有索引。也许我已经找到原因了。 user 表中的 row_format 是动态的,但在 sku_pro 表中是固定的。与 sku_pro.is_agent 相比,我需要更多时间来查找 user.is_active 的值。