gpt4 book ai didi

mysql - 最左边的列在 MySQL 的索引中重要吗?

转载 作者:搜寻专家 更新时间:2023-10-30 23:37:25 24 4
gpt4 key购买 nike

我需要知道最左边的列在索引中很重要

我的表包含以下字段

id (int Primary key)
first_name(varchar(50))
last_name varchar(50)

我的索引是

Create index ix_name_1 on name(last_name,first_name)

查询

当我运行以下查询时,它采用正确的索引

Select *
from name
where first_name = 'firstName' and last_name ='lastName'

select *
from name
where last_name = 'lastName' and first_name = 'firstName'

但是当我运行的时候

Select * from name where first_name = 'firstName'

它进行表扫描。

我发现 last_name 是最左边的列,我尝试对不同的表和不同的查询进行同样的操作,方法是在 where 子句中遗漏最左边的列,同样的情况发生了。

所以我需要知道在索引中最左边的列很重要

最佳答案

是的,索引中最左边的列很重要,请参阅 this :

http://dev.mysql.com/doc/refman/5.7/en/multiple-column-indexes.html 

据说:

The name index is an index over the last_name and first_name columns.The index can be used for lookups in queries that specify values in aknown range for combinations of last_name and first_name values. Itcan also be used for queries that specify just a last_name valuebecause that column is a leftmost prefix of the index (as describedlater in this section). Therefore, the name index is used for lookupsin the following queries: ...

关于mysql - 最左边的列在 MySQL 的索引中重要吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40107217/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com