为什么我操作的是非索引列,查询还是那么快?
Why query is still so fast when I operate a non-indexing column?
我正在学习数据库索引。
这里是 table 的索引。而这个 table 有 330k 条记录。
mysql> show index from employee;
+----------+------------+-------------+--------------+---------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | Visible | Expression |
+----------+------------+-------------+--------------+---------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
| employee | 0 | PRIMARY | 1 | id | A | 297383 | NULL | NULL | | BTREE | | | YES | NULL |
| employee | 0 | ak_employee | 1 | personal_code | A | 297383 | NULL | NULL | | BTREE | | | YES | NULL |
| employee | 1 | idx_email | 1 | email | A | 297383 | NULL | NULL | | BTREE | | | YES | NULL |
+----------+------------+-------------+--------------+---------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
如您所见,此table上只有三个索引。
现在我想用 birth_date
列上的 where 查询,我认为它会很慢,因为 birth-date
列上没有索引,我在尝试查询时发现它是非常快。
mysql> select sql_no_cache *
-> from employee
-> where birth_date > '1955-11-11'
-> limit 100
-> ;
100 rows in set, 1 warning (0.04 sec)
所以我很困惑:
- 为什么没有索引还是那么快?
- 既然它仍然很快,为什么我们还需要索引?
这是您的查询:
select sql_no_cache *
from employee
where birth_date > '1955-11-11'
limit 100
没有索引,因此查询开始从数据页读取数据。在每条记录上,它比较生日和 returns 行。当它找到 100 时(由于 limit
)它停止。
据推测,它很快就找到了 100 行。毕竟,美国的中位年龄约为 38 岁——(在我写这篇文章时)出生年份是 1981 年。到目前为止,大多数人出生于 1955 年之后。
如果您有 order by
或 group by
,查询会慢得多。这将需要在返回任何内容之前读取所有数据。
我正在学习数据库索引。
这里是 table 的索引。而这个 table 有 330k 条记录。
mysql> show index from employee;
+----------+------------+-------------+--------------+---------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | Visible | Expression |
+----------+------------+-------------+--------------+---------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
| employee | 0 | PRIMARY | 1 | id | A | 297383 | NULL | NULL | | BTREE | | | YES | NULL |
| employee | 0 | ak_employee | 1 | personal_code | A | 297383 | NULL | NULL | | BTREE | | | YES | NULL |
| employee | 1 | idx_email | 1 | email | A | 297383 | NULL | NULL | | BTREE | | | YES | NULL |
+----------+------------+-------------+--------------+---------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
如您所见,此table上只有三个索引。
现在我想用 birth_date
列上的 where 查询,我认为它会很慢,因为 birth-date
列上没有索引,我在尝试查询时发现它是非常快。
mysql> select sql_no_cache *
-> from employee
-> where birth_date > '1955-11-11'
-> limit 100
-> ;
100 rows in set, 1 warning (0.04 sec)
所以我很困惑:
- 为什么没有索引还是那么快?
- 既然它仍然很快,为什么我们还需要索引?
这是您的查询:
select sql_no_cache *
from employee
where birth_date > '1955-11-11'
limit 100
没有索引,因此查询开始从数据页读取数据。在每条记录上,它比较生日和 returns 行。当它找到 100 时(由于 limit
)它停止。
据推测,它很快就找到了 100 行。毕竟,美国的中位年龄约为 38 岁——(在我写这篇文章时)出生年份是 1981 年。到目前为止,大多数人出生于 1955 年之后。
如果您有 order by
或 group by
,查询会慢得多。这将需要在返回任何内容之前读取所有数据。