簡體   English   中英

什么是 MySQL 相當於 PostgreSQL 的 EXPLAIN ANALYZE

[英]What is the MySQL equivalent of PostgreSQL's EXPLAIN ANALYZE

我想在 MySQL 中獲得一個詳細的查詢計划,類似於 PostgreSQL 中的 EXPLAIN ANALYZE 顯示。 有等價物嗎?

編輯:雖然不是直接等效或像解釋分析那樣詳細,但您可以查看一些工具

mysql 提供 EXPLAIN 和過程 analysis()
http://dev.mysql.com/doc/refman/5.0/en/explain.html
http://dev.mysql.com/doc/refman/5.0/en/procedure-analysis.html

在 MySQL 具有EXPLAIN EXTENDED之前,我沒有使用過 PostgreSQL,它提供的信息比EXPLAIN並且可能為您提供您正在尋找的信息。

EXPLAIN EXTENDED

MariaDB/MySQL 提供了一種叫做EXPLAIN EXTENDED東西。 但是EXPLAIN ANALYZE是無可替代的。 EXPLAIN EXTENDED提供任何時間信息,內部分解也沒有那么冗長。

Name: 'EXPLAIN'
Description:
Syntax:
EXPLAIN [explain_type] SELECT select_options

explain_type:
    EXTENDED
  | PARTITIONS

Or:

EXPLAIN tbl_name

The EXPLAIN statement can be used either as a way to obtain information
about how MySQL executes a statement, or as a synonym for DESCRIBE:

o When you precede a SELECT statement with the keyword EXPLAIN, MySQL
  displays information from the optimizer about the query execution
  plan. That is, MySQL explains how it would process the statement,
  including information about how tables are joined and in which order.
  EXPLAIN EXTENDED can be used to obtain additional information.

  For information about using EXPLAIN and EXPLAIN EXTENDED to obtain
  query execution plan information, see
  https://mariadb.com/kb/en/explain/.

o EXPLAIN PARTITIONS is useful only when examining queries involving
  partitioned tables. For details, see
  http://dev.mysql.com/doc/refman/5.5/en/partitioning-info.html.

o EXPLAIN tbl_name is synonymous with DESCRIBE tbl_name or SHOW COLUMNS
  FROM tbl_name. For information about DESCRIBE and SHOW COLUMNS, see
  [HELP DESCRIBE], and [HELP SHOW COLUMNS].

URL: https://mariadb.com/kb/en/explain/

例如,這是從這個例子中獲取的

EXPLAIN ANALYZE SELECT *
FROM history AS h1
WHERE EXISTS (
  SELECT 1
  FROM history AS h2
  WHERE h1.lead_id = h2.lead_id
  GROUP BY lead_id
  HAVING count(is_first OR NULL) > 1
);

會在 PostgreSQL 上產生這樣的東西,

                                                     QUERY PLAN                                                     
--------------------------------------------------------------------------------------------------------------------
 Seq Scan on history h1  (cost=0.00..82680.50 rows=1100 width=9) (actual time=0.048..0.065 rows=3 loops=1)
   Filter: (SubPlan 1)
   Rows Removed by Filter: 3
   SubPlan 1
     ->  GroupAggregate  (cost=0.00..37.57 rows=1 width=5) (actual time=0.007..0.007 rows=0 loops=6)
           Group Key: h2.lead_id
           Filter: (count((h2.is_first OR NULL::boolean)) > 1)
           Rows Removed by Filter: 0
           ->  Seq Scan on history h2  (cost=0.00..37.50 rows=11 width=5) (actual time=0.003..0.004 rows=2 loops=6)
                 Filter: (h1.lead_id = lead_id)
                 Rows Removed by Filter: 4
 Planning time: 0.149 ms
 Execution time: 0.123 ms
(13 rows)

雖然這是 MySQL 的等價物,

+------+--------------------+-------+------+---------------+------+---------+------+------+----------+-------------+
| id   | select_type        | table | type | possible_keys | key  | key_len | ref  | rows | filtered | Extra       |
+------+--------------------+-------+------+---------------+------+---------+------+------+----------+-------------+
|    1 | PRIMARY            | h1    | ALL  | NULL          | NULL | NULL    | NULL |    6 |   100.00 | Using where |
|    2 | DEPENDENT SUBQUERY | h2    | ALL  | NULL          | NULL | NULL    | NULL |    6 |   100.00 | Using where |
+------+--------------------+-------+------+---------------+------+---------+------+------+----------+-------------+
2 rows in set, 2 warnings (0.00 sec)

MySQL 8.0.18 原生引入了EXPLAIN ANALYZE

MySQL 8.0.18 引入了 EXPLAIN ANALYZE ,它運行一個查詢並產生 EXPLAIN 輸出以及時間和額外的、基於迭代器的關於優化器的期望如何匹配實際執行的信息。 對於每個迭代器,提供以下信息:

  • 估計執行成本

  • 估計的返回行數

  • 返回第一行的時間

  • 返回所有行的時間(實際成本)

  • 迭代器返回的行數

  • 循環次數

    EXPLAIN ANALYZE 只能與 SELECT 語句一起使用。

只是為了清楚起見,對接受的答案發表評論(沒有足夠的業力來添加評論)

過程 analysis() 與 EXPLAIN 的目的不同,它分析指定列的數據集並建議最好的數據類型,即當我們有 1000 行 varchar(255) 並想檢查我們真的有多少長度時它很有用需要,它可能會告訴 varchar(23) 就足夠了

2020 年更新解釋EXPLAIN ANALYZE可用

老問題,但只是為了更新, MySQL 8.0.18Explain Analyze也可用,您可以像下面這樣使用它:

mysql> explain analyze select count(*) from sbtest1 where k > 500000\G
*************************** 1. row ***************************
EXPLAIN: -> Aggregate: count(0) (actual time=178.225..178.225 rows=1 loops=1)
-> Filter: (sbtest1.k > 500000) (cost=98896.53 rows=493204) (actual time=0.022..147.502 rows=625262 loops=1)
-> Index range scan on sbtest1 using idx3 (cost=98896.53 rows=493204) (actual time=0.021..96.488 rows=625262 loops=1)

1 row in set (0.18 sec)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM