当前位置:网站首页>MySQL index
MySQL index
2022-07-06 01:58:00 【Crazy Stuka】
List of articles
- 1. The concept of index
- 2. The function of index
- Side effects of indexing
- 3. The principle of index creation is based on
- 4. Classification and creation of index
- Specify index when creating table
- Create a unique index directly
- Modify tables to create
- When creating a table, specify
- (5) Full-text index
- Fuzzy query instance
- Delete index
1. The concept of index
An index is a sorted list , In this list are stored the value of the index and the physical address of the row containing the data of this value ( Be similar to c The linked list of language points to the memory address of data record through pointer ).
After using the index, you can locate the data of a row without scanning the whole table , Instead, we first find the corresponding physical address of the row data through the index table, and then access the corresponding data , Therefore, it can speed up the query speed of the database .
An index is like a catalogue of a book , You can quickly find the required content according to the page number in the table of contents .
Index is the top method to sort the values of one or several columns in a table .
The purpose of indexing is to speed up the search or sorting of records in a table .
2. The function of index
After setting the appropriate index , The database uses a variety of fast positioning technologies , Can greatly speed up the query speed , This is the main reason to create indexes .
When the table is large or the query involves multiple tables , Using indexes can improve query speed thousands of times .
It can reduce the cost of the database IO cost , And the index can also reduce the sorting cost of the database .
By creating a unique index , It can ensure the uniqueness of each row of data in the data table .
You can speed up the connection between tables .
When using grouping and sorting , Can greatly reduce the grouping and sorting time .
Indexing can significantly improve performance when searching and recovering data in a database
Side effects of indexing
Index takes up extra disk space .
about MyISAM In terms of engines , Index files and data files are separate , The address where the index file is used to hold the data record .
and InnoDB The table data file of the engine itself is the index file .
It takes more time to insert and modify data , Because the index has to change with it .
3. The principle of index creation is based on
Although index can improve the speed of database query , But it's not always appropriate to create an index . Because the index itself consumes system resources , With an index , The database will perform index query first , Then navigate to the specific data row , If the index is not used properly , On the contrary, it will increase the burden of the database .
Primary Key 、 Foreign key must have index . Because the primary key is unique , The foreign key is associated with the primary key of the primary table , You can quickly locate when querying .
Number of records exceeds 300 The table of rows should have an index . If there is no index , Every query needs to traverse the table , Will seriously affect the performance of the database .
A table that is often connected to other tables , Index should be established on the connection field .
Fields with poor uniqueness are not suitable for indexing .
Fields that are updated too frequently are not suitable for creating indexes .
It often appears in where Fields in clause , In particular, the fields of large tables , It should be indexed .
On a regular basis GROUP BY、ORDER BY Index the fields of .
Indexes should be built on fields with high selectivity .
Indexes should be built on small fields , For large text fields or even extra long fields , Don't index .
4. Classification and creation of index
(1) General index : The most basic index type , There's no such thing as uniqueness .
Create index directly
create index Index name on Table name ( Field )
Modify tables to create
ALTER TABLE Table name ADD INDEX Index name ( Name );
Specify index when creating table
CREATE TABLE Table name ( Field 1 data type , Field 2 data type [,...],INDEX Index name ( Name ));
(2) unique index : Similar to a normal index , But the difference is that each value of a unique index column is unique . Null values are allowed for unique indexes ( Note that it's different from the primary key ). If it's a composite index , The combination of column values must be unique . Adding a unique key automatically creates a unique index .
Create a unique index directly
Modify tables to create
ALTER TABLE Table name ADD UNIQUE Index name ( Name );
When creating a table, specify
CREATE TABLE Table name ( Field 1 data type , Field 2 data type [,...],UNIQUE Index name ( Name ));
(3) primary key : Is a special unique index , Must be specified as "PRIMARY KEY”. A table can only have one primary key , No null values are allowed . Adding a primary key will automatically create a primary key index .
Modify tables to create
ALTER TABLE Table name ADD PRIMARY KEY( Name )
(4) Composite index :( Single column index and multi column index ): It can be an index created on a single column , It can also be indexes created on multiple columns . Need to meet The principle of left , because select Of the statement where The conditions are executed from left to right in turn , So it's using select Statement query where
The order of the fields used by the condition must be the same as the sort in the composite index , Otherwise the index will not take effect .
Modify the table structure to create
mysql> alter table member add index phone_addr_index (phone,address);
(5) Full-text index
(FULLTEXT): It is suitable for fuzzy query , Can be used to retrieve text information in an article . stay MySQL5.6 Version before FULLTEXT The index can only be used for MyISAM engine , stay 5.6 After the version innodb The engine also supports FOLLTEXT Indexes . Full text indexing can be done in CHAR、VARCHAR、 perhaps TEXT Create on column of type . Only one full-text index per table is allowed .
Create index directly
mysql> create fulltext index remark_index on member(remark);
Modify table structure and create index
Fuzzy query instance
Look at the index
The meaning of each field is as follows :
Table: The name of the table .
Non_unique: If the index can't include repetition , Then for 0; If possible , Then for 1.
Key_name: Name of index .
Seq_in_index: Column ordinal in index , from 1 Start .
column_name: Column name .
collation: How columns are stored in indexes . stay MySQL in , Valuable 'A′( Ascending ) or NULL( No classification ).
Cardinality: An estimate of the number of unique values in an index .
Sub_part: If the column is only partially indexed , Is the number of characters indexed . If the entire column is indexed , Then for NuLL.
Packed: Indicates how keywords are compressed . If it's not compressed , Then for NULL.
Null: If the column contains NULL, It contains YES. without , The column contains NO.
Index_type: Used indexing methods (BTREE,FULLTEXT,HASH,RTREE).
Comment: remarks .
Delete index
Delete index directly
DROP INDEX Index name ON Table name ;
Modify the table to delete the index
ALTER TABLE Table name DROP INDEX Index name ;
Delete primary key index
alter table Table name drop primary key;
`
边栏推荐
- [network attack and defense training exercises]
- Accelerating spark data access with alluxio in kubernetes
- leetcode-2. Palindrome judgment
- ClickOnce does not support request execution level 'requireAdministrator'
- A Cooperative Approach to Particle Swarm Optimization
- Computer graduation design PHP enterprise staff training management system
- Redis list
- Mongodb problem set
- [flask] obtain request information, redirect and error handling
- How to upgrade kubernetes in place
猜你喜欢
Force buckle 9 palindromes
NumPy 数组索引 切片
Executing two identical SQL statements in the same sqlsession will result in different total numbers
Blue Bridge Cup embedded_ STM32 learning_ Key_ Explain in detail
Redis list
[Jiudu OJ 09] two points to find student information
TrueType字体文件提取关键信息
Online reservation system of sports venues based on PHP
2 power view
【SSRF-01】服务器端请求伪造漏洞原理及利用实例
随机推荐
Redis string type
Basic operations of databases and tables ----- primary key constraints
[solved] how to generate a beautiful static document description page
Luo Gu P1170 Bugs Bunny and Hunter
leetcode-2.回文判断
[understanding of opportunity-39]: Guiguzi - Chapter 5 flying clamp - warning 2: there are six types of praise. Be careful to enjoy praise as fish enjoy bait.
selenium 元素定位(2)
500 lines of code to understand the principle of mecached cache client driver
Online reservation system of sports venues based on PHP
Internship: unfamiliar annotations involved in the project code and their functions
Leetcode sum of two numbers
正则表达式:示例(1)
Competition question 2022-6-26
How does redis implement multiple zones?
This time, thoroughly understand the deep copy
[flask] obtain request information, redirect and error handling
selenium 等待方式
NumPy 数组索引 切片
国家级非遗传承人高清旺《四大美人》皮影数字藏品惊艳亮相!
干货!通过软硬件协同设计加速稀疏神经网络