当前位置:网站首页>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 !
边栏推荐
- 2022, enterprise unified process platform design and integration specifications refer to thubierv0.1
- Home raiding II (leetcode-213)
- 多表查询之子查询_单行单列情况
- MySQL - 删除数据库表中的数据
- NIO知识点
- Kotlin Advanced Grammar
- PC博物馆(2) 1972年 HP-9830A
- How many indexes are associated indexes under MySQL InnoDB?
- Curse of knowledge
- Programmers can't JVM? Ashes Engineer: all waiting to be eliminated! This is a must skill!
猜你喜欢

差分约束系统---1且2--2022年5月27日

Nirvana rebirth! Byte Daniel recommends a large distributed manual, and the Phoenix architecture makes you become a God in fire

PC博物馆(1) 1970年 Datapoint 2000

Analysis of distributed lock redistribution principle

Sentinel 三种流控效果

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

Differential restraint system -- 1 and 2 -- May 27, 2022
![[correcting Hongming] what? I forgot to take the](/img/41/c8fa6380ab63949ae6d904fdbb005c.png)
[correcting Hongming] what? I forgot to take the "math required course"!

IEPE vibration sensor synchronous signal acquisition card /icp synchronous data network acquisition module

zoj-Swordfish-2022-5-6
随机推荐
The best time to buy and sell stocks Ⅲ (leetcode-123)
Balance between management / business and technology
PC博物馆(1) 1970年 Datapoint 2000
WEB安全基础 - - -文件上传(文件上传绕过)
Volcanic engine: open ByteDance, the same AI infrastructure, a system to solve multiple training tasks
Curse of knowledge
MySQL performance optimization (IV): how to use indexes efficiently and correctly
The method modified by private can be accessed through reflection. What is the meaning of private?
OSPF includes special area experiments, mGRE construction and re release
Google Earth engine - QA in Landsat 5 toa dataset_ Pixel and QA_ Radsat band
MySQL - 唯一索引
Basic SQL operations
Rust tokio:: task:: localset running mode
MySQL - 多列索引
Uniapp calendar component
谷歌联合高校研发通用模型ProteoGAN,可设计生成具有新功能的蛋白质
Erlang学习02
Kotlin advanced
[electronic device note 4] inductance parameters and type selection
Try the video for 5 minutes [easy to understand]