当前位置:网站首页>MySQL index
MySQL index
2022-07-27 16:09:00 【w ͏ l ͏ j ͏】
Indexes
# Basic concepts
- Indexes : Data structure to optimize query efficiency
- shortcoming :
1. Maintaining indexes consumes database resources
2. Take up disk space
3. Maintain the index when adding, deleting and modifying
- principle : Index common search fields
- Index classification
- InnoDB( Must be ) Primary key only Single column Reunite with
1. primary key ( Cluster index )
2. Single index ( Single value , Ordinary )
3. unique index ( The value of the column is unique , It can be for null)
4. Composite index ( Multiple columns in one index )
- MyISAM( Unsupported transaction )
- Full Text : Full-text index
# Basic operation of index :
- Query index
show index from tableName
- Creation of ordinary index key()
1. Create... When creating a table
create table t user(id varchar(20) primary key, name varchar(20), key(column));
2. After creating a table, create
create index indexName on table(tableColumnName)
- unique index
1. Create... When creating a table
create table t user(id varchar(20) primary key, name varchar(20), unique(column));
2. After creating a table, create
create unique index indexName on table(tableColumnName)
- Composite index
1. Create... When creating a table
create table t user(id varchar(20) primary key, name varchar(20), key(column1,column2));
2. After creating a table, create
create index indexName on table(col1,col2)
3. principle :( interview )
1. Leftmost prefix principle ( Left containment principle )
Create order, such as (name,age), that name age bir Sure ,age name bir Can not be ()
2. Dynamically adjust the query order , In order to make better use of queries
## The index name cannot be specified when creating a table , After creating the table, you can specify the index name
# Underlying data structure (B+ Tree) It's usually 2-4 layer
## The phenomenon
1. Insert id disorder
2. Automatically sort when querying ( The primary key index is sorted )
## explain
1. Index auto sort , Convenient for quick query in the future
## other
1. Page based management , Add the principle that the index will automatically sort
2. By default, every page is 16KB
3. A page table ( Store the first pointer of each page , You can link directly to the corresponding page )
4. Top level pages reside in memory
# Clustered index and non clustered index
- Cluster index :( The default is primary key , If there is no primary key, you will find a unique and non empty index )
Put the data and index together , The leaf node of the index structure holds the row data (B+)
- Nonclustered index :
Separate data from index , The leaf node of the index structure points to the corresponding position of the data
The primary key of the data is searched , Need to search again is the real data
The reason why there is no address is : The storage address needs to be changed to the address of the auxiliary index , If you use a primary key, you don't have to consider modifying the address .
## The primary key index must be a clustered index , Otherwise, it doesn't work .
## A table can only have one clustered index , Others are non clustered or auxiliary indexes .
## InnoDB Clustering advantages
1. Save the primary key value , It can prevent the maintenance of non clustered trees when modifying addresses , It can also reduce disk consumption
2. Clustered indexes store primary key and row data , Find the leaf node and you can return data
## MyISAM Nonclustered index , It's the address
# Why is it recommended that the primary key be self incremented ?
1. If at this time a page 1 2 3 4 5 6 7 8 10 11, If you want a primary key 9 Data insertion , According to the principle of sorting by primary key , Will plug into 8 Back , But if this page is full , That will cause page splitting and record shifting , Cause performance loss .
# Index failure scenario
1. The best left prefix rule ( important )
2. It's not equal to (!= perhaps <>) Cause index to fail
3. Primary key insertion order ( Why is it recommended that the primary key be self incremented ?)
4. Calculation 、 function 、 Index invalidation due to type conversion
5. The column index to the right of the range condition fails ( Multiple composite indexes are in different order )
6. is null You can use index ,is not null Index not available
7. like Use wildcards % Invalid start index ( important )
8. OR As long as there are non indexed columns before and after , Will lead to index invalidation
9. Database and table character sets are used uniformly utf8mb4
## INNODB Maximum ID Record in memory , Restart the database or OPTIMIZE The operation will be lost .
- optimize You can spread it out (fragmented) The stored data is moved back together (defragmentation), Clear away the debris , Reclaim idle database space
- This action locks the table
# HEAP Tables exist in memory , For temporary high-speed storage .
# CHAR_LENGTH It's the number of characters , and LENGTH Is the number of bytes .
# SELECT VERSION(); Used to get the current Mysql Version of .
# Create table time TIMESTAMP Column use Zero to update . As long as other fields in the table change ,
# UPDATE CURRENT_TIMESTAMP The Modifier updates the timestamp field to the current time
# shell Sign in MySQL
# [mysql dir]/bin/mysql -h hostname -u <UserName> -p <password>
# MySQL A single watch exceeds 500 The capacity of ten thousand rows or single table exceeds 2GB Divide the database or table
# Like Fuzzy query :% Corresponding to 0 Characters or more ,_ It's just LIKE A character in a statement .
# Mysql stay V5.1 The previous default storage engine was MyISAM; After that, the default storage engine is InnoDB
# Any standard table can be built at most 16 Index columns
# NUMERIC and DECIMAL Used to save data with high progress , For example, the financial industry

MySQL Business
- MySQL The default is autocommit Mode , Transaction is not supported by default , All database update operations will be submitted immediately .
- If the engine is InnoDB, Then you can set support transactions .Set AutoCommit = 0 , The update operation in this mode requires manual commit Will take effect , Use Rollback Perform transaction rollback .
# Open transaction START TRANSACTION; SELECT @A:=SUM(salary) FROM table1 WHERE type=1; UPDATE table2 SET summmary=@A WHERE type=1; COMMIT;
MySQL lock

边栏推荐
猜你喜欢

QT (VI) value and string conversion

flume增量采集mysql数据到kafka

时间序列——使用tsfresh进行分类任务

Scratch crawler framework

无线网络分析有关的安全软件(aircrack-ng)

解决flink启动后无法正常关闭

解决MT7620不断循环uboot(LZMA ERROR 1 - must RESET board to recover)

MySQL索引

Sword finger offer 51. reverse pairs in the array
![[sword finger offer] interview question 42: the maximum sum of continuous subarrays -- with 0x80000000 and int_ MIN](/img/01/bbf81cccb47b6351d7265ee4a77c55.png)
[sword finger offer] interview question 42: the maximum sum of continuous subarrays -- with 0x80000000 and int_ MIN
随机推荐
Personal perception of project optimization
接连取消安富利/文晔/世平代理权,TI到底打的什么算盘?
Axure 安装图标字体元件库
台积电的反击:指控格芯侵犯25项专利,并要求禁售!
Paper_ Book
ARIMA模型选择与残差
禁令之下,安防巨头海康与大华的应对之策!
Delete node quickly and efficiently_ modules
: 0xc0000005: an access conflict occurs when writing position 0x01458000 - to be solved
[sword finger offer] interview question 53-i: find the number 1 in the sorted array -- three templates for binary search
数据表的约束以及设计、联合查询——8千字攻略+题目练习解答
It can carry 100 people! Musk releases the strongest "starship" in history! Go to Mars as early as next year!
C语言程序设计(第三版)
携手SiFive,格兰仕进军半导体领域!两款自研芯片曝光
makefile 中指定程序运行时加载的库文件路径
Using Lombok results in the absence of parent class attributes in the printed toString
[tensorboard] oserror: [errno 22] invalid argument processing
Content ambiguity occurs when using transform:translate()
Live broadcast software development, customized pop-up effect of tools
快速高效删除node_modules