3

mysql 用到了哪些索引

 11 months ago
source link: https://www.v2ex.com/t/938507
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

V2EX  ›  MySQL

mysql 用到了哪些索引

  zhaokun · 1 小时 51 分钟前 · 469 次点击

现在有个 mysql 索引问题,表有三个字端:a ,b ,c 表中有三个索引 index ( a,b ), index(b,c),index ( a,c ) 现有查询 where a = ‘1’ and b = ‘1’ and c = ‘1’ 请问哪些字端用到了索引,用到了哪些索引?为什么? 请教大佬解惑

13 条回复    2023-05-09 11:44:45 +08:00

cheng6563      1 小时 49 分钟前

索引只会用一个,这种情况猜就是 index ( a,b )咯

jtwor      1 小时 48 分钟前

索引最左匹配原则

brader      1 小时 42 分钟前

@cheng6563 #1 新版的 mysql ,有 index_merge 的功能,当然不是指题主这个语句一定会发生,只在某些情况下会出现

bootvue      1 小时 42 分钟前   1

一切皆有可能

fiypig      1 小时 40 分钟前

建个 a b c 在建个 a c 是不是比较好

RedisMasterNode      1 小时 32 分钟前

我再帮楼主细化一下问题以便于分类讨论。

现在有个 mysql 索引问题,表有三个字段:a ,b ,c ,共计 1000w 行数据。表中有三个索引 index(a,b), index(b,c),index(a,c)。现有查询 where a = 1 and b = 1 and c = 1 。

场景 1:
满足 a=1 and b=1 的数据有 999w 行,满足 b=1 and c=1 的数据有 999w 行,满足 a=1 and c=1 的数据有 100 行。

场景 2:
满足 a=1 and b=1 的数据有 999w 行,满足 b=1 and c=1 的数据有 999w 行,满足 a=1 and c=1 的数据有 999w 行。

场景 3:
满足 a=1 and b=1 的数据有 1 行,满足 b=1 and c=1 的数据有 1 行,满足 a=1 and c=1 的数据有 1 行。

场景 4:
满足 a=1 and b=1 的数据有 1000 行,满足 b=1 and c=1 的数据有 50 行,满足 a=1 and c=1 的数据有 10 行。

请问哪些字端用到了索引,用到了哪些索引?为什么? 请教大佬解惑

--------------------------------------
我认为的答案:
场景 1:
极大概率使用 index(a,c)

场景 2:
执行全表扫描

场景 3:
所有索引均有使用可能

场景 4:
所有索引均有使用可能


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK