当前位置:网站首页>Simply use MySQL index
Simply use MySQL index
2022-07-26 02:28:00 【look-word】
MySQL Indexes
1 What is index
- In the database table , Indexing fields can greatly improve query speed . By making good use of these indexes , Can make MySQL And
Run more efficiently .- If the index is properly designed and used MySQL It's a Lamborghini , So there's no index designed and used MySQL Namely
A man-made tricycle. Take the catalog page of the Chinese Dictionary( Indexes )Let's say , We canPinyin 、 stroke 、 Partial radicalsEtc
record ( Indexes ) Quickly find the words you need
2 Common index classification
| The index name | explain |
|---|---|
| primary key (primary key) | Primary key is a unique index , Each table can only have one primary key , Used to identify each record in the data table |
| unique index (unique) | The unique index refers to All of the index columns Values can only appear once , Must be unique . |
| General index (index) | The most common index , Role is Speed up data Access speed |
MySql Save the indexes of a table in the same index file , If you add, delete or modify the data in ,MySql Will automatically change
New index .
2.1 primary key (PRIMARY KEY)
characteristic : Primary key is a unique index , Each table can only have one primary key , Used to identify a record in the data table .
- A table can have no primary key , But there can only be one primary key at most , also
The primary key value cannot contain NULL.
- Create a database table with only fields
demo01
CREATE TABLE demo01(
did INT,
dname VARCHAR(20),
hobby VARCHAR(30)
);
Create primary key index
- Grammar format
- When creating a table, directly add the primary key index ( The most commonly used )
CREATE TABLE Table name (
-- Add primary key ( A primary key is a unique index , Not for null, Can't repeat ,)
Field name type PRIMARY KEY,
);
- Modify table structure Add primary key index
ALTER TABLE Table name ADD PRIMARY KEY ( Name )
- by demo1 Add primary key index to the table
ALTER TABLE demo01 ADD PRIMARY KEY ( did )
2.2 unique index (UNIQUE)
characteristic : All values of an index column can only appear once , Must be unique .
- Unique index can ensure the uniqueness of data records . in fact , on many occasions , People often do not create unique indexes for Improve access speed , Just to avoid data duplication .
Create unique index
Grammar format
- When creating a table, directly add the primary key index
CREATE TABLE Table name (
Name type ( length ),
-- Add unique index
UNIQUE [ The index name ] ( Name )
);
- Use create Sentence creation : Create an index on an existing table
create unique index Index name on Table name ( Name ( length ))
- Modify table structure to add index
ALTER TABLE Table name ADD UNIQUE ( Name )
- by hobby Add a unique index to the field
create unique index ind_hobby on demo01(hobby)

test
- Insert data into table
INSERT INTO demo01 VALUES(1,' Zhang San ','DBJ');
# Report errors Duplicate entry 'DBJ' for key 'hobby'
# The unique index ensures the uniqueness of data , The efficiency of indexing has also improved
INSERT INTO demo01 VALUES(2,' Li Si ','DBJ');

3 General index (INDEX)
General index( By keyword KEY or INDEX Index of definitions ) Your only task isSpeed up access to data.- therefore , It should only be for those that most often appear in query conditions (WHERE column=) Or sort by (ORDERBY column) Create data columns in
Indexes .
Create a normal index
Grammar format
- Use create index Sentence creation : Create an index on an existing table
create index Index name on Table name ( Name [ length ])
- Modify table structure to add index
ALTER TABLE Table name ADD INDEX Index name ( Name )
- to dname Add index to field
# to dname Add index to field
alter table demo01 add index dname_indx(dname);
4 Delete index
- Because the index will occupy a certain amount of disk space , therefore , In order to avoid affecting the performance of the database , You should delete the unused cables in time
lead
- Grammar format
ALTER TABLE table_name DROP INDEX index_name;
- Delete demo01 The name in the table is dname_indx The general index of
ALTER TABLE demo01 DROP INDEX dname_indx;
3 Index performance test
1 Import data table
Download tests sql
Find... Under the software folder Test index .sql file , Click on the perform

The following problems occurred during operation:
Err 1153 - Got a packet bigger than ‘max_allowed_packet’ bytes
Because of the large amount of data ,mysql It can be used for single table data with large amount SQL Make restrictions ,10w The string of data exceeds max_allowed_packet
The allowable range of .

terms of settlement


find :max_allowed_packet Sure ctrl+f Search for Change the default value to 419430400 Probably 400M
Check whether we have successfully modified

Let's get down to business
- Inquire about test_index The total number of records in the table · Table has
500 Ten thousand data
SELECT COUNT(*) FROM test_index;

- test
Without adding an index , Use dname Field to query
# Index not added , Group query
SELECT * FROM test_index GROUP BY dname;

by dname Field add normal index
- Be careful : Generally, we are creating tables Determine the fields that need to be indexed
# Add index
ALTER TABLE test_index ADD INDEX dname_indx(dname);

4 Summary of the advantages and disadvantages of the index
- Adding an index should first be considered in where And order by Index the columns involved .
- The advantages of indexing
- Greatly improve the query speed
- The sorting time and grouping in the query can be significantly reduced .
- Disadvantages of indexes
- Indexing takes time to create and maintain , And the larger the amount of data, the longer the time
- When adding data in the table , modify , When deleting , The index should also be maintained at the same time , Reduced data maintenance
Speed
边栏推荐
猜你喜欢

Li Kou 148: sorting linked list

1. Mx6ul core module uses serial EMMC read / write test (IV)

(Dynamic Programming Series) sword finger offer 48. the longest substring without repeated characters

Handling process of the problem that the virtual machine's intranet communication Ping fails

博云容器云、DevOps 平台斩获可信云“技术最佳实践奖”

2. Login - verification code function and saving login status

Information System Project Manager - Chapter 10 communication management and stakeholder management examination questions over the years

Audio and video technology development weekly | 254

MySQL建Websites数据表

Sword finger offer 28. symmetric binary tree
随机推荐
1. Mx6ul core module uses serial can and buzzer test (XI)
numpy.sort
信息系统项目管理师---第十章沟通管理和干系人管理历年考题
1. Mx6ul core module use serialization - view system information (II)
我来图书馆小程序一键签到和一键抢位置工具
Implementation of Ti am335x industrial control module network and file system nfs
Exclusive interview with ringcentral he Bicang: empowering future mixed office with innovative MVP
项目管理:精益管理法
eslint常见报错集合
1. Mx6ul core module serial use - touch screen calibration (IX)
记录之目标检测NMS(非极大值抑制)
Business Intelligence BI full analysis, explore the essence and development trend of Bi
19_请求表单与文件
图解B+树的插入过程
Keil's operation before programming with C language
What does the Red Cross in the SQL editor mean (toad and waterdrop have been encountered...)
Is it safe to open galaxy securities account by mobile phone?
Master-slave replication in MySQL
微信小程序解密并拆包获取源码教程
npm link的简单介绍及使用