# 2. 定位执行效率较低的 SQL 语句

可以通过以下两种方式定位执行效率较低的 SQL 语句：

* 通过<mark style="color:blue;">**慢查询日志**</mark>定位那些执行效率较低的 SQL 语句，用 <mark style="color:blue;">**--log-slow-queries\[= file\_name]**</mark> 选项启动时，mysqld 会将执行时间超过 <mark style="color:blue;">**long\_query\_time**</mark> 秒的 SQL 语句的写入指定的日志文件中。
* 慢查询日志在查询结束以后才记录，所以在应用反映执行效率出现问题的时候查询慢查询日志并不能定位问题，可以使用 <mark style="color:orange;">**show processlist**</mark> 命令查看当前 MySQL 正在进行中的线程，包括线程的状态、是否锁表等，可以实时地查看 SQL 的执行情况，同时对一些锁表操作进行优化。

## 查询的状态

对于一个 MySQL 连接，或者一个线程，任何时刻都有一个状态，<mark style="color:blue;">**该状态表示了 MySQL 当前正在做什么**</mark>。有很多种方式能查看当前的状态，最简单的是使用 <mark style="color:blue;">**SHOW \[ FULL ] PROCESSLIST**</mark> 命令（该命令返回结果中的 Command 列，其就表示当前的状态）。**在一个查询的生命周期中，状态会变化很多次**。

<table><thead><tr><th width="303">状态</th><th>描述</th></tr></thead><tbody><tr><td><mark style="color:blue;"><strong>Sleep</strong></mark></td><td>线程正在<strong>等待客户端发送新的请求</strong>。</td></tr><tr><td><mark style="color:blue;"><strong>Query</strong></mark></td><td>线程<strong>正在执行查询</strong>或者<strong>正在将结果发送给客户端</strong>。</td></tr><tr><td><mark style="color:blue;"><strong>Locked</strong></mark></td><td><p>在 MySQL 服务器层，该线程<strong>正在等待</strong><mark style="color:blue;"><strong>表锁</strong></mark>。</p><p><mark style="color:orange;"><strong>在存储引擎级别实现的锁，例如 InnoDB 的行锁，并不会体现在线程状态中。</strong></mark></p></td></tr><tr><td><mark style="color:blue;"><strong>Analyzing and statistics</strong></mark></td><td>线程<strong>正在检查存储引擎的统计信息</strong>，并优化查询。</td></tr><tr><td><mark style="color:blue;"><strong>Copying to tmp table [on disk]</strong></mark></td><td>线程正在执行查询，并且<strong>将其结果集复制到一个临时表中</strong>，这种状态一般要么是在做 <mark style="color:orange;"><strong>GROUP BY</strong></mark> 操作，要么是在进行<mark style="color:orange;"><strong>文件排序</strong></mark>操作，或者是在进行 <mark style="color:orange;"><strong>UNION</strong></mark> 操作。<br>如果这个状态后面还有“<strong>on disk</strong>”标记，那表示 MySQL <strong>正在将一个内存临时表放到磁盘上</strong>。</td></tr><tr><td><mark style="color:blue;"><strong>Sorting result</strong></mark></td><td>线程<strong>正在对结果集进行排序</strong>。</td></tr></tbody></table>

```sql
SHOW FULL PROCESSLIST;

+-------+-------------------------------------------------------------+
|Id     |7                                                            |
+-------+-------------------------------------------------------------+
|User   |root                                                         |
+-------+-------------------------------------------------------------+
|Host   |localhost:65084                                              |
+-------+-------------------------------------------------------------+
|db     |sakila                                                       |
+-------+-------------------------------------------------------------+
|Command|Query                                                        |
+-------+-------------------------------------------------------------+
|Time   |0                                                            |
+-------+-------------------------------------------------------------+
|State  |starting                                                     |
+-------+-------------------------------------------------------------+
|Info   |/* ApplicationName=DataGrip 2021.1.3 */ SHOW FULL PROCESSLIST|
+-------+-------------------------------------------------------------+
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://bohans.gitbook.io/ji-chu/mysql/sql-you-hua/cha-xun-you-hua/you-hua-sql-yu-ju-de-yi-ban-bu-zhou/2.-ding-wei-zhi-xing-xiaoljiao-di-de-sql-yu-ju.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
