Home Database Mysql Tutorial Introduction to mysql slow query and EXPLAIN

Introduction to mysql slow query and EXPLAIN

May 12, 2017 am 11:04 AM

Preface: Today, database operations have increasingly become the performance bottleneck of the entire application, which is especially obvious for Web applications. Regarding the performance of the database, this is not just something that DBAs need to worry about, but this is something that we programmers need to pay attention to. When we design the database table structure and operate the database (especially SQL statements when looking up tables), we need to pay attention to the performance of data operations.

1. Enable slow query

1> 查看慢查询是否开启
   show variables like "%quer%";
   slow_query_log = ON  #已开启
Copy after login

2> Enable method: my.cnf directory configuration

  slow_query_log=on #是否开启
   slow_query_log_file=/opt/MySQL_Data/TEST1-slow.log #慢查询文件位置
   long_query_time=2 #查询超过多少秒才记录
Copy after login

2. EXPLAIN SELECT query appearing in slow query log

explain column explanation

table: Shows which table the data in this row refers to

type: This is an important column, showing what type of connection is used. The join types from best to worst are const, eq_reg, ref, range, index, all

possible_keys: Displays the indexes that may be applied to this table. If empty, no index is possible. You can choose an appropriate statement from the where statement for the relevant domain

key: The actual index used. If null, no index is used. Rarely, MySQL will select an index that is under-optimized. In this case, you can use use index (indexname) in the select statement to force the use of an index or use ignore index (indexname) to force MySQL to ignore the index

key_len: The length of the index used. Without losing accuracy, the shorter the length the better

ref: Shows which column of the index is used, if possible, a constant

rows: mysql thinks it must be checked The number of rows used to return the requested data

extra: Extra information about how MySQL parses the query. Example: using temporary and using filesort, which means that mysql cannot use the index at all, and the result is that the retrieval will be very slow

[Related recommendations]

1. Free mysql online video tutorial

2. MySQL latest manual tutorial

3. Those things about database design

The above is the detailed content of Introduction to mysql slow query and EXPLAIN. 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 AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

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)

Redis slow query troubleshooting and optimization methods Redis slow query troubleshooting and optimization methods Jun 20, 2023 am 11:40 AM

Redis is a high-performance, scalable memory data structure storage system that is widely used in data caching, message queues, distributed locks and other fields. In Redis application process, slow query is a common performance problem. When the response time of Redis processing requests exceeds the specified threshold, it can be called a slow query. Slow queries may cause Redis performance to degrade and affect application stability and reliability. In order to ensure the high availability and reliability of Redis, we need to troubleshoot slow queries from multiple aspects and

What is the meaning of key_len in mysql explain and how to calculate it What is the meaning of key_len in mysql explain and how to calculate it Jun 02, 2023 am 09:52 AM

Preface: One of the columns in the result of executing explain in Mysql is key_len. So what is the meaning of key_len? key_len: Indicates the number of bytes used by the index. Based on this value, the usage of the index can be judged. Especially when combining indexes, it is very important to judge how many parts of the index are used. Notes on calculating key_len: Additional information about the index field: it can be divided into variable-length and fixed-length data types for discussion. When the index field is a fixed-length data type, such as char, int, datetime, a mark is required to indicate whether it is empty. This mark Occupies 1 byte (for notnull fields, this 1 byte is not needed); for variable-length data types, such as v

MySql's behavior log and slow query: how to quickly locate performance problems MySql's behavior log and slow query: how to quickly locate performance problems Jun 15, 2023 pm 09:01 PM

With the rapid development of application systems, how to effectively solve system performance problems has become increasingly important. For applications that use MySql database, behavior logs and slow queries are two important tools for locating performance problems. This article will introduce MySql's behavior logs and slow queries, and discuss how to use these two tools to quickly locate performance problems. 1. MySql behavior log MySql's behavior log is a log that records all operations performed by the database server. By looking at the behavior log, we can learn

What are the attributes of mysql explain? What are the attributes of mysql explain? Jun 01, 2023 pm 10:18 PM

1. table The table to be queried 2. type index query type, from best to worst: system>const>eq_ref>ref>range>index>ALL. Generally speaking, ensure that the query reaches at least the range level, and preferably reaches the ref. 3. possible_keys displays one or more indexes that may be applied to this table. If there is an index for the field involved in the query, the index will be listed, but it may not be actually used by the query. 4. The index actually used by the key. If it is NULL, the index is not used. If a covering index is used in the query, the index Only appears in the key list 5. key

Example analysis of usage records of pt-query-digest tool in MySQL Example analysis of usage records of pt-query-digest tool in MySQL Nov 30, 2022 pm 05:07 PM

This article brings you relevant knowledge about mysql. It mainly introduces a tool pt-query-digest for analyzing mysql slow query logs. Let's take a look at it together. I hope it will be helpful to you.

Database slow query optimization practice: application in PHP programming Database slow query optimization practice: application in PHP programming Jun 23, 2023 am 08:37 AM

Database Slow Query Optimization Practice: Application in PHP Programming As a PHP programmer, you often encounter the problem of low database query efficiency during the development process, which is the so-called "slow query". If not optimized, these slow queries can cause programs to run slowly and create a poor user experience. This article mainly introduces several optimization ideas and practical techniques to help PHP programmers solve the problem of slow queries. 1. Reduce the number of database queries as much as possible. Reducing the number of database queries is an important way to improve system efficiency. In actual development, we

How to avoid slow queries in PHP language development How to avoid slow queries in PHP language development Jun 11, 2023 am 09:10 AM

With the rapid development of the Internet, reliability and efficiency have become one of the most important issues for developers. For PHP language developers, solving slow queries has become one of the common problems. The emergence of slow queries will cause the website to respond slowly, which will affect the user experience and may even lead to data security issues. To this end, this article will introduce how to avoid slow queries in PHP development. 1. What is slow query? In the MySQL database, the phenomenon of a query being slower than expected is called a slow query. Slow queries not only affect the performance of your website;

What does mysql's Explain command do? What does mysql's Explain command do? May 31, 2023 pm 12:33 PM

Concept 1. Explain is used to analyze the SELECT query statement. Developers can optimize the query statement by analyzing the Explain results. Function 2. Through the explain command, we can learn how the SQL is executed, and then parsing the explain results can help us use better indexes and ultimately optimize it! Through the explain command, we can know the following information: the reading order of the table, the type of data reading operation, which indexes can be used, which indexes are actually used, references between tables, how many rows of each table are queried by the optimizer, etc. information. Example explainselect*fromtable_user;

See all articles