一、介绍
1.什么是索引?
在一般的应用系统中,读写比在10:1左右,插入操作和一般的更新操作很少有性能问题,在生产环境中,我们遇到的最多,也是最容易出现问题的,或者一些复杂的查询操作,所以查询语句的优化显然是重中之重。说到加速查询,不得不提一下索引。
[En]
In general application systems, the read-write ratio is about 10:1, and insert operations and general update operations rarely have performance problems, in the production environment, we encounter the most, but also the most prone to problems, or some complex query operations, so the optimization of query statements is obviously the top priority. When it comes to * accelerated query * , we have to mention indexes.
2.为什么要有索引呢?
引在MySQL中也叫做”键”,是存储引擎用于快速找到记录的一种数据结构。索引对于良好的性能
这一点至关重要,尤其是随着表中的数据量越来越大,索引对性能的影响变得越来越重要。
[En]
It is critical, especially as the amount of data in the table becomes larger and larger, the impact of indexes on performance becomes more and more important.
索引优化应该是优化查询性能的最有效手段。索引可以轻松地将查询性能提高几个数量级。
[En]
Index optimization should be the most effective means to optimize query performance. Indexes can easily improve query performance by several orders of magnitude.
该索引相当于字典的顺序表。如果你想查一个词,如果你不使用顺序表,你需要从数百页中一页一页地查。
[En]
The index is equivalent to the sequence table of a dictionary. If you want to look up a word, if you do not use the sequence table, you need to look it up page by page from hundreds of pages.
3.主要优点:
索引大大减少了服务器需要扫描的数据量,大大加快了数据检索的速度,这也是创建索引的主要原因。
[En]
The index greatly reduces the amount of data that the server needs to scan, which greatly speeds up the speed of data retrieval, which is also the main reason for creating the index.
可以加速仪表和表之间的连接,特别是在实现数据的引用完整性方面。
[En]
The connection between the meter and the table can be accelerated, especially in achieving the referential integrity of the data.
使用GROUPING和SORTING子句进行数据检索时,还可以显著减少查询中的分组和排序时间。
[En]
When using grouping and sorting clauses for data retrieval, you can also significantly reduce the time of grouping and sorting in a query.
4.缺点:
创建和维护索引需要时间,该时间会随着数据量的增加而增加
[En]
It takes time to create and maintain an index, which increases as the amount of data increases
索引需要占用物理空间,除了数据表占用数据空间外,每个索引还需要占用一定的物理空间。
[En]
Indexes need to take up physical space, in addition to data tables take up data space, each index also takes up a certain amount of physical space.
当表中的数据被添加、删除或更改时,索引也需要动态维护,这会降低整数的维护速度。
[En]
When the data in the table is added, deleted or changed, the index should also be maintained dynamically, which reduces the maintenance speed of integers.
对于非常小的表,简单的全表扫描在大多数情况下效率更高
[En]
For very small tables, a simple full table scan is more efficient in most cases
二、索引的本质
索引原理:索引的目的在于提高查询效率,与我们查阅图书所用的目录是一个道理:先定位到章,然后定位到该章下的一个小节,然后找到页数。相似的例子还有:查字典,查火车车次,飞机航班等
索引本质:通过不断地缩小想要获取数据的范围来筛选出最终想要的结果,同时把随机的事件变成顺序的事件,也就是说,有了这种索引机制,我们可以总是用同一种查找方式来锁定数据。
三、MySQL的索引分类
1.普通索引 index :加速查找
2.唯一索引 与普通索引类似,不同的就是:索引列的值必须唯一,但允许有空值。如果是组合索引,则列值的组合必须是唯一的,创建方法和普通索引类似。
主键索引:primary key :加速查找+约束(不为空且唯一)
唯一索引:unique:加速查找+约束 (唯一)
3.联合索引(组合索引) 平时用的SQL查询 语句一般都有比较多的限制条件,所以为了进一步榨取MySQL的效率,就要考虑建立组合索引。
-primary key(id,name):联合主键索引
-unique(id,name):联合唯一索引
-index(id,name):联合普通索引
4.全文索引 fulltext :用于搜索很长一篇文章的时候,效果最好。
5.空间索引 spatial :了解就好,几乎不用
案例:
四、复合索引
一列的索引称为单列索引,多列的称为复合索引,因为BTREE索引是顺序排列的,所以比较适合范围查询,但是在复合索引中,还应注意列数目、列的顺序以及前面范围查询的列对后边列的影响。
Original: https://www.cnblogs.com/zzc1102/p/16029955.html
Author: and脱发周大侠
Title: MySQL基础_索引
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/505548/
转载文章受原作者版权保护。转载请注明原作者出处!