Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql-where子句比完全扫描慢_Mysql - Fatal编程技术网

Mysql-where子句比完全扫描慢

Mysql-where子句比完全扫描慢,mysql,Mysql,询问 问题B 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**) 我不明白为什么查询B需要20秒,而查询A需要0秒。关于“地位”的索引。prod和dev.serv

询问

问题B

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**)

我不明白为什么查询B需要20秒,而查询A需要0秒。关于“地位”的索引。prod和dev.server上的结果相同。

0.00秒听起来查询可能已缓存。这就是说,对表的
id
的查询可以直接从主键索引(即,根本不看表数据!)得到回答,而带有
WHERE
子句的查询需要数据库实际读取行。

状态上有一个索引,但是,一旦MySQL确定了哪些行的状态为
status=0
,就会调用每一行来查找其id。如果您
在作业上创建索引idx\u jobs\u status\u id(status,id)我预计20秒将大大缩短。

尝试将索引更改为两列
(状态,id)

在我看来,在id上没有任何索引

(查找“
Scalar expressions 125
”)说明
COUNT(*)
给出表的行数:打算从一开始就对其进行优化

如果指定了COUNT(*),则结果是T的基数

这就是为什么
COUNT(*)
COUNT(id)
快得多的原因<代码>计数(*)
可以使用
状态
索引<代码>计数(id)
将不使用此索引,并且没有其他有效索引


什么是聚集索引,什么是PK?两个都有(即使索引相同),对吗?

0.00秒听起来像是缓存了查询。是否在测试之间刷新缓存?重置查询缓存;从nsync.jobs中选择id;->10705行(0.00秒)。仍然是相同的结果:(嗯。如果所有的时间都是“发送数据”,那么当你只要求一个
COUNT()
匹配查询而不是行时,查询时间是多少?你能在查询前使用
EXPLAIN
语句,然后用它的结果编辑你的问题吗?这里我添加了EXPLAIN。选择COUNT(*)其中状态=0;需要0秒。选择计数(id)需要20秒。奇怪奇怪。从作业中选择国家;10705结果为0秒从状态为0的作业中选择国家;7933结果为20秒。就是这样!现在开始工作。非常感谢。现在我明白了为什么索引会有几个值…这正是它。我只有id上的索引…但没有带状态和id的索引…现在我知道了了解差异。它工作起来很有魅力。非常感谢。@Benjamin:id是聚集索引/PK吗?如果是,那么你不应该需要复合索引,因为如果索引是如何构造的,它是主键。“聚集索引”是什么
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)