当前位置:网站首页>MySQL Index & execution plan
MySQL Index & execution plan
2022-07-24 06:29:00 【zsm030616】
One , Indexes
1. What is index
An index is a combination of one or more columns in a database table , Its function is to improve the query speed of data in the table .
A catalogue similar to a book , Easy and fast positioning , Find the specified content .
2. Advantages and disadvantages of index
advantage : Speed up data query .
shortcoming : The time to create and maintain indexes has increased , At the same time, it takes up hard disk space .
3. Index classification
General index : Is the most basic index , It has no restrictions ;
unique index : Similar to the previous normal index , The difference is that : The value of the index column must be unique , But you can have an empty value . If it's a composite index , The combination of column values must be unique ;
primary key : Is a special unique index , A table can only have one primary key , No null values are allowed ;
Composite index : An index created on multiple fields , Only the first field that created the index is used in the query criteria , Indexes are used . When using a composite index, follow the leftmost prefix set ;
Full-text index : Use FULLTEXT Parameters can be set , Full text index can only be created in CHAR,VARCHAR,TEXT Type field on . The main function is to improve the speed of querying larger string types ; Only MyISAM The engine supports the index ,MySQL The default engine does not support ;mysql5.7+
4. Create index
CREATE [UNIQUE|FULLTEXT] INDEX Index name ON Table name ( Field name [( length )][ASC|DESC])
5. Modify the index
ALTER TABLE Table name ADD [UNIQUE|FULLTEXT] INDEX Index name ( Field name [( length )][ASC|DESC])
6. Delete index
DROP INDEX Index name ON Table name
7. Case study
Additional explanation :
Bulk import :LOAD DATA INFILE( recommend )
You can go through first SELECT INTO OUTFILE The way , Export data to Mysql Of C:\ProgramData\MySQL\MySQL Server 5.5\data Under the table of contents , Re pass LOAD DATA INFILE Mode import .
- select * from Table name into outfile ‘/ file name .sql’;
- load data infile ‘/ file name .sql’ into table Table name ( Name 1,…);
1) Common index cases
create index Index name on Table name ( Field name )
select * from t_log where moduleid='100401666';
create index idx_moduleid on t_log(moduleid);
2) Unique index case ( The corresponding value cannot be the same )
create unique index Index name on Table name ( Field name )
create unique index idx_url on t_log(url)
3) Primary key index case
create table Table name (
id int not null auto_increment primary key,
...
)
-- use id Query efficiency will be faster
select * from t_log where id='212121213131'
4) Combined index cases The leftmost segment matching principle
create index Index name on Table name ( Field name 1, Field name 2,...)
Some fields in brackets will match the index
create index idx_userid_moduleid_url on t_log(userid,moduleid,url);
EXPLAIN SELECT * from t_log -- This line of code does not execute the index ( Because there is no matching index )
EXPLAIN SELECT * from t_log where userid='';
EXPLAIN SELECT * from t_log where userid='' and moduleid='';
EXPLAIN SELECT * from t_log where url='';
Two , Import and export
Add :delete And turncate The difference between
delete Time consuming 6. More than a second
delete from t_log ----- Long time consuming
TRUNCATE Time consuming 0.147 second ( Fast efficiency )
The index will be deleted at the same time
TRUNCATE TABLE t_log# 3、 ... and , View
1. Using tools
( similar Sqlyog、Navicat Wait to import and export data ) Longer time consuming
step :
Right click Run SQL file
Select the file you want to import
Long import time 
If there is no connection tool or the connection tool cannot be used under certain circumstances , Then we can import and export data by command
– notes : Short time , Efficient
2. Use mysqldump Import and export
(1) export
1.1 Export table data and table structure
mysqldump -u user name -p password Database name > Database name .sql ( The name is optional )
There is no need to enter the client when executing commands , But the database user and password are required to execute the command
Execute statement :#/usr/local/mysql/bin mysqldump -uroot -pabc >abc.sql
After typing enter, you will be prompted to output the password
1.2 Export only table structure
mysqldump -u user name -p password -d Database name > Database name .sql
#mysqldump -uroot -p -d abc > abc.sql
notes : The exported data is in mysql Of bin Under the table of contents
(2) Import
Be careful : First, create an empty database
mysql>create database abc;
2.1 Method 1
mysql>use abc; # Select database
mysql>set names utf8; # Set database encoding
mysql>source /home/abc/abc.sql; # Import data
2.2 Method 2
mysql -u user name -p password Database name < Database name .sql
#mysql -uabc_f -p abc < abc.sql
3.LOAD DATA INFILE
You can go through first SELECT INTO OUTFILE The way , Export data to Mysql Of C:\ProgramData\MySQL\MySQL Server 5.5\data Under the table of contents , Re pass LOAD DATA INFILE Mode import .
- select * from Table name into outfile ‘/ file name .sql’;
- load data infile ‘/ file name .sql’ into table Table name ( Name 1,…);
select * from t_log into outfile ‘E:/989898.sql’;
select * from t_log into outfile ‘D:/989898.sql’;
load data infile ‘E:/989898.sqll’ into table t_log(id,ip,userid,moduleid,content,createdate,url);
summary : Take less time
3、 ... and , View
1. What is a view
(1) A view is a virtual table , A table derived from one or more tables in a database .
(2) The definition of view is stored in the database , There is no data stored in view , The data is stored in the original table .
(3) When using views to query data , The database system will take the corresponding data from the original table .
2. The function of view
(1) Make the operation simple .
(2) Increase data security .
(3) Improve the logical independence of tables .
3. Basic grammar
sentence :CREATE VIEW View name AS SELECT
Tips : The premise of successful view creation is that duplicate columns cannot appear
for example :CREATE VIEW
CREATE VIEW v_student as
select s.*,sc.score,c.*,t.tname from t_student s,t_score sc,t_course c,t_teacher t
where s.sid=sc.sid and sc.cid-c.cid and c.tid=t.tid
select * from v_student
Four , Implementation plan
Example 1 :explain
EXPLAIN select * from t_user
EXPLAIN select * from t_user u left join t_role r on u.roleid = r.roleid
EXPLAIN select * from t_user u where u.roleid = (select roleid from t_role where rolename = ' Administrators ')
Example 2 :id
id identical , From top to bottom
Purpose : Lies in knowing sql What is the execution step of the statement left outreach
EXPLAIN select * from t_user u left join t_role r on u.roleid = r.roleid
Right outreach
EXPLAIN select * from t_user u right join t_role r on u.roleid = r.roleid
Example 3 :select_type
(1)SIMPLE( Simple SELECT, Don't use UNION Or subquery, etc )
EXPLAIN select * from t_user
(2)PRIMARY( If the query contains any complex sub parts , The outermost select Marked as PRIMARY)
SUBQUERY( First in subquery SELECT)
EXPLAIN select * from t_user u where u.roleid = (select roleid from t_role where rolenmae = ' Administrators ')
(3)UNION(UNION The second or later of SELECT sentence )
UNION RESULT: from union Table to get the result SELECT
EXPLAIN select * from t_user where id=3
union
select * from t_user where roleid = 3
Example 4 : Use index correctly
(1) Use like When the sentence is ,% The index is only used on the right .
It works
Improve query efficiency
EXPLAIN select * from t_user where username like ' Zhang '
(2)or The index will become invalid only if there is an unindexed column in the condition
Invalid
EXPLAIN select * from t_user where password = 123456 or username=' Zhang San '
It works
EXPLAIN select * from t_user where password = 123456 and username=' Zhang San '
(3) The type of condition is inconsistent
Invalid
EXPLAIN select * from t_user where username = 1
It works
EXPLAIN select * from t_user where username = '1'
边栏推荐
- 【222】内存溢出及定位
- Mysql database - SQL summary (remember to pay attention to me! Come on in China!)
- LVM与磁盘配额
- Ia class summary (2)
- Unity (III) three dimensional mathematics and coordinate system
- Leetcode sword finger offer JZ9 dual stack implementation queue
- [251] common test tools
- IP笔记(11)
- Leetcode剑指offer JZ9 双栈实现队列
- Flink restart policy
猜你喜欢

ip作业(1)

Website B video is embedded in the web page, and relevant controls are hidden

IP笔记(12)

Flink production environment configuration recommendations

【214】什么是自动化框架

IP course (OSPF) comprehensive experiment

A batch of interview questions and answers_ 20180403 latest arrangement

【222】内存溢出及定位

IA笔记 1

IP作业(2)RIP
随机推荐
Server hardware and RAID configuration practice
Set up a WordPress personal blog locally and launch it through the intranet (22)
IP课笔记(4)
List of problems in the re disk guidance of the project
sed命令
利用sshpass批量实现基于key验证
进程和计划任务管理
leetcode剑指offer JZ23:链表中环的入口节点
IP job (2) rip
Flink function (2): checkpointedfunction
CentOS操作系统安全加固
ip作业(1)
Flink state use
Leetcode does not add, subtract, multiply, divide, and calculate the number of 1 in binary
Use and principle of spark broadcast variable and accumulator
Dameng database_ Small matters needing attention during use
Mysql database - SQL summary (remember to pay attention to me! Come on in China!)
Do not rent servers, build your own personal business website (2)
leetcode剑指offer JZ42 连续子数组的最大和
IP笔记(12)