mysql如何使用索引index提升查询效率?
https://dev.mysql.com/doc/refman/8.0/en/mysql-indexes.html
Indexes are used to find rows with specific column values quickly. Without an index, MySQL must begin with the first row and then read through the entire table to find the relevant rows. The larger the table, the more this costs. If the table has an index for the columns in question, MySQL can quickly determine the position to seek to in the middle of the data file without having to look at all the data. This is much faster than reading every row sequentially.
MySQL uses indexes for these operations:
To find the rows matching a
WHERE
clause quickly.To eliminate rows from consideration. If there is a choice between multiple indexes, MySQL normally uses the index that finds the smallest number of rows (the most selective index).
If the table has a multiple-column index, any leftmost prefix of the index can be used by the optimizer to look up rows. For example, if you have a three-column index on
(col1, col2, col3)
, you have indexed search capabilities on(col1)
,(col1, col2)
, and(col1, col2, col3)
. For more information, see Section 8.3.6, “Multiple-Column Indexes”.To retrieve rows from other tables when performing joins. MySQL can use indexes on columns more efficiently if they are declared as the same type and size. In this context,
VARCHAR
andCHAR
are considered the same if they are declared as the same size. For example,VARCHAR(10)
andCHAR(10)
are the same size, butVARCHAR(10)
andCHAR(15)
are not.For comparisons between nonbinary string columns, both columns should use the same character set. For example, comparing a
utf8
column with alatin1
column precludes use of an index.Comparison of dissimilar columns (comparing a string column to a temporal or numeric column, for example) may prevent use of indexes if values cannot be compared directly without conversion. For a given value such as
1
in the numeric column, it might compare equal to any number of values in the string column such as'1'
,' 1'
,'00001'
, or'01.e1'
. This rules out use of any indexes for the string column.To find the
MIN()
orMAX()
value for a specific indexed columnkey_col
. This is optimized by a preprocessor that checks whether you are usingWHERE
on all key parts that occur beforekey_part_N
=constant
key_col
in the index. In this case, MySQL does a single key lookup for eachMIN()
orMAX()
expression and replaces it with a constant. If all expressions are replaced with constants, the query returns at once. For example:SELECT MIN(key_part2),MAX(key_part2)
FROM tbl_name WHERE key_part1=10;To sort or group a table if the sorting or grouping is done on a leftmost prefix of a usable index (for example,
ORDER BY
). If all key parts are followed bykey_part1
,key_part2
DESC
, the key is read in reverse order. (Or, if the index is a descending index, the key is read in forward order.) See Section 8.2.1.14, “ORDER BY Optimization”, Section 8.2.1.15, “GROUP BY Optimization”, and Section 8.3.13, “Descending Indexes”.In some cases, a query can be optimized to retrieve values without consulting the data rows. (An index that provides all the necessary results for a query is called a covering index.) If a query uses from a table only columns that are included in some index, the selected values can be retrieved from the index tree for greater speed:
SELECT key_part3 FROM tbl_name
WHERE key_part1=1
Indexes are less important for queries on small tables, or big tables where report queries process most or all of the rows. When a query needs to access most of the rows, reading sequentially is faster than working through an index. Sequential reads minimize disk seeks, even if not all the rows are needed for the query. See Section 8.2.1.21, “Avoiding Full Table Scans” for details.
mysql如何使用索引index提升查询效率?的更多相关文章
- MySql采用range分区可提升查询效率
简介: RANGE分区基于一个给定的连续区间范围,早期版本RANGE主要是基于整数的分区.在5.7版本中DATE.DATETIME列也可以使用RANGE分区,同时在5.5以上的版本提供了基于非整形的R ...
- ArcGIS Engine 创建索引(属性索引)——提高查询效率
转自原文 ArcGIS Engine 创建索引(属性索引)——提高查询效率 众所周知,建立索引可以提高查询的效率,当对FeatureClass中的某一列频繁的查找,且数据量比较大时,建立索引是非常有必 ...
- mysql5.7关于使用到OR是否会用到索引并提高查询效率的探讨
相信很多人在mysql中看到了where条件中使用到了or就会以为这样是不会走索引的,通常会使用union all或者in 来进行优化,事实并不是想象的这样具体问题具体分析. 下面我们来看看 首先我们 ...
- MySQL时间设计 int timestamp datatime 查询效率性能比较
在数据库设计的时候,我们经常会需要设计时间字段,在MYSQL中,时间字段可以使用int.timestamp.datetime三种类型来存储,那么这三种类型哪一种用来存储时间性能比较高,效率好呢?飘易就 ...
- mysql关于char和varchar的查询效率问题
看了好多资料都说 varchar(size) 可变长度的字符值,节省空间,查询效率低 char(size) 固定长度的字符值,浪费空间,查询效率高 但是实际测试 char(100) varcha ...
- mysql中存储字段类型的查询效率
检索性能从快到慢的是(此处是听人说的): 第一:tinyint,smallint,mediumint,int,bigint第二:char,varchar第三:NULL 解释(转载): 整数类型1.TI ...
- mysql update join优化update in查询效率
数据库版本:5.6.16 update in 修改数据,结果执行时间过慢,一直不出结果. SQL语句及执行计划如下: UPDATE erp_order_extra SET last_time=1231 ...
- sql 提升查询效率 group by option hash group
问题: 一个程序查询经常超过20siis限制时间,排查问题后发现其中的一个存储过程时间会在15s左右 解决思路: 1:确认问题点 通过输出时间的方式查看存储过程中每个部分的执行时间,找到最耗时的三个过 ...
- mysql高级、索引
一.mysql高级 1.视图 # 引子 select * from emp left join dep on emp.dep_id = dep.id union select * from emp r ...
随机推荐
- Vue slot简单理解
情形一: 子组件定义了具名的slot,父组件使用具名的slot,slot显示顺序为子组件定义slot的顺序 子组件: Vue.component('child',{ template:`<div ...
- 如何在eclipse中使用mvn clean install
1.在Maven项目或者pom.xml上右键——>Run As ——>“Maven Build...”或者Run Configuration——>“Maven Build” 2.在“ ...
- ADCD 1.9 ZOS 配置 CTCI-W32 TCPIP 网络
试验步骤:两步走,第一步修改Hercules的配置文件 在hercules 配置文件末尾加上 0E20-0E21 CTCI -n 0A-00-27-00-00-00 192.168.5 ...
- HNOI2015题解
奇了怪了我上次发的题解怎么不见了? 题意自己戳链接-- Day 1 id=4008">HNOI2015 Arthur 思路:期望DP 直接DP是死也D不出的 转化一下 令f[i][j] ...
- Git Step by Step – (3) Git对象模型
前面一篇文章介绍了本地仓库的一系列操作,下面我们将进一步了解Git的工作原理,介绍Git对象模型. 刚开始使用Git的时候,对Git对象模型.工作原理并不理解,但是经过一段时间的使用.熟悉之后,然后再 ...
- ios开发之--条用第三方地图路线导航
项目里面有位置功能,需要有导航,导航两种实现方式 (集成第三方SDK.URL跳转第三方应用) ,直接集成就不说,下面来说下通过url跳转, 最终效果如如下: 如果手机上安装的有客户端就展示,没有就不展 ...
- 雪花算法-snowflake
雪花算法-snowflake 分布式系统中,有一些需要使用全局唯一ID的场景,这种时候为了防止ID冲突可以使用36位的UUID,但是UUID有一些缺点,首先他相对比较长,另外UUID一般是无序的. 有 ...
- 基于github+hexo搭建个人博客(window)
0x01 环境搭建 1.Node.js环境 下载Node.js安装文件:https://nodejs.org/en/download/ 根据系统选择相应安装包下载,安装过程一路Next,默认设置即可. ...
- Python模块常用的几种安装方式 【转】
转自:http://blog.chinaunix.net/uid-23500957-id-3781907.html 一. 单文件模块直接把文件拷贝到 $python_dir/Lib 二. 多文件模块, ...
- 使用 requests 维持会话
什么是 Cookie 和 Session: 简单来说,我们访问每一个互联网页面,都是通过 HTTP 协议进行的,而 HTTP 协议是一个无状态协议,所谓的无状态协议即无法维持会话之间的状态.比如,仅使 ...