8.2.1.4 Index Merge Optimization 索引合并优化:

索引合并方法是用于检索记录 使用多个 范围扫描和合并它们的结果集到一起

mysql> show index from ClientInvestOrder;
+-------------------+------------+------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------------------+------------+------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| ClientInvestOrder | 0 | PRIMARY | 1 | sn | A | 11466 | NULL | NULL | | BTREE | | |
| ClientInvestOrder | 0 | orderNo | 1 | orderNo | A | 11466 | NULL | NULL | | BTREE | | |
| ClientInvestOrder | 1 | ClientInvestOrder_idx1 | 1 | clientSn | A | 1263 | NULL | NULL | | BTREE | | |
+-------------------+------------+------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
3 rows in set (0.00 sec) mysql> explain select * from ClientInvestOrder where clientSn=12804;
+----+-------------+-------------------+------------+------+------------------------+------------------------+---------+-------+------+----------+-------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------------------+------------+------+------------------------+------------------------+---------+-------+------+----------+-------+
| 1 | SIMPLE | ClientInvestOrder | NULL | ref | ClientInvestOrder_idx1 | ClientInvestOrder_idx1 | 4 | const | 7 | 100.00 | NULL |
+----+-------------+-------------------+------------+------+------------------------+------------------------+---------+-------+------+----------+-------+
1 row in set, 1 warning (0.00 sec) mysql> explain select * from ClientInvestOrder where productSn=747 and clientSn=12804;
+----+-------------+-------------------+------------+------+------------------------+------------------------+---------+-------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------------------+------------+------+------------------------+------------------------+---------+-------+------+----------+-------------+
| 1 | SIMPLE | ClientInvestOrder | NULL | ref | ClientInvestOrder_idx1 | ClientInvestOrder_idx1 | 4 | const | 7 | 10.00 | Using where |
+----+-------------+-------------------+------------+------+------------------------+------------------------+---------+-------+------+----------+-------------+
1 row in set, 1 warning (0.00 sec) mysql> create index ClientInvestOrder_idx2 on ClientInvestOrder(productSn);
Query OK, 0 rows affected (0.12 sec)
Records: 0 Duplicates: 0 Warnings: 0 mysql> explain select * from ClientInvestOrder where productSn=747 and clientSn=12804;
+----+-------------+-------------------+------------+-------------+-----------------------------------------------+-----------------------------------------------+---------+------+------+----------+-----------------------------------------------------------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------------------+------------+-------------+-----------------------------------------------+-----------------------------------------------+---------+------+------+----------+-----------------------------------------------------------------------------+
| 1 | SIMPLE | ClientInvestOrder | NULL | index_merge | ClientInvestOrder_idx1,ClientInvestOrder_idx2 | ClientInvestOrder_idx1,ClientInvestOrder_idx2 | 4,4 | NULL | 1 | 100.00 | Using intersect(ClientInvestOrder_idx1,ClientInvestOrder_idx2); Using where |
+----+-------------+-------------------+------------+-------------+-----------------------------------------------+-----------------------------------------------+---------+------+------+----------+-----------------------------------------------------------------------------+
1 row in set, 1 warning (0.00 sec) 合并可以产生unions,intersections 或者unions-of-intersections 。 这个访问方式合并索引扫描从一个单独的表,它不会合并扫描跨越多个表 在EXPALIN 输出, Index Merge 方法出现作为index_merge 在类型列, 在这种情况下, key 列 包含了使用的索引的列, key_len 包含那些索引的最长索引部分的列表
Examples: SELECT * FROM tbl_name WHERE key1 = 10 OR key2 = 20; SELECT * FROM tbl_name
WHERE (key1 = 10 OR key2 = 20) AND non_key=30; SELECT * FROM t1, t2
WHERE (t1.key1 IN (1,2) OR t1.key2 LIKE 'value%')
AND t2.key1=t1.some_col; SELECT * FROM t1, t2
WHERE t1.key1=1
AND (t2.key1=t1.some_col OR t2.key2=t1.some_col2); Index 合并方法有几个访问算法(查看EXPLAIN 额外的字段) Using intersect(...) Using union(...) Using sort_union(...) 下面的章节将描述那些方法: 注意: Index 合并优化算法有下面已知的缺陷: 1.如果你的查询是一个复杂的WHERE 子句有嵌套的AND/OR MYSQL 不知道选择合适的执行计划,尝试分散表达式使用下面的规律: (x AND y) OR z = (x OR z) AND (y OR z) (x OR y) AND z = (x AND z) OR (y AND z) 索引合并不适用于全文索引,我们计划在将来的版本中实现 在MySQL 5.6.6之前,如果一个range scan是可能的在一些索引上, 优化器不会考虑使用Index 合并 union 或者Index合并Sort-Union算法,比如,考虑下面的查询: SELECT * FROM t1 WHERE (goodkey1 < 10 OR goodkey2 < 20) AND badkey < 30; 对于这个查询,两个计划是可能的: 1.index 合并scan 使用 (goodkey1 < 10 OR goodkey2 < 20) condition. 2.一个range scan 使用badkey < 30 condition. 然而, 优化器只考虑第2个计划 选择在不同的可能索引合并访问方法的变体 在不同的可能的Index Merge 访问方法和其他访问方法的区别是基于成各种变量选项的成本评估 8.2.1.4 索引合并交叉访问算法: 这个访问算法可以被利用当一个WHERE 子句被转换成多个范围条件在不同的keys 使用AND连接, 每个条件是下面中的一个: 在这种形式下, index有N部分(有就是说,所有的index部分是被覆盖的) key_part1=const1 AND key_part2=const2 ... AND key_partN=constN 任何范围条件覆盖一个InnoDB表的主键 例子: SELECT * FROM innodb_table WHERE primary_key < 10 AND key_col1=20; SELECT * FROM tbl_name
WHERE (key1_part1=1 AND key1_part2=2) AND key2=2; Index 合并交叉算法执行并发的扫描在所有被使用的索引和 产生记录顺序的交集 它从合并的Index 扫描接收 如果所有的列 用于在查询是通过使用的索引覆盖,full table 记录 不是被检索的 (EXPLAIN 输出包含使用的索引在额外字段)这里有一个查询的例子: SELECT COUNT(*) FROM t1 WHERE key1=1 AND key2=1; 如果 被使用的索引不覆盖所有的列在查询里,全部的记录是被接收只有当范围条件对于那些使用的索引被满足 如果其中一个合并条件是一个条件覆盖了一个InnoDB表的主键, 它不用于行检索,但是用于过滤检索的记录用于其他条件 8.2.1.4.2 The Index Merge Union Access Algorithm 索引合并联合访问算法

8.2.1.4 Index Merge Optimization 索引合并优化:的更多相关文章

  1. MySQL Index Merge Optimization

    Index Merge用在通过一些range scans得到检索数据行和合并成一个整体.合并可以通过 unions,intersections,或者unions-intersection运用在底层的扫 ...

  2. MySQL 优化之 index merge(索引合并)

    深入理解 index merge 是使用索引进行优化的重要基础之一.理解了 index merge 技术,我们才知道应该如何在表上建立索引. 1. 为什么会有index merge 我们的 where ...

  3. index merge的一次优化

    手机微博4040端口SQL优化 现象 某端口常态化延迟,通过使用pt-query-digest发现主要由于一条count(*)语句引发,具体如下: # .5s .58M rss, .84M vsz # ...

  4. MySQL 查询优化之 Index Merge

    MySQL 查询优化之 Index Merge Index Merge Intersection 访问算法 Index Merge Union 访问算法 Index Merge Sort-Union ...

  5. MySQL中Index Merge简介

    索引合并优化 官网翻译 MySQL5.7文档 索引合并是为了减少几个范围(type中的range类型:range can be used when a key column is compared t ...

  6. MySQL 优化之 index_merge (索引合并)

    深入理解 index merge 是使用索引进行优化的重要基础之一.理解了 index merge 技术,我们才知道应该如何在表上建立索引. 1. 为什么会有index merge 我们的 where ...

  7. mysql索引及优化

    索引; 2.索引入门对于任何DBMS,索引都是进行优化的最主要的因素.对于少量的数据,没有合适的索引影响不是很大,但是,当随着数据量的增加,性能会急剧下降.如果对多列进行索引(组合索引),列的顺序非常 ...

  8. MySql 索引 查询 优化

    官方文档: https://dev.mysql.com/doc/refman/5.7/en/explain-output.html#explain_rows type: 连接类型 system 表只有 ...

  9. MySQL 索引的优化

    一.MySQL如何使用索引(index) 1.1 索引概述 索引用于快速查找具有特定列值的行. 如果不使用索引,MySQL必须从表的第一行开始,然后扫描整个表来寻找符合条件的行.这种情况下,表越大,扫 ...

随机推荐

  1. mysql数据库sql常用命令

    1.查看索引:mysql> show index from tblname; 2.利用索引查询:SELECT * FROM product WHERE ID > =(select id f ...

  2. jenkins服务器安装

    http://www.360doc.com/content/13/0412/09/10504424_277718090.shtml

  3. 8000401a错误解决方式(Excel)

    前一阵子做开发须要用到Excel和Word编程,本人用的是Vista系统,开发环境是VS2005和Office2007,測试无不论什么问题,但是到部署的时候出现了一些令人非常头痛的问题,老是会出现比如 ...

  4. [Javascript] Advanced Function Scope

    Something like 'for' or 'while', 'if', they don't create a new scope: ,,]; ; i < ary.length; i++) ...

  5. CentOS 6.6下PXE+Kickstart无人值守安装操作系统

    一.简介 1.1 什么是PXE PXE(Pre-boot Execution Environment,预启动执行环境)是由Intel公司开发的最新技术,工作于Client/Server的网络模式,支持 ...

  6. MegaCLI SAS RAID Management Tool

    MegaCLI SAS RAID Management Tool  Ver 8.04.08 July 05, 2012    (c)Copyright 2011, LSI Corporation, A ...

  7. 【翻译】A (very) short introduction to R R的简短介绍

    [前言] 本文翻译自Paul Torfs & Claudia Brauer的文章A (very) short introduction to R.其中比较简单的地方没有翻译,不好用中文描述的地 ...

  8. nginx 站点80跳443配置

    server { listen 80; server_name www.furhacker.cn; location /{# return 301; rewrite ^(.*)$ https://$h ...

  9. block为什么要用copy,runtime的简单使用

    分享一篇文章:link

  10. 选课 树形dp+路径输出

    #include<iostream> #include<cstdio> #include<cstring> #define maxn 2010 using name ...