Select id from jobs;
| 55966 |
| 55971 |
+-------+
10705 rows in set (0.00 sec)
查詢乙
Select id from jobs where status = 0;
| 55966 |
| 55971 |
+-------+
7933 rows in set (**20.22 sec**)
較慢有上狀態的指標。
mysql> explain select id from jobs where status = 0;
+----+-------------+-------+------+---------------+------+---------+------+-------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+------+---------------+------+---------+------+-------+-------------+
| 1 | SIMPLE | jobs | ALL | status | NULL | NULL | NULL | 10705 | Using where |
+----+-------------+-------+------+---------------+------+---------+------+-------+-------------+
1 row in set (0.01 sec)
mysql> show profile for query 1;
+--------------------------------+-----------+
| Status | Duration |
+--------------------------------+-----------+
| starting | 0.000023 |
| checking query cache for query | 0.000039 |
| checking permissions | 0.000006 |
| Opening tables | 0.000008 |
| System lock | 0.000004 |
| Table lock | 0.000016 |
| init | 0.000021 |
| optimizing | 0.000007 |
| statistics | 0.000904 |
| preparing | 0.000023 |
| executing | 0.000003 |
| Sending data | 19.751547 |
| end | 0.000009 |
| query end | 0.000002 |
| freeing items | 0.001561 |
| storing result in query cache | 0.000122 |
| logging slow query | 0.000002 |
| logging slow query | 0.000002 |
| cleaning up | 0.000003 |
+--------------------------------+-----------+
mysql> show index from jobs;
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| jobs | 1 | status | 1 | status | A | 6 | NULL | NULL | YES | BTREE | |
| jobs | 1 | date | 1 | dateinit | A | 1784 | NULL | NULL | YES | BTREE | |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
10 rows in set (0.02 sec)
我不明白爲什麼查詢B需要20秒,而查詢A需要0。 「狀態」索引。 prod和dev的結果相同。服務器。
0.00秒聽起來像查詢被緩存。你是否在測試之間刷新緩存? –
RESET QUERY CACHE;從nsync.jobs中選擇ID; - > 10705行(0.00秒)。還是同樣的結果:?( – Benjamin
嗯如果所有的時間「發送數據」,什麼是查詢的時候,你問的只是一個'COUNT()'匹配查詢,而不是行的 –