MySQL database - index (3) - index syntax (create index, view index, delete index, case demonstration), SQL performance analysis (SQL execution frequency, slow query log)

Table of contents

Index syntax

Create index

View index

Delete index

Case presentation

SQL performance analysis

SQL execution frequency

Slow query log


Index syntax

Create index

CREATE [ UNIQUE | FULLTEXT ] INDEX index_name ON table_name (index_col_name,... ) ;

View index

SHOW INDEX FROM table_name ;

Delete index

DROP INDEX index_name ON table_name ;


Case presentation

First create a table tb_user and query the test data. The data inserted in the table structure is as follows:

After the data is ready, next, we will complete the following requirements:

  1. The name field is the name field. The value of this field may be repeated. Create an index for this field.
  2. The value of the phone number field is non-empty and unique. Create a unique index for this field.
  3. Create joint indexes for profession, age, and status .
  4. Establish appropriate indexes for emails to improve query efficiency.
  5. View all index data in the tb_user table.

 1. Just create the index directly.

CREATE INDEX idx_user_name ON tb_user(name);

2. Create a unique index. (Add the keyword unique in front of index)

CREATE UNIQUE INDEX idx_user)phone ON tb_user(phone);

3. Create a joint index. In the joint index, the order of each field has an impact (just for understanding now)

CREATE INDEX idx_user_pro_age_sta ON tb_user(profession,age,status);

4. The email field here is suitable for single column index.

CREATE INDEX idx_email ON tb_user(email); 

5. View all index data in the tb_user table.

show index from tb_user;

SQL performance analysis (Part 1)

SQL execution frequency

After the MySQL client is successfully connected, server status information can be provided through the show [session|global] status
command . Through the following commands, you can check the access frequency of INSERT, UPDATE, DELETE, and SELECT ( add, modify, delete, check) of the current database .

-- session 是查看当前会话 ;
-- global 是查询全局数据 ;
SHOW GLOBAL STATUS LIKE 'Com_______';
-- 七个下划线

Com_insert: number of insertions
Com_select: number of queries
Com_update: number of updates

Through the above instructions, we can check whether the current database is mainly based on queries or based on additions, deletions and modifications, thereby providing a reference for database optimization.

If it is mainly about additions, deletions and modifications, we can consider not optimizing the index.

If it is mainly about query, then you need to consider optimizing the index of the database.

 

So if it is mainly about queries, how should we position and optimize those query statements? times we can resort to slow query logs.

Slow query log

(in Linux system)

The slow query log records the logs of all SQL statements whose execution time exceeds the specified parameter ( long_query_time , unit: seconds, default 10 seconds) . MySQL's slow query log is not enabled by default. We can check the system variable slow_query_log.

If you want to enable slow query logs, you need to configure the following information in the MySQL configuration file (/etc/my.cnf):

# 开启MySQL慢日志查询开关
slow_query_log=1
# 设置慢日志的时间为2秒,SQL语句执行时间超过2秒,就会视为慢查询,记录慢查询日志
long_query_time=2

After the configuration is complete, use the following command to restart the MySQL server for testing and check the information recorded in the slow log file
/var/lib/mysql/localhost-slow.log.

systemctl restart mysqld;
-- 在Linux系统中重启MySQL

At this time, check the switch status again, and the slow query log is turned on.


END


Learn from: Dark Horse Programmer - MySQL Database Course

Guess you like

Origin blog.csdn.net/li13437542099/article/details/133045197