当前位置:网站首页>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;
`
边栏推荐
- Redis如何实现多可用区?
- Xshell 7 Student Edition
- 通过PHP 获取身份证相关信息 获取生肖,获取星座,获取年龄,获取性别
- NLP fourth paradigm: overview of prompt [pre train, prompt, predict] [Liu Pengfei]
- Publish your own toolkit notes using NPM
- 竞赛题 2022-6-26
- Redis-列表
- Online reservation system of sports venues based on PHP
- Redis key operation
- [le plus complet du réseau] | interprétation complète de MySQL explicite
猜你喜欢
Campus second-hand transaction based on wechat applet
XSS learning XSS lab problem solution
安装php-zbarcode扩展时报错,不知道有没有哪位大神帮我解决一下呀 php 环境用的7.3
Publish your own toolkit notes using NPM
3D视觉——4.手势识别(Gesture Recognition)入门——使用MediaPipe含单帧(Singel Frame)和实时视频(Real-Time Video)
Using SA token to solve websocket handshake authentication
500 lines of code to understand the principle of mecached cache client driver
UE4 unreal engine, editor basic application, usage skills (IV)
Pangolin Library: subgraph
NLP fourth paradigm: overview of prompt [pre train, prompt, predict] [Liu Pengfei]
随机推荐
Leetcode3. Implement strstr()
【Flask】官方教程(Tutorial)-part2:蓝图-视图、模板、静态文件
NiO related knowledge (II)
Cadre du Paddle: aperçu du paddlelnp [bibliothèque de développement pour le traitement du langage naturel des rames volantes]
晶振是如何起振的?
Publish your own toolkit notes using NPM
【Flask】官方教程(Tutorial)-part1:项目布局、应用程序设置、定义和访问数据库
剑指 Offer 38. 字符串的排列
Paddle framework: paddlenlp overview [propeller natural language processing development library]
阿裏測開面試題
Basic operations of databases and tables ----- primary key constraints
ClickOnce does not support request execution level 'requireAdministrator'
National intangible cultural heritage inheritor HD Wang's shadow digital collection of "Four Beauties" made an amazing debut!
Format code_ What does formatting code mean
leetcode3、實現 strStr()
【Flask】官方教程(Tutorial)-part3:blog蓝图、项目可安装化
Basic operations of databases and tables ----- unique constraints
Pangolin Library: subgraph
[flask] obtain request information, redirect and error handling
Ali test Open face test