V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
MySQL 5.5 Community Server
MySQL 5.6 Community Server
Percona Configuration Wizard
XtraBackup 搭建主从复制
Great Sites on MySQL
Percona
MySQL Performance Blog
Severalnines
推荐管理工具
Sequel Pro
phpMyAdmin
推荐书目
MySQL Cookbook
MySQL 相关项目
MariaDB
Drizzle
参考文档
http://mysql-python.sourceforge.net/MySQLdb.html
zhaokun
V2EX  ›  MySQL

mysql 用到了哪些索引

  •  
  •   zhaokun · May 9, 2023 · 2672 views
    This topic created in 1088 days ago, the information mentioned may be changed or developed.

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

    13 replies    2023-05-09 11:44:45 +08:00
    cheng6563
        1
    cheng6563  
       May 9, 2023   ❤️ 1
    索引只会用一个,这种情况猜就是 index ( a,b )咯
    jtwor
        2
    jtwor  
       May 9, 2023   ❤️ 1
    索引最左匹配原则
    brader
        3
    brader  
       May 9, 2023   ❤️ 1
    @cheng6563 #1 新版的 mysql ,有 index_merge 的功能,当然不是指题主这个语句一定会发生,只在某些情况下会出现
    bootvue
        4
    bootvue  
       May 9, 2023   ❤️ 2
    一切皆有可能
    fiypig
        5
    fiypig  
       May 9, 2023   ❤️ 1
    建个 a b c 在建个 a c 是不是比较好
    RedisMasterNode
        6
    RedisMasterNode  
       May 9, 2023   ❤️ 2
    我再帮楼主细化一下问题以便于分类讨论。

    现在有个 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:
    所有索引均有使用可能
    0littleboy
        7
    0littleboy  
       May 9, 2023   ❤️ 1
    a,b
    optional
        8
    optional  
       May 9, 2023 via iPhone   ❤️ 1
    其实用哪个跟你后面还有没有 order by 关系很大,没有的话你这个例子用哪个都有可能
    optional
        9
    optional  
       May 9, 2023 via iPhone   ❤️ 1
    对了,还有 select
    4lieS
        10
    4lieS  
       May 9, 2023   ❤️ 1
    盲猜会根据 a b c 三个字段区分度决定走哪个索引
    demonps
        11
    demonps  
       May 9, 2023   ❤️ 1
    搞几张表 explain 一下各种情况:(
    oldshensheep
        12
    oldshensheep  
       May 9, 2023 via iPhone   ❤️ 1
    where a = ‘1’ and b = ‘1’ and c = ‘1’
    其中 a = ‘1’ ,b = ‘1’ ,c = ‘1’ 这几个条件是可以随意排列组合的,是等价的。所以都有可能。

    除非 MySQL 数据库太菜了。
    morty0
        13
    morty0  
       May 9, 2023   ❤️ 1
    取决于哪个索引的区分度更高, 跟存量数据和查询值有关
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2430 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 54ms · UTC 02:26 · PVG 10:26 · LAX 19:26 · JFK 22:26
    ♥ Do have faith in what you're doing.