Table of Contents
mysql> show index from employee\G;//主键会默认建一个id索引
Copy after login
" >
mysql> show index from employee\G;//主键会默认建一个id索引
Copy after login
Home Database Mysql Tutorial Examples introducing the use of MySQL indexes

Examples introducing the use of MySQL indexes

Dec 11, 2020 pm 05:34 PM
mysql index

mysql tutorial Column examples explain the use of MySQL indexesExamples introducing the use of MySQL indexes

##More related free learning recommendations: mysql Tutorial(Video)

Usage examples of MySQL index

    • 1. Slow query log
    • 2. Query Analyzer——explain
    • 3. Basic use of index
    • 4. Composite index
    • 5. Covering index
1. Slow query log

//查看是否开启慢查询日志
mysql> show variables like '%slow%';//临时开启慢查询日志
mysql> set global slow_query_log=ON;//查看是否开启慢查询日志
mysql> show variables like '%slow%';
Copy after login

Examples introducing the use of MySQL indexes

//查询超过多少时间就可以记录,上面是如果超过10秒就要记录
mysql> show variables like '%long%';//改成一秒,如果超过一秒就写到慢日志里面去(一般一秒是最好的)mysql> set long_query_time=1;//查看日记存储方式,默认FILE
mysql> show variables like '%log_output%';// 慢查询日志文件所在位置
mysql> show variables like '%datadir%';
Copy after login

Examples introducing the use of MySQL indexes

//响应时间是3秒,超过了原先设定的一秒
mysql> select sleep(3);
Copy after login

Examples introducing the use of MySQL indexes We When I went to check the folder, I found that it had been stored in the slow query log

Examples introducing the use of MySQL indexes

This part explains how to find out through the slow log

is slower SQL, the following part will talk about why it is slow and how to make it faster.

2. Query Analyzer - explain

Function: Through this, you can know where the sql is slow and what aspects need to be optimized

columns : We create a

employee data table

create table employee(
	id int not null auto_increment primary key,
	name varchar(30) comment '姓名',
	sex varchar(1) comment '性别',
	salary int comment '薪资(元)',
	dept varchar(30) comment '部门');insert into employee(name, sex, salary, dept) values('张三', '男', 5500, '部门A');insert into employee(name, sex, salary, dept) values('李洁', '女', 4500, '部门C');insert into employee(name, sex, salary, dept) values('李小梅', '女', 4200, '部门A');insert into employee(name, sex, salary, dept) values('欧阳辉', '男', 7500, '部门C');insert into employee(name, sex, salary, dept) values('李芳', '女', 8500, '部门A');insert into employee(name, sex, salary, dept) values('张江', '男', 6800, '部门A');insert into employee(name, sex, salary, dept) values('李四', '男', 12000, '部门B');insert into employee(name, sex, salary, dept) values('王五', '男', 3500, '部门B');insert into employee(name, sex, salary, dept) values('马小龙', '男', 6000, '部门A');insert into employee(name, sex, salary, dept) values('龙五', '男', 8000, '部门B');insert into employee(name, sex, salary, dept) values('冯小芳', '女', 10000, '部门C');insert into employee(name, sex, salary, dept) values('马小花', '女', 4000, '部门B');insert into employee(name, sex, salary, dept) values('柳峰', '男', 8800, '部门A');
Copy after login

Examples introducing the use of MySQL indexes

//通过explain解读他,后面加一个\G便于阅读
mysql> explain select * from employee where name='柳峰'\G;//扫描快捷
mysql> explain select * from employee where id=13\G;
Copy after login

Examples introducing the use of MySQL indexes

Effect: As shown below, you can see why it was so slow before , requires four seconds response time

Examples introducing the use of MySQL indexes

3. Basic use of index

mysql> show index from employee\G;//主键会默认建一个id索引
Copy after login

Examples introducing the use of MySQL indexes

Improved index creation efficiency

//查询分析
mysql> explain select * from employee where name='柳峰';//创建普通索引
mysql> create index idx_name on employee(name);
Copy after login

Examples introducing the use of MySQL indexes

//删除
mysql> drop index idx_name on employee;
Copy after login

Examples introducing the use of MySQL indexes Teacher’s list:

Examples introducing the use of MySQL indexes

If you use like to search, the efficiency remains the same, so you have to look How do you use

Examples introducing the use of MySQL indexes

4. Composite index

//查的时候可以看到一个主键索引
mysql> show index from employee\G;
Copy after login

Examples introducing the use of MySQL indexes

Currently it is an all global scan

select * from employee where name ='柳峰';//查询分析
explain select * from employee where name ='柳峰'\G;
Copy after login

Examples introducing the use of MySQL indexes

Create index

//创建索引
create index idx_name_salary_dept on employee(name,salary,dept);//查询分析
explain select * from employee where name ='柳峰'\G;
Copy after login

Examples introducing the use of MySQL indexes

Verify that the index can be indexed if there is a name

// name和salary
mysql> explain select * from employee where name ='柳峰' and salary=8800\G;//name和dept
mysql> explain select * from employee where name ='柳峰' and dept='部门A'\G;
Copy after login

Examples introducing the use of MySQL indexes

No Name cannot use index

mysql> explain select * from employee where  salary=8800;mysql> explain select * from employee where  dept='部门A';
Copy after login

Examples introducing the use of MySQL indexes

5. Covering index

Following the above steps, we can see four indexes, the first one is

Primary key index , followed by compound index name_salary_dept

mysql> show index from employee;
Copy after login

Examples introducing the use of MySQL indexes How to trigger

We use id as query data

mysql> select * from employee;mysql> select * from employee where id =11;
Copy after login

Examples introducing the use of MySQL indexes

Check ID only

mysql> explain select id from employee  employee where id=11\G;mysql> explain select id from employee\G;
Copy after login

Examples introducing the use of MySQL indexes

//查name,salary
mysql> explain select name,salary from employee;//查name,salary,dept
mysql> explain select name,salary,dept from employee;//因为没有sxe条件,所以只能做全部扫描type为null
mysql> explain select name,sex,salary,dept from employee;
Copy after login

Examples introducing the use of MySQL indexes

The above is the detailed content of Examples introducing the use of MySQL indexes. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP's big data structure processing skills PHP's big data structure processing skills May 08, 2024 am 10:24 AM

PHP's big data structure processing skills

How to optimize MySQL query performance in PHP? How to optimize MySQL query performance in PHP? Jun 03, 2024 pm 08:11 PM

How to optimize MySQL query performance in PHP?

How to use MySQL backup and restore in PHP? How to use MySQL backup and restore in PHP? Jun 03, 2024 pm 12:19 PM

How to use MySQL backup and restore in PHP?

How to insert data into a MySQL table using PHP? How to insert data into a MySQL table using PHP? Jun 02, 2024 pm 02:26 PM

How to insert data into a MySQL table using PHP?

What are the application scenarios of Java enumeration types in databases? What are the application scenarios of Java enumeration types in databases? May 05, 2024 am 09:06 AM

What are the application scenarios of Java enumeration types in databases?

How to fix mysql_native_password not loaded errors on MySQL 8.4 How to fix mysql_native_password not loaded errors on MySQL 8.4 Dec 09, 2024 am 11:42 AM

How to fix mysql_native_password not loaded errors on MySQL 8.4

How to use MySQL stored procedures in PHP? How to use MySQL stored procedures in PHP? Jun 02, 2024 pm 02:13 PM

How to use MySQL stored procedures in PHP?

Performance optimization strategies for PHP array paging Performance optimization strategies for PHP array paging May 02, 2024 am 09:27 AM

Performance optimization strategies for PHP array paging

See all articles