zoukankan      html  css  js  c++  java
  • MySQL索引最左匹配原则

    原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/11609103.html

    创建一个测试表

     1 CREATE TABLE `test` (
     2   `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
     3   `a` varchar(32) NOT NULL,
     4   `b` varchar(32) NOT NULL,
     5   `c` varchar(64) NOT NULL,
     6   `d` varchar(128) NOT NULL,
     7   `e` varchar(256) NOT NULL,
     8   `create_time` timestamp(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),  
     9   `update_time` timestamp(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3), 
    10   PRIMARY KEY (`id`)
    11 ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

    插入数据

     1 INSERT INTO `test` (`a`, `b`, `c`, `d`, `e`) VALUES('a', 'b', 'c', 'd', 'e');
     2 INSERT INTO `test` (`a`, `b`, `c`, `d`, `e`) VALUES('a', 'b', 'c', 'd', 'e');
     3 INSERT INTO `test` (`a`, `b`, `c`, `d`, `e`) VALUES('a', 'b', 'c', 'd', 'e');
     4 INSERT INTO `test` (`a`, `b`, `c`, `d`, `e`) VALUES('a', 'b', 'c', 'd', 'e');
     5 INSERT INTO `test` (`a`, `b`, `c`, `d`, `e`) VALUES('a', 'b', 'c', 'd', 'e');
     6 INSERT INTO `test` (`a`, `b`, `c`, `d`, `e`) VALUES('a', 'b', 'c', 'd', 'e');
     7 INSERT INTO `test` (`a`, `b`, `c`, `d`, `e`) VALUES('a', 'b', 'c', 'd', 'e');
     8 INSERT INTO `test` (`a`, `b`, `c`, `d`, `e`) VALUES('a', 'b', 'c', 'd', 'e');
     9 INSERT INTO `test` (`a`, `b`, `c`, `d`, `e`) VALUES('a', 'b', 'c', 'd', 'e');
    10 INSERT INTO `test` (`a`, `b`, `c`, `d`, `e`) VALUES('a', 'b', 'c', 'd', 'e');

    查看索引

    1 show index from test;

    查看执行计划

    1 explain select * from test where id = "1";

    1 explain select * from test where a = "1";

    1 explain select * from test where b = "1";

    1 explain select * from test where a = "1" and b = "1";

    创建索引(a和b的联合索引)

    1 create index idx_a_b on test(a, b);

    查看索引

    1 show index from test;

    查看执行计划

    1 explain select * from test where a = "1";

    1 explain select * from test where b = "1";

    1 explain select * from test where a = "1" and b = "1";

    创建索引(b的索引)

    1 create index idx_b on test(b);

    查看索引

    1 show index from test;

    查看执行计划

    1 explain select * from test where a = "1";

    1 explain select * from test where b = "1";

    1 explain select * from test where a = "1" and b = "1";

    删除索引

    1 drop index idx_a_b on test;
    2 drop index idx_b on test;

    删除索引后,创建a, b, c 3个字段的联合索引

    1 create index idx_a_b_c on test(a, b, c);

    查看如下语句的执行计划

     1 -- ref
     2 explain select * from test where a = "1";
     3 -- all 扫全表
     4 explain select * from test where b = "1";
     5 -- all 扫全表
     6 explain select * from test where c = "1";
     7 -- ref
     8 explain select * from test where a = "1" and b = "1" and c = "1";
     9 -- ref
    10 explain select * from test where a = "1" and b = "1";
    11 -- ref
    12 explain select * from test where a = "1" and c = "1"
    13 -- all 扫全表
    14 explain select * from test where b = "1" and c = "1"



      

  • 相关阅读:
    simhash算法:海量千万级的数据去重
    卸载pycharm再重新安装后,找不到第三方库
    一个完整的jmeter APP登录接口测试实例
    pycharm主题 变量颜色 自定义
    基于python xlsxwriter、xlrd 生成测试报告
    通过python xlsxwriter模块生成EXCEL柱状图、饼图
    jenkins环境搭建(Windows)
    'pip' 不是内部或外部命令,也不是可运行的程序 或批处理文件 — 处理办法
    python 模块学习——time模块
    Appium 使用android_uiautomator定位元素时报错: The requested resource could not be found, or a request was received using an HTTP method that is not supported by the mapped resource
  • 原文地址:https://www.cnblogs.com/agilestyle/p/11609103.html
Copyright © 2011-2022 走看看