首页 > 数据库 >mysql 索引知识梳理

mysql 索引知识梳理

时间:2023-02-28 23:02:34浏览次数:59  
标签:index column col2 col1 索引 mysql 梳理 columns

mysql 索引知识梳理

如果说数据是字典的话,索引就是字典的目录。mysql常用的索引有两种 B-Tree(实际上是B+Tree),和 Hash,默认是前者(实际上InnoDB只支持BTree)

哪些列适合创建索引

哪些列不适合创建索引

主键索引

It has an associated index, for fast query performance. Query performance benefits from the NOT NULL optimization, because it cannot include any NULL values. With the InnoDB storage engine, the table data is physically organized to do ultra-fast lookups and sorts based on the primary key column or columns.

主键有唯一

外键优化

大表拆成多张单表,然后通过外键关联起来,这样可以减少查询的数据量,提高查询效率。

If a table has many columns, and you query many different combinations of columns, it might be efficient to split the less-frequently used data into separate tables with a few columns each, and relate them back to the main table by duplicating the numeric ID column from the main table.

底层原理:每次尽可能加载少量的数据块,减少磁盘IO,占用更少的缓存内存

the queries might perform less I/O and take up less cache memory because the relevant columns are packed together on disk. (To maximize performance, queries try to read as few data blocks as possible from disk; tables with only a few columns can fit more rows in each data block.)

多列索引

composite indexes 或者称为 组合索引

Multiple-Column Indexes

1. 基础概念

  1. sorted array

  2. leftmost prefix

  3. Index Merge Optimization

  4. alternative

As an alternative to a composite index, you can introduce a column that is “hashed” based on information from other columns. If this column is short, reasonably unique, and indexed, it might be faster than a “wide” index on many columns. In MySQL, it is very easy to use this extra column:

SELECT * FROM tbl_name
WHERE hash_col=MD5(CONCAT(val1,val2))
AND col1=val1 AND col2=val2;

2. 官网摘要

  1. A multiple-column index can be considered a sorted array, the rows of which contain values that are created by concatenating the values of the indexed columns.

  2. The first column in the index is the most significant column, the second column is the next most significant column, and so on.

  3. If a multiple-column index exists on col1 and col2, the appropriate rows can be fetched directly. If separate single-column indexes exist on col1 and col2, the optimizer attempts to use the Index Merge optimization (see Section 8.2.1.3, “Index Merge Optimization”), or attempts to find the most restrictive index by deciding which index excludes more rows and using that index to fetch the rows.

    单列索引,sql优化器会尝试使用Index Merge优化,或者尝试找到最具限制性的索引,通过决定哪个索引排除更多行并使用该索引来获取行。

  4. demo

    SELECT * FROM tbl_name WHERE col1=val1;
    SELECT * FROM tbl_name WHERE col1=val1 AND col2=val2;
    
    SELECT * FROM tbl_name WHERE col2=val2;
    SELECT * FROM tbl_name WHERE col2=val2 AND col3=val3;
    
    

    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).

    leftmost prefix 指的是索引的最左前缀,如果表有一个多列索引,优化器可以使用索引的任何最左前缀来查找行。例如,如果您在(col1,col2,col3)上有一个三列索引,则可以在(col1),(col1,col2)和(col1,col2,col3)上进行索引搜索。

    If an index exists on (col1, col2, col3), only the first two queries use the index. The third and fourth queries do involve indexed columns, but do not use an index to perform lookups because (col2) and (col2, col3) are not leftmost prefixes of (col1, col2, col3).

参考链接

标签:index,column,col2,col1,索引,mysql,梳理,columns
From: https://www.cnblogs.com/livebetter/p/17166412.html

相关文章

  • mysql忘记密码
    mysql是在三年前下载的,但是此时密码忘记了,又不想卸载后重新安装,此时需要重置密码.①netstopMySQL80(这个SQL根据服务中SQL的名称填写,网上大部分都是sql,我发......
  • mysql数据库的主主复制和半同步复制
    今天分享的是mysql数据库的主主复制和半同步复制的一系列步骤,以及在各处出现错误的解决方法和其在操作过程中需要注意的地方范例:主主复制主主复制:两个节点,都可以更新数据,并......
  • MySQL参数优化之thread_cache_size
    1.thread_cache_size简介每建立一个连接,都需要一个线程来与之匹配,此参数用来缓存空闲的线程,以至不被销毁,如果线程缓存中有空闲线程,这时候如果建立新连接,MYSQL就会很快的响应......
  • 路飞项目使用mysql数据库详细讲解
    目录一、首先需要彻底删除原有的数据库步骤二、去官网下载mysql步骤三、安装mysql数据库步骤四、一管理员身份进去cmd进行一系列命令启动五、接下来为路飞项目创建数据库六......
  • 通过java代码向mysql数据库插入记录,中文乱码处理
    处理步骤修改mysql配置文件,并重启mysql服务。mysql默认配置文件路径为/etc/my.cnf。修改配置如下:[mysqld]character-set-server=utf8[client]default-character-set......
  • MySQL 性能优化浅析及线上案例
    作者:京东健康孟飞1、数据库性能优化的意义业务发展初期,数据库中量一般都不高,也不太容易出一些性能问题或者出的问题也不大,但是当数据库的量级达到一定规模之后,如果缺失有......
  • MySql创建存储过程
    #1.插入数据前先把表中的索引去掉,数据插入完成之后,再创建索引#2.关闭事务的自动提交DROPPROCEDUREifEXISTSBatchInsert;#分隔符delimiter$$CREATEPROCEDURE......
  • mysql-索引
    一、索引回表:B+树索引主要可以分为两种索引,聚集索引和非聚集索引。聚集索引:也就是平常我们说的主键索引,在B+树中叶子节点存的是整行数据。非聚集索引:也叫二级索引,也......
  • MogDB 学习笔记之 -- 索引失效
    [[toc]]#概念描述哪些操作会导致分区表的全局索引失效(比如movepartition,droppartition,truncatepartition,splitpartition,mergepartitions)#测试验证1、环境准......
  • MySQL 内连接、外连接、全连接
    连接查询用于多表关联查询,连接方式不同,查询重点不同。内连接作用:查询多表之间交集部分数据关键词:innerjoin可简写为:join,即innerjoin=join图示:左外......