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
xmbaozi
V2EX  ›  MySQL

Mysql 关于复合索引的顺序。a 在前?b 在前?

  •  
  •   xmbaozi · May 27, 2014 · 7526 views
    This topic created in 4396 days ago, the information mentioned may be changed or developed.
    有个表5000w的记录,要给a、b列建唯一索引。其中a为int,大概分布在1-1500左右;b为varchar(32),8-32个字符,b中出现重复的极低。
    现在考虑到where a=x1 and b=x2的情况,这个索引顺序怎么建比较科学,然后理由?

    已经google过了,如
    http://blog.segmentfault.com/waterblog/1190000000473085
    5.联合索引的顺序问题:将选择性高的索引放到前面


    众神什么看?
    4 replies    2014-05-28 12:19:42 +08:00
    duzhe0
        1
    duzhe0  
       May 27, 2014   ❤️ 1
    测一下实际效果看看。估计差别不大, 没必要那么纠结。我猜b-a可能好一点
    tonitech
        2
    tonitech  
       May 28, 2014   ❤️ 1
    楼主做个测试再给个答案吧
    cyyever
        3
    cyyever  
       May 28, 2014   ❤️ 1
    根据选择率是a-b,但是有个问题,这样做索引会比b-a大,因为a-b中a出现了1500次,b也出现了1500次,如果是key buffer比较稀缺的话,还不如b-a,毕竟查内存索引总比查磁盘索引快吧
    我建议楼主两个索引都创建一下,然后用show table status看下index大小。然后结合服务器资源综合考虑
    我的看法是内存再慢也比io快,redis就是省内存,比memcached现在发展得好。。虽然它单个操作可能比memcached慢。。
    tonic
        4
    tonic  
       May 28, 2014   ❤️ 1
    (b, a) use where b= and a=
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3162 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 53ms · UTC 14:23 · PVG 22:23 · LAX 07:23 · JFK 10:23
    ♥ Do have faith in what you're doing.