当前位置:网站首页>MySQL index classification
MySQL index classification
2022-06-12 19:49:00 【WD Technology】
(1)、 primary key
After setting it as the primary key, the database will automatically create an index ,innodb Index for clustering
(1) If the table has a primary key defined , be PK It's a clustered index ;
(2) If the table does not have a primary key defined , Then the first non empty unique index (not NULL unique) Columns are clustered indexes ;
(3) otherwise ,InnoDB Will create a hidden row-id As a clustered index ;( This row-id yes 6 Bit , So try to specify the primary key )
# Index with table :
CREATE TABLE customer (id INT(10) UNSIGNED AUTO_INCREMENT ,customer_no VARCHAR(200),customer_name VARCHAR(200),
PRIMARY KEY(id)
);
# Use AUTO_INCREMENT Keyword columns must have indexes ( As long as there is an index ).
CREATE TABLE customer2 (id INT(10) UNSIGNED,customer_no VARCHAR(200),customer_name VARCHAR(200),
PRIMARY KEY(id)
);
# Create a separate primary key index :
ALTER TABLE customer add PRIMARY KEY customer(customer_no);
# Delete the primary key index :
ALTER TABLE customer drop PRIMARY KEY ;
# Modify the primary key index :
# You must delete (drop) The original index , New again (add) Indexes
(2)、 Single value index
That is, an index contains only a single column , A table can have multiple single-column indexes
# Index with table :
CREATE TABLE customer (id INT(10) UNSIGNED AUTO_INCREMENT ,customer_no VARCHAR(200),customer_name VARCHAR(200),
PRIMARY KEY(id),
KEY (customer_name)
);
# Indexes built with tables Same as index name Name (customer_name)
# Create a single value index separately :
CREATE INDEX idx_customer_name ON customer(customer_name);
# Delete index :
DROP INDEX idx_customer_name ;
(3)、 unique index
The value of the index column must be unique , But you can have an empty value
# Index with table :
CREATE TABLE customer (id INT(10) UNSIGNED AUTO_INCREMENT ,customer_no VARCHAR(200),customer_name VARCHAR(200),
PRIMARY KEY(id),
KEY (customer_name),
UNIQUE (customer_no)
);
# establish The unique index must ensure that all values are unique ( except null), If there are duplicate data , Will report a mistake .
# Create a unique index :
CREATE UNIQUE INDEX idx_customer_no ON customer(customer_no);
# Delete index :
DROP INDEX idx_customer_no on customer ;
(4)、 Composite index
That is, an index contains multiple columns
During database operations , Composite indexes require less overhead than single valued indexes ( Index the same multiple columns )
A composite index can be used when the number of rows in a table is much larger than the number of indexed columns
# Index with table :
CREATE TABLE customer (id INT(10) UNSIGNED AUTO_INCREMENT ,customer_no VARCHAR(200),customer_name VARCHAR(200),
PRIMARY KEY(id),
KEY (customer_name),
UNIQUE (customer_name),
KEY (customer_no,customer_name)
);
# Index separately :
CREATE INDEX idx_no_name ON customer(customer_no,customer_name);
# Delete index :
DROP INDEX idx_no_name on customer ;
Basic grammar
establish :ALTER mytable ADD [UNIQUE ] INDEX [indexName] ON (columnname(length))
Delete :DROP INDEX [indexName] ON mytable;
see :SHOW INDEX FROM table_name\G
There are four ways to add indexes to a data table :
ALTER TABLE tbl_name ADD PRIMARY KEY (column_list): This statement adds a primary key , This means that the index value must be unique , And cannot be NULL.
ALTER TABLE tbl_name ADD UNIQUE index_name (column_list): The value of the index created by this statement must be unique ( except NULL Outside ,NULL There may be many times ).
ALTER TABLE tbl_name ADD INDEX index_name (column_list): Add a normal index , Index values can appear multiple times .
ALTER TABLE tbl_name ADD FULLTEXT index_name (column_list): The statement specifies that the index is FULLTEXT , For full-text indexing .
Which situations need to be indexed
Primary key automatically creates unique index
Fields that are frequently used as query criteria should be indexed (where Subsequent statements )
Fields associated with other tables in the query , Index foreign key relationship
Single key / The choice of Composite Index ,who?( Tend to create composite indexes at high and low levels )
Fields sorted in the query , If the sorting field is accessed by index, the sorting speed will be greatly improved
Statistics or grouping fields in query
When not to create indexes
There are too few records
Often add or delete tables : Improved query speed , At the same time, it will reduce the speed of updating the table , Such as on the table INSERT、UPDATE and DELETE. Because when updating tables ,MySQL Not only to save data , And save the index file
Where The fields that are not used in the condition are not indexed
Duplicate and evenly distributed table fields , So you should index only the most frequently queried and sorted data columns , A data column contains many duplicates , Indexing doesn't have much practical effect .
边栏推荐
- 3D object detection
- Equipment management - borrowing / returning module interface code
- exec函数、shell的实现
- 什么是数据驱动
- Basic structure of arithmetic unit
- [digital ic/fpga] data accumulation output
- Unsupported class file major version 60
- How to close icloud when Apple ID of Apple mobile phone forgets password and frequently jumps out to log in
- In 2022, 20 cities with the largest number of college students in China
- Fault analysis | a case of MySQL remote slave database replication delay
猜你喜欢

【数字IC/FPGA】数据累加输出

VC Hacon Joint Programming genimage3extern writeimage

Process accounting, process time, daemon

基于微信电子书阅读小程序毕业设计毕设作品(7)中期检查报告

Microsoft Word tutorial, how to insert page numbers and table of contents in word?

Reading small program based on wechat e-book graduation design (4) opening report

torch 网络模型转换onnx格式,并可视化

测试必备:推荐一款跨平台App性能专项测试工具!

Test prerequisites: recommend a special cross platform app performance test tool!

Using / developing private plug-ins in traifik proxy 2.5 (traifik official blog)
随机推荐
基于微信电子书阅读小程序毕业设计毕设作品(8)毕业设计论文模板
Viewpoint sharing | Li Wei, an expert of Gewu titanium intelligent technology products: underlying logic and scenario practice of unstructured data platform
typescript的装饰器(Decorotor)基本使用
【刷题笔记】线段树
开源深度学习框架PlaidML安装及测试
进程会计、进程时间、守护进程
system()
New product launch
基于微信电子书阅读小程序毕业设计毕设作品(4)开题报告
解释器文件
Macro definitions and functions
golang类型断言理解[go语言圣经]
今晚7:00 | PhD Debate 自监督学习在推荐系统中的应用
Mode of most elements (map, sort, random, Boyer Moore voting method)
Low code enables rural construction to open "smart mode"
First build green, then build city
Process creation fork (), demise wait()
Jenkins中pipeline对接CMDB接口获取主机列表的发布实践原创
Torch network model is converted to onnx format and visualized
IO流基础知识详解--文件及IO流原理