当前位置:网站首页>MySQL - normal index
MySQL - normal index
2022-07-24 10:31:00 【Camellia Populus】
Create and view indexes
- Creating an index is to create an index on one or more columns of a table , In order to speed up the access to the table .
- Index creation has 3 Ways of planting , Namely Create indexes when creating tables 、 Create an index on an existing table and Use ALTER TABLE Statement to create an index . This section will explain this in detail according to the specific index classification 3 Ways to create .
General index
The so-called general index , When creating an index , Without any restrictions ( only 、 Non empty limit ). Indexes of this type can be created on fields of any data type .
1. Define indexes when creating tables
CREATE TABLE tablename( propname1 type1, propname2 type2, …… propnamen type..n, INDEX | KEY [indexname] (propnamen [(length)] [ ASC | DESC ] ) );Parameters INDEX and KEY Is used to specify a field as an index , Just choose one of the two , It's the same thing ;
Parameters indexname It's the index name , Omission ;
Parameters propnamen Is the name of the field corresponding to the index , This field must be the one defined before ;
Parameters length Is an optional parameter , It refers to the length of the index , Must be a string type to use ;
Parameters ASC and DESC All optional parameters ,ASC Indicates ascending order ,DESC Represents a descending order , If you don't specify , In ascending order .example :
mysql> create table class(id int, name varchar(64), teacher varchar(64), INDEX id_index (id DESC)); // establish class surface , Appoint id Column as index column , The sorting method is descending mysql> show create table class; // Query table structure mysql> insert into class values(1, ' Class one ', ' Zhang San '); // insert data 1 mysql> insert into class values(2, ' Class two ', ' Li Si '); // insert data 2 mysql> select * from class where id>0; // Query by column

2. Create an index on an existing table
Method 1 : perform create sentenceCREATE INDEX indexname ON tablename (propname [(length)] [ASC|DESC]);Parameters INDEX Is used to specify a field as an index , It can't be KEY;
Parameters indexname Is the name of the newly created index ;
Parameters tablename Is the name of the table that needs to be indexed , The table must already exist , If it doesn't exist , You need to create ;
Parameters propname Specify the name of the field corresponding to the index , This field must be the one defined before ;
Parameters length Is an optional parameter , Indicates the length of the index , Must be a string type to use ;
Parameters ASC and DESC All optional parameters ,ASC Indicates ascending order ,DESC Represents a descending order , Default ascending order .mysql> create table class(id int, name varchar(64), teacher varchar(64)); // establish class surface mysql> create INDEX id_index ON class(id DESC); // Appoint class In the table id Index column , Descending order mysql> show create table class; // Query table structure mysql> insert into class values(1, ' Class one ', ' Zhang San '); // insert data 1 mysql> insert into class values(2, ' Class two ', ' Li Si '); // insert data 2 mysql> select * from class where id>0; // Query by columnMethod 2 : perform ALTER TABLE sentence
ALTER TABLE tablename ADD INDEX | KEY indexname (propname [(length)] [ASC|DESC]);Parameters tablename Is the table that needs to be indexed ;
keyword IDNEX or KEY Used to specify the creation of a normal index ;
Parameters indexname Used to specify the name of the index created ;
Parameters propname Used to specify the name of the field associated with the index ;
Parameters length Used to specify the length of the index ;
Parameters ASC Used to specify ascending sort ; Parameters DESC Used to specify descending sort .mysql> create table class(id int, name varchar(64), teacher varchar(64)); // establish class surface mysql> alter table class INDEX id_index (id DESC); // Appoint class In the table id Index column , Descending order mysql> show create table class; // Query table structure mysql> insert into class values(1, ' Class one ', ' Zhang San '); // insert data 1 mysql> insert into class values(2, ' Class two ', ' Li Si '); // insert data 2 mysql> select * from class where id>0; // Query by column
Check the execution of the index
- EXPLAIN Query statement
- Output results :
key: Actual index used . If NULL, No index is used
possible_keys: Show the indexes that may be applied to this table , One or more . If there is an index on the field involved in the query , Then the index will be listed , But it doesn't have to be actually used by the query
key_len: Represents the number of bytes used in the index , You can use this column to calculate the length of the index used in the query . The shorter the value, the better !
边栏推荐
- MySQL——锁
- Zoj1137+ operation 1 -- May 28, 2022
- OSPF含特殊区域实验,MGRE构建和重发布
- zoj-Swordfish-2022-5-6
- [correcting Hongming] what? I forgot to take the "math required course"!
- Chapter V Modification implementation (impl) class
- pom文件dependency中的 scope用法
- WEB安全基础 - - -文件上传(文件上传绕过)
- [leecode] get the longest common substring of two strings
- 多表查询之子查询_单行单列情况
猜你喜欢

MySQL - 索引的隐藏和删除

Dynamic programming -- a collection of stock problems

ECCV 2022 | 清华提出首个嵌入光谱稀疏性的Transformer

PC博物馆(1) 1970年 Datapoint 2000

Home raiding III (leetcode-337)

757. Set the intersection size to at least 2: greedy application question

WEB安全基础 - - -文件上传(文件上传绕过)

《nlp入门+实战:第二章:pytorch的入门使用 》

Websocket 协议解读-RFC6455

How does ribbon get the default zoneawareloadbalancer?
随机推荐
PC博物馆(1) 1970年 Datapoint 2000
Arduino + AD9833 波形发生器
Configuration description and componentization development steps of various documents in the scaffold
JS function call download file link
谷歌联合高校研发通用模型ProteoGAN,可设计生成具有新功能的蛋白质
2022, our small goal
[recommendation system] the classic technical architecture of the data flow of the recommendation system + the most complete evolution map of the top 10 deep learning CTR models of Microsoft, Alibaba,
Sentinel 流量控制快速入门
Volcanic engine: open ByteDance, the same AI infrastructure, a system to solve multiple training tasks
Arduino + AD9833 waveform generator
2022, will lead the implementation of operation and maintenance priority strategy
ZOJ 2770 differential restraint system -- 2 -- May 20, 2022
563页(30万字)智慧化工园区(一期)总体设计方案
WEB安全基础 - - -文件上传(文件上传绕过)
ECCV 2022 | Tsinghua proposes the first transformer to embed spectral sparsity
Nirvana rebirth! Byte Daniel recommends a large distributed manual, and the Phoenix architecture makes you become a God in fire
Programmers can't JVM? Ashes Engineer: all waiting to be eliminated! This is a must skill!
Erlang studies abroad
Home raiding III (leetcode-337)
Dr. water 3