当前位置:网站首页>Mysql database - index
Mysql database - index
2022-06-21 21:12:00 【[email protected]】
List of articles
1 Indexes
1.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 a way to sort the values of one or more columns in a table .
● The purpose of indexing is to speed up the search or sorting of records in a table .
1.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 for creating all the .
● 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 .
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 .
1.3 The principle of index creation is based on
Indexing can speed up database queries , 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 child 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 , You need 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 .
● 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 .
1.4 Classification and creation of index
1.4.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 ( Name [(length)]);
#( Name (length)):length Optional. . If you ignore length Value , Then use the value of the entire column as the index . If specified, use the... Before the column length Characters to create the index , This helps to reduce the size of the index file .
# The index name is suggested to be “_index” ending .
example :create index phone_index on member (phone);
select phone from member;
show create table member;
● Modify tables to create
ALTER TABLE Table name ADD INDEX Index name ( Name );
example :alter table member add index id_index (id);
select id from member;
select id,name from member;
Specify index when creating table
CREATE TABLE Table name ( Field 1 data type , Field 2 data type [,...],INDEX Index name ( Name ));
example :create table test(id int(4) not null,name varchar(10) not null,cardid varchar(18) not null,index id_index (id));
show create table test;
1.4.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
CREATE UNIQUE INDEX Index name ON Table name ( Name );
example :select * from member;
create unique index address_index on member (address);
create unique index name_index on member (name);
show create table member;
Modify tables to create
ALTER TABLE Table name ADD UNIQUE Index name ( Name );
example :alter table member add unique cardid_index (cardid);
● Specify index when creating table
CREATE TABLE Table name ( Field 1 data type , Field 2 data type [,...],UNIQUE Index name ( Name ));
example :create table amd2 (id int,name varchar(20),unique id_index (id));
show creat table amd2;
1.4.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 .
● When creating a table, specify
CREATE TABLE Table name ([...],PRIMARY KEY ( Name ));
example :create table test1 (id int primary key,name varchar(20));
create table test2 (id int,name varchar(20),primary key (id));
show create table test1;
show create table test2;
● Modify tables to create
ALTER TABLE Table name ADD PRIMARY KEY ( Name );
1.4.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 .
CREATE TABLE Table name ( Name 1 data type , Name 2 data type , Name 3 data type ,INDEX Index name ( Name 1, Name 2, Name 3));
select * from Table name where Name 1='...' AND Name 2='...' AND Name 3='...';
example :create table amd1 (id int not null,name varchar(20),cardid varchar(20),index index_amd (id,name));
show create table amd1;
insert into amd1 values(1,'zhangsan','123123');
select * from amd1 where name='zhangsan' and id=1;
Query index usage select Query notes where The leftmost principle of . The order of the query fields should be consistent with the composite index , To take effect .
1.4.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 FULLTEXT 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
CREATE FULLTEXT INDEX Index name ON Table name ( Name );
example :select * from member;
create fulltext index remark_index on member (remark);
● Modify tables to create
ALTER TABLE Table name ADD FULLTEXT Index name ( Name );
● Specify index when creating table
CREATE TABLE Table name ( Field 1 data type [,...],FULLTEXT Index name ( Name ));
1
# The data type can be CHAR、VARCHAR perhaps TEXT
● Use full text index queries
SELECT * FROM Table name WHERE MATCH( Name ) AGAINST(' Query content ');
example :select * from member where match(remark) against('this is vip');
2 Look at the index
show index from Table name ;
show index from Table name \G; Display table index information vertically
show keys from Table name ;
show keys from Table name \G;
| Field | meaning |
|---|---|
| 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 |
| lndex_type | Used indexing methods (BTREE,FULLTEXT,HASH,RTREE) |
| comment | remarks |
3 Delete index
Besides deleting the primary key index , The way to delete other indexes is the same .
Delete primary key index
alter table Table name drop primary key;
Delete non primary key index
drop index Index name on Table name ; # Delete index directly
alter table Table name drop index Index name ; # How to modify the table to drop the index
summary
General index
create index Index name on Table name ( Field );
alter table Table name add index Index name ( Field );
create table Table name ( Field .... , index Index name ( Field ));
unique index
create unique index XXX index on Table name ( Field );
alter table Table name add unique xxx index Index name ( Field );
create table Table name ( Field ....,unique xxx index( Field ));
primary key
alter table Table name add primary key( Field );
create table Table name ( Field ...…,primary key( Field ));
Composite index ( Single column 、 Multi column index )
create index XXx index on Table name ( Field 1, Field 2,.., Field n);
alter table Table name add index XXx_index( Field 1, Field 2,.…, Field n);
Pay attention to where The leftmost principle of
select*from Table name where Field 1=xxx and Field 2=xxx and....
Full-text index
create fulltext index on Table name ( Field );
alter table Table name add fulltext XXX index( Field );
create table Table name ( Field ...,fulitext xxx index( Field ));
Fuzzy query
select * from Table name where match( Field )against(' Query string ');
Query index
show index from Table name ;
show keys from Table name ;
show create table Table name ; Only the fields and names of the index can be viewed
版权声明
本文为[[email protected]]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/172/202206211932347286.html
边栏推荐
- 多线程实例代码(demo)
- MySQL数据库---事务
- Asynchronous method understanding (demo with code)
- What is the C language callback function?
- AXI_Bus_Matrix_4x4 设计 - 逻辑设计部分
- LeeCode70 爬楼梯
- Shutter input box assembly
- What is the gateway
- 最详细整理STL之vector基础
- evaluating expression ‘ew.sqlSegment != null and ew.sqlSegment != ‘‘ and ew. mybaties plus问题
猜你喜欢

Unity 模拟手电筒光源探测器,AI攻击范围检测区域,视锥内检测物体,扇形区域检测,圆形区域检测,圆锥区域检测

机器学习之数据处理与可视化【鸢尾花数据分类|特征属性比较】

【并行与分布式计算】10b_MapReduce GFS Implementation

File compilation process

向量與平面交點

Welcome to the markdown editor

Intersection du vecteur et du plan

Cluster 2 - LVS load balancing cluster Dr mode

ASP. Net core creates razor page and uploads multiple files (buffer mode)

Idea has this class but can't find it
随机推荐
产品创新 | 物极必反,回归真实生活的创新社交APP
LeeCode70 爬楼梯
Cluster I -- LVS load balancing cluster NAT mode and LVS load balancing actual deployment
黄金哪些值得注意的技术:资金管理的重要性
函数的声明方式
Problems caused by redis caching scenario
Leecode435 non overlapping interval
Server body 17: simple understanding of memory mapping and shared memory
Xcode插件管理工具Alcatraz
Intersection of vector and plane
Adum1401arwz-rl adenault digital signal isolation module
集群二---LVS负载均衡群集DR模式
UIButton实现左文字右图片
RDKit | 分子所具有的自由基电子数、价电子数
【小程序】通过request实现小程序与后台asp.net的数据json传输(Post协议 图文+代码)
[summary of smart trash cans based on Hetai ht32f52352]
Cocoapods安装(Xcode8.0之后,无限卡在Setting up CocoaPods master repo)
What are the applications of 4.3-inch touch screen intelligent network central control host
String类型转换成List<Integer>
What are some tricks that novice programmers don't know?