`
tmrp
  • 浏览: 44758 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

3 ways MySQL uses indexes

阅读更多
I often see people confuse different ways MySQL can use indexing, getting wrong ideas on what query performance they should expect. There are 3 main ways how MySQL can use the indexes for query execution, which are not mutually exclusive, in fact some queries will use indexes for all 3 purposes listed here.

Using index to find rows The main purpose of the index is to find rows quickly – without scanning whole data set. This is most typical reason index gets added on the first place. Most popular index type in MySQL – BTREE can speed up equality and prefix range matches. So if you have index on (A,B) This index can be used to lookup rows for WHERE clauses like A=5 ; A BETWEEN 5 AND 10 ; A=5 AND B BETWEEN 5 AND 10 it however will NOT be able to help lookup rows for B BETWEEN 5 AND 10 predicate because it is not index prefix. It is important to look at key_len column in explain plan to see how many index parts are actually used for row lookup. Very common problem I see is multi column indexes which are used but only to their short prefix which is not very selective. A lot of this mistakes come from missing one very important MySQL limitation – once MySQL runs into the interval range it will not use any further index parts. If you have A BETWEEN 5 AND 10 AND B=5 for the same index MySQL will use the index… but it will only use A prefix for row lookups and scan whole A BETWEEN 5 AND 10 range. It is interesting to note this limitation only applies to interval ranges – for enumerated ranges MySQL will use both key parts. Hence if you change this predicate to A IN (5,6,7,8,9,10) AND B=5 you will quite likely see improved query performance. Beware however of large nested enumerated ranges they are very hard on the optimizer. This just describes how MySQL uses single index – there are more complex rules of how indexes will be used if you look at multiple indexes usage with “index merge”

Using Index to Sort Data Another great benefit of BTREE index is – it allows to retrieve data in sorted form hence avoiding external sort process for executing of queries which require sorting. Using index for sorting often comes together with using index to find rows, however it can also be used just for sort for example if you’re just using ORDER BY without and where clauses on the table. In such case you would see “Index” type in explain which correspond to scanning (potentially) complete table in the index order. It is very important to understand in which conditions index can be used to sort data together with restricting amount of rows. Looking at the same index (A,B) things like ORDER BY A ; ORDER BY A,B ; ORDER BY A DESC, B DESC will be able to use full index for sorting (note MySQL may not select to use index for sort if you sort full table without a limit). However ORDER BY B or ORDER BY A, B DESC will not be able to use index because requested order does not line up with the order of data in BTREE. If you have both restriction and sorting things like this would work A=5 ORDER BY B ; A=5 ORDER BY B DESC; A>5 ORDER BY A ; A>5 ORDER BY A,B ; A>5 ORDER BY A DESC which again can be easily visualized as scanning a range in BTREE. Things like this however would not work A>5 ORDER BY B , A>5 ORDER BY A,B DESC or A IN (3,4) ORDER BY B – in these cases getting data in sorting form would require a bit more than simple range scan in the BTREE and MySQL decides to pass it on. There are some workarounds you can use though.

Using index to read data Some storage engines (MyISAM and Innodb included) can also use index to read the data, hence avoiding to read the row data itself. This is not simply savings of having 2 reads per index entry instead of one but it can save IO orders of magnitude in some cases – Indexes are sorted (at least on the page boundary) so doing index range scan you typically get many index entries from the same page but the rows itself can be scattered across many pages requiring potentially a lot of IOs. On top of that if you just need access to couple of columns
index can be simply much smaller than the data which is one of the reason covering indexes help to speed up queries even if data is in memory. If MySQL is only reading index and not accessing rows you will see “using index” in EXPLAIN output.

These are the main “core” use for indexes. You can also see others like using index for group by but I think they can be pretty much looked as one of these 3 ways described.
http://www.mysqlperformanceblog.com/2009/09/12/3-ways-mysql-uses-indexes/
分享到:
评论

相关推荐

    8.3 Optimization and Indexes

    MySQL数据库系统的性能优化是数据库管理中的一个重要方面,而索引是影响查询性能的关键因素之一。本部分主要介绍MySQL中的索引如何使用,以及如何通过索引来提升查询操作的效率。 首先,索引是数据库中用于快速定位...

    MySQL 8 Cookbook epub 格式

    Maximize performance by using new features of MySQL 8 like descending indexes, controlling query optimizer and resource groups Learn how to use general table space to suit the SaaS or multi-tenant ...

    mysql官方绿色安装包32位 mysql-5.7.36-win32

    3. **JSON支持**:MySQL 5.7引入了对JSON数据类型的原生支持,使得NoSQL和SQL数据库之间的交互变得更加便捷。 4. **柱状图索引(Columnstore Indexes)**:对于数据分析和报告任务,MySQL 5.7引入了InnoDB Column...

    MySQL V5.5帮助文档

    1.6.2. MySQL Community Support at the MySQL Forums 1.6.3. MySQL Community Support on Internet Relay Chat (IRC) 1.6.4. MySQL Enterprise 1.7. How to Report Bugs or Problems 1.8. MySQL Standards ...

    mysql语法手册-mysql语法手册-mysql语法手册-mysql语法手册

    手册会讲解如何创建(CREATE INDEX)、查看(SHOW INDEXES)和删除(DROP INDEX)索引,以及不同类型的索引,如唯一索引、主键索引和全文索引。 五、视图 视图是虚拟表,由SELECT语句定义。手册会涵盖创建(CREATE ...

    High Performance MySQL_3rd_edition

    With High Performance MySQL, you’ll learn advanced techniques for everything from designing schemas, indexes, and queries to tuning your MySQL server, operating system, and hardware to their fullest...

    High Performance MySQL: Optimization, Backups, Replication, and More

    Learn how to design schemas, indexes, queries and advanced MySQL features for maximum performance, and get detailed guidance for tuning your MySQL server, operating system, and hardware to their ...

    mysql 学习用例(console程序)

    3. **数据库操作**: - 创建数据库:`CREATE DATABASE 数据库名;` - 查看数据库:`SHOW DATABASES;` - 选择数据库:`USE 数据库名;` - 删除数据库:`DROP DATABASE 数据库名;` 4. **表操作**: - 创建表:`...

    Devart dbForge Studio for MySQL Professional Edition v7.1.13

    Visual designer for modifying or creating table structure and indexes. Get full control over the changes you have made, the heuristics feature will suggest the column type on creating a column basing ...

    mysql-installer-community-8.0.30.0.msi

    2020年1月13日,MySQL官方...在JSON函数中使用多值索引(JSON functions using multi-valued indexes) MySQL shell (管理MGR的MySQL客户端 ) Innodb Cluster (来自官方的MGR产品解决方案) MGR 在复制上的提升

    MYSQL数据库信息查看

    3. **连接MySQL服务器**: 使用MySQL客户端,我们首先需要连接到MySQL服务器。这通常涉及提供服务器地址(localhost或IP地址)、用户名、密码以及要连接的数据库名。 4. **数据库信息查看**: - **`SHOW ...

    MySQL 8 新特性之Invisible Indexes

    背景 索引是把双刃剑,在提升查询速度的同时会减慢DML的操作。毕竟,索引的维护需要一定的成本。所以,对于索引,要加上该加的,删除无用的。...mysql> show create table sys.schema_unused_indexes\G *****

    mysql5.7.19 数据库安装包

    - **Column Store Indexes**:对于分析查询,增加了对ColumnStore索引的支持,提高了数据聚合速度。 4. **安全特性**: - **加强的默认安全设置**:5.7版本默认禁用了弱密码,并强制执行更严格的加密标准。 - **...

    mysql-8.0.21-winx64.zip.zip

    3. **JSON支持**:MySQL 8.0增强了对JSON数据类型的支持,允许存储和查询半结构化数据,适合混合型数据库需求。 4. **动态柱状图索引**:引入了Dynamic Columnar Indexes(DCI),对大数据量的列式存储进行优化,...

    linux下mysql的操作与存储过程

    3. 索引(Indexes):提升查询性能,使用`CREATE INDEX`创建。 4. 存储函数(Stored Functions):与存储过程类似,但不返回多条结果,而是单个值。 5. 权限管理:使用`GRANT`和`REVOKE`命令管理用户权限。 在...

    MySQL从入门到精通PPT大全(13份).rar

    索引(Indexes)的使用可以显著提高查询速度,但需要合理设计以避免负面影响。视图(Views)可以提供虚拟表,简化复杂查询,并为用户提供定制的数据视图。 此外,还有事务处理(Transactions)的概念,这对于确保...

    Mysql Mysql学习资料

    3. **MySQL性能优化** - 查询优化:分析查询执行计划,减少JOIN操作,优化WHERE子句,避免全表扫描。 - 数据库设计优化:合理设计数据库模式,减少冗余,提高数据一致性。 - 表分区(Partitioning):通过将大表...

    MySQL教程(命令提示符形式)

    3. 退出MySQL客户端:输入`exit`或`\q`。 三、数据库操作 1. 创建数据库:使用`CREATE DATABASE 数据库名;`命令。 2. 选择数据库:`USE 数据库名;` 3. 查看所有数据库:`SHOW DATABASES;` 4. 删除数据库:`DROP ...

    mysql5.7.22解压版

    3. **Performance Schema**:MySQL 5.7 对 Performance Schema 进行了改进,提供了更全面的性能监控和分析工具,帮助管理员识别系统瓶颈并优化数据库性能。 4. **SQL 查询优化器改进**:优化器引入了 Cost-Based ...

Global site tag (gtag.js) - Google Analytics