zoukankan      html  css  js  c++  java
  • mysql的索引下推理解和实践

    对于mysql建表稍有点经验的开发人员都会为后续的where查询条件提前考虑创建索引。

    这里说的是在使用索引查询时有关索引下推的有关知识点。

    综合前人的经验结果:索引下推是数据库检索数据过程中为减少回表次数而做的优化。

    判断是否需要回表的是由mysql存储引擎控制,默认从mysql5.6版本开始支持。

    下面用docker分别创建基于mysql5.5和mysql5.6的容器,表结构保持一致(docker创建mysql容器不做演示)。

    首先看mysql5.5:

    mysql> select version();
    +-----------+
    | version() |
    +-----------+
    | 5.5.62    |
    +-----------+
    1 row in set (0.00 sec)
    
    mysql> show create table testhhG;
    *************************** 1. row ***************************
           Table: testhh
    Create Table: CREATE TABLE `testhh` (
      `id` int(10) unsigned NOT NULL,
      `age` int(10) unsigned DEFAULT '0',
      `name` char(10) NOT NULL DEFAULT '',
      `height` int(10) NOT NULL DEFAULT '0',
      `name2` char(10) NOT NULL DEFAULT '',
      `height2` int(10) NOT NULL DEFAULT '0',
      PRIMARY KEY (`id`),
      KEY `age_index` (`age`) USING HASH,
      KEY `un` (`name`,`height`)
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1
    1 row in set (0.00 sec)
    
    ERROR:
    No query specified
    
    mysql> explain select * from testhh where name like 'a%' and height = 100G;
    *************************** 1. row ***************************
               id: 1
      select_type: SIMPLE
            table: testhh
             type: range
    possible_keys: un
              key: un
          key_len: 14
              ref: NULL
             rows: 1
            Extra: Using where
    1 row in set (0.00 sec)
    
    ERROR:
    No query specified

    上面可见explain的extra字段结果时Using where,表示优化器需要通过索引回表查询数据。

    再看mysql5.6:

    mysql> select version();
    +-----------+
    | version() |
    +-----------+
    | 5.6.50    |
    +-----------+
    1 row in set (0.00 sec)
    
    mysql> show create table uaG;
    *************************** 1. row ***************************
           Table: ua
    Create Table: CREATE TABLE `ua` (
      `id` int(10) NOT NULL AUTO_INCREMENT,
      `name` char(10) NOT NULL DEFAULT '',
      `height` int(10) NOT NULL DEFAULT '0',
      `name2` char(10) NOT NULL DEFAULT '',
      `height2` int(10) NOT NULL DEFAULT '0',
      PRIMARY KEY (`id`),
      KEY `nh` (`name`,`height`)
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1
    1 row in set (0.00 sec)
    
    ERROR: 
    No query specified
    
    mysql> explain select * from ua where name like 'a%' and height=10G;
    *************************** 1. row ***************************
               id: 1
      select_type: SIMPLE
            table: ua
             type: range
    possible_keys: nh
              key: nh
          key_len: 14
              ref: NULL
             rows: 1
            Extra: Using index condition
    1 row in set (0.00 sec)
    
    ERROR: 
    No query specified

    explain的extra字段是Using index condition,表示会先通过条件过滤索引,再通过过滤后的索引查询符合索引条件的数据。

  • 相关阅读:
    unity的#pragma strict,#pragma downcast等指令分享
    Unity3d 添加多相机后编译警告
    Invoke计时器
    unity3d UI自动适合屏幕分辨率
    实现卷轴效果的脚本
    .unity3d格式的导出与加载
    Linux 网络编程
    姿态解算基本完成,程序编写笔记
    验证网络上四元数的正确性
    2440 模拟IIC 可以读取 L3G4200D ,ADXL345
  • 原文地址:https://www.cnblogs.com/wscsq789/p/14162885.html
Copyright © 2011-2022 走看看