当前位置:网站首页>B+ tree selection index (1) -- MySQL from entry to proficiency (22)
B+ tree selection index (1) -- MySQL from entry to proficiency (22)
2022-07-26 13:17:00 【User 9919783】
The last article introduced that grouping with index columns can also improve efficiency , And the cost of returning to the table , The back table is a secondary index + Return to the table for query , If the amount of data returned is too large ,mysql The optimizer will use full table scan queries , The overlay index will not be returned to the table .
B+ Tree index uses (9) grouping 、 Back to the table 、 Overlay index ( The 21st )
How to select indexes
After understanding the power of index , How do we use indexes ?
search in progress 、 Sort 、 Create indexes for grouped Columns
That is to say where Column after statement , Join column in join clause , Or appear in order by 、group by The columns in the clause need to be indexed , and select Query columns do not need . such as
Select birthday,country from person_info where name = ‘’; This sql Statement inside birthday and country There's no need to create indexes , and name You need to create an index .
Consider the cardinality of the column
The cardinality of a column refers to the non duplicate data in a column , such as name Listed 3,2,3,5,5,3,2,3,3, Although there are many data , But his different data is only three , So the base number is 3. In the data record , The larger the cardinality of the column , The more scattered the data , The smaller the cardinality of the column , It shows that the more centralized the data . Suppose the cardinality of the column is 1, All the data in the front row are the same , You can't sort , There is no point in indexing , If you build a secondary index , It will also lose performance due to the operation of returning a large amount of data , So only when the column cardinality is larger , Is suitable for indexing .
The type of index column should be as small as possible
We are defining the specified column types displayed in the table structure , Let's take integer type as an example , Yes TINYINT,MEDIUMINT,INT,BIGINT How many , The memory they occupy increases in turn , The type size we refer to here , Is to establish the data range of this column , Minimize choices , This is because :1) The smaller the data type , The faster the query (CPU Level things ).2) The smaller the amount of data , The smaller the storage space occupied by the index , Put more records on a data page , This reduces the number of disks I/O Performance loss , Reduce the performance consumption caused by querying more data pages .
This is not only applicable to secondary indexes , The same applies to primary keys , Because the leaf nodes of the secondary index have primary keys , The smaller the type of primary key , The smaller the memory used .
Index string prefix
We know that a string consists of several characters , We use it utf8 The composition needs to occupy 1~3 Bytes ,1) Spatiality :b+ The tree needs to store all the strings of the columns , The longer the string ,b+ The larger the space occupied by the leaf node of the tree .2) Timeliness : The longer the string , When sorting and comparing , The longer it takes to compare one character to another .
We talked about that before , For columns of strings , It is sorted by character , So the index designer , Considering space and time , Only the first few specified characters of the string are stored and sorted , In this way, you can also relatively locate your seat , Using the corresponding primary key id De clustering index b+ The tree queries all values . Write sql We can write this when , establish name by 100 String range of , But the index specification only specifies name front 10 Characters of .
mysql> create table person_info2(
-> id int not null auto_increment,
-> name varchar(100) not null,
-> birthday date not null,
-> phone char(11) not null,
-> country varchar(100) not null,
-> primary key (id),
-> key idx_name_birthday_phone (name(10),birthday,phone)
-> );
Query OK, 0 rows affected (0.04 sec)
// You can also create long string columns hash Indexes :
mysql> create index hash_name using hash on person_info2(name);
Query OK, 0 rows affected (0.04 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show index from person_info2;
+--------------+------------+-------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+--------------+------------+-------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| person_info2 | 0 | PRIMARY | 1 | id | A | 0 | NULL | NULL | | BTREE | | |
| person_info2 | 1 | idx_name_birthday_phone | 1 | name | A | 0 | 10 | NULL | | BTREE | | |
| person_info2 | 1 | idx_name_birthday_phone | 2 | birthday | A | 0 | NULL | NULL | | BTREE | | |
| person_info2 | 1 | idx_name_birthday_phone | 3 | phone | A | 0 | NULL | NULL | | BTREE | | |
| person_info2 | 1 | hash_name | 1 | name | A | 0 | NULL | NULL | | BTREE | | |
+--------------+------------+-------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+边栏推荐
- Today in history: IBM obtained the first patent; Verizon acquires Yahoo; Amazon releases fire phone
- 深度学习3D人体姿态估计国内外研究现状及痛点
- [flower carving hands-on] fun music visualization series small project (12) -- meter tube fast rhythm light
- Hcip day 11 comparison (BGP configuration and release)
- Shutter background graying effect, how transparency, gray mask
- 基于C#开放式TCP通信建立与西门子PLC的socket通信示例
- 二叉树的初阶笔记
- postgresql官网下载出错
- Today's sleep quality record 75 points
- The child component triggers the defineemits of the parent component: the child component passes values to the parent component
猜你喜欢
![[5g] what are Cu and Du in 5g?](/img/5b/3453ade88ded4593edfcba9beb8ada.jpg)
[5g] what are Cu and Du in 5g?

C regards type as generic type T and uses it as generic type of method

HCIP第十二天笔记整理(BGP联邦、选路规则)

【5G】5G中的CU和DU是什么?

1312_ Apply 7z command for compression and decompression

3D modeling and rendering based on B é zier curve
![[typescript] typescript common types (Part 2)](/img/6b/2ac07f16af044bdfb719753ae241cc.png)
[typescript] typescript common types (Part 2)

银行业客户体验管理现状与优化策略分析

从其他文件触发pytest.main()注意事项

A college archives management system based on asp.net
随机推荐
mqtt send receive
Outline design specification
Shutter cachednetworkimage fillet
Leetcode 263. ugly number
MySQL data directory (3) -- table data structure MyISAM (XXVI)
Does anyone know where the retract of flinksql can be specified? Only api code settings can be seen in online materials
RMII, smii, gmii, rgmii interfaces of Ethernet Driver
牛客刷SQL---2
Kubelet CRI 容器运行时
postgresql官网下载出错
panic: Error 1045: Access denied for user ‘root‘@‘117.61.242.215‘ (using password: YES)
[typescript] typescript common types (Part 1)
Exploration on cache design optimization of community like business
A college archives management system based on asp.net
Shutter background graying effect, how transparency, gray mask
基于BERT的情感分析模型
B+树挑选索引(2)---mysql从入门到精通(二十三)
Elementary notes of binary tree
Can I take your subdomain? Exploring Same-Site Attacks in the Modern Web
MySQL data directory (2) -- table data structure (XXV)