新聞中心
執(zhí)行計劃(EXPLAIN)是對一條 SQL 查詢語句在數(shù)據(jù)庫中執(zhí)行過程的描述。

用戶可以通過 EXPLAIN 命令查看優(yōu)化器針對給定 SQL 生成的邏輯執(zhí)行計劃。如果要分析某條 SQL 的性能問題,通常需要先查看 SQL 的執(zhí)行計劃,排查每一步 SQL 執(zhí)行是否存在問題。所以讀懂執(zhí)行計劃是 SQL 優(yōu)化的先決條件,而了解執(zhí)行計劃的算子是理解 EXPLAIN 命令的關(guān)鍵。
EXPLAIN 命令格式
OceanBase 數(shù)據(jù)庫的執(zhí)行計劃命令有三種模式:EXPLAIN BASIC、EXPLAIN 和 EXPLAIN EXTENDED。這三種模式對執(zhí)行計劃展現(xiàn)不同粒度的細節(jié)信息:
-
EXPLAIN BASIC命令用于最基本的計劃展示。 -
EXPLAIN EXTENDED命令用于最詳細的計劃展示(通常在排查問題時使用這種展示模式)。 -
EXPLAIN命令所展示的信息可以幫助普通用戶了解整個計劃的執(zhí)行方式。
命令格式如下:??
EXPLAIN [BASIC | EXTENDED | PARTITIONS | FORMAT = format_name] explainable_stmt
format_name: { TRADITIONAL | JSON }
explainable_stmt: { SELECT statement
| DELETE statement
| INSERT statement
| REPLACE statement
| UPDATE statement }
執(zhí)行計劃形狀與算子信息
在數(shù)據(jù)庫系統(tǒng)中,執(zhí)行計劃在內(nèi)部通常是以樹的形式來表示的,但是不同的數(shù)據(jù)庫會選擇不同的方式展示給用戶。
如下示例分別為 PostgreSQL 數(shù)據(jù)庫、Oracle 數(shù)據(jù)庫和 OceanBase 數(shù)據(jù)庫對于 TPCDS Q3 的計劃展示。
obclient>SELECT /*TPC-DS Q3*/ *
FROM (SELECT dt.d_year,
item.i_brand_id brand_id,
item.i_brand brand,
Sum(ss_net_profit) sum_agg
FROM date_dim dt,
store_sales,
item
WHERE dt.d_date_sk = store_sales.ss_sold_date_sk
AND store_sales.ss_item_sk = item.i_item_sk
AND item.i_manufact_id = 914
AND dt.d_moy = 11
GROUP BY dt.d_year,
item.i_brand,
item.i_brand_id
ORDER BY dt.d_year,
sum_agg DESC,
brand_id)
WHERE rownum <= 100;
-
PostgreSQL 數(shù)據(jù)庫執(zhí)行計劃展示如下:
Limit (cost=13986.86..13987.20 rows=27 width=91) -> Sort (cost=13986.86..13986.93 rows=27 width=65) Sort Key: dt.d_year, (sum(store_sales.ss_net_profit)), item.i_brand_id -> HashAggregate (cost=13985.95..13986.22 rows=27 width=65) -> Merge Join (cost=13884.21..13983.91 rows=204 width=65) Merge Cond: (dt.d_date_sk = store_sales.ss_sold_date_sk) -> Index Scan using date_dim_pkey on date_dim dt (cost=0.00..3494.62 rows=6080 width=8) Filter: (d_moy = 11) -> Sort (cost=12170.87..12177.27 rows=2560 width=65) Sort Key: store_sales.ss_sold_date_sk -> Nested Loop (cost=6.02..12025.94 rows=2560 width=65) -> Seq Scan on item (cost=0.00..1455.00 rows=16 width=59) Filter: (i_manufact_id = 914) -> Bitmap Heap Scan on store_sales (cost=6.02..658.94 rows=174 width=14) Recheck Cond: (ss_item_sk = item.i_item_sk) -> Bitmap Index Scan on store_sales_pkey (cost=0.00..5.97 rows=174 width=0) Index Cond: (ss_item_sk = item.i_item_sk)
-
Oracle 數(shù)據(jù)庫執(zhí)行計劃展示如下:
Plan hash value: 2331821367 -------------------------------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | -------------------------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 100 | 9100 | 3688 (1)| 00:00:01 | |* 1 | COUNT STOPKEY | | | | | | | 2 | VIEW | | 2736 | 243K| 3688 (1)| 00:00:01 | |* 3 | SORT ORDER BY STOPKEY | | 2736 | 256K| 3688 (1)| 00:00:01 | | 4 | HASH GROUP BY | | 2736 | 256K| 3688 (1)| 00:00:01 | |* 5 | HASH JOIN | | 2736 | 256K| 3686 (1)| 00:00:01 | |* 6 | TABLE ACCESS FULL | DATE_DIM | 6087 | 79131 | 376 (1)| 00:00:01 | | 7 | NESTED LOOPS | | 2865 | 232K| 3310 (1)| 00:00:01 | | 8 | NESTED LOOPS | | 2865 | 232K| 3310 (1)| 00:00:01 | |* 9 | TABLE ACCESS FULL | ITEM | 18 | 1188 | 375 (0)| 00:00:01 | |* 10 | INDEX RANGE SCAN | SYS_C0010069 | 159 | | 2 (0)| 00:00:01 | | 11 | TABLE ACCESS BY INDEX ROWID| STORE_SALES | 159 | 2703 | 163 (0)| 00:00:01 | --------------------------------------------------------------------------------------------------
-
OceanBase 數(shù)據(jù)庫執(zhí)行計劃展示如下:
|ID|OPERATOR |NAME |EST. ROWS|COST | ------------------------------------------------------- |0 |LIMIT | |100 |81141| |1 | TOP-N SORT | |100 |81127| |2 | HASH GROUP BY | |2924 |68551| |3 | HASH JOIN | |2924 |65004| |4 | SUBPLAN SCAN |VIEW1 |2953 |19070| |5 | HASH GROUP BY | |2953 |18662| |6 | NESTED-LOOP JOIN| |2953 |15080| |7 | TABLE SCAN |ITEM |19 |11841| |8 | TABLE SCAN |STORE_SALES|161 |73 | |9 | TABLE SCAN |DT |6088 |29401| =======================================================
由示例可見,OceanBase 數(shù)據(jù)庫的計劃展示與 Oracle 數(shù)據(jù)庫類似。OceanBase 數(shù)據(jù)庫執(zhí)行計劃中的各列的含義如下:
|
列名 |
含義 |
|---|---|
ID | 執(zhí)行樹按照前序遍歷的方式得到的編號(從 0 開始)。 |
OPERATOR | 操作算子的名稱。 |
NAME | 對應(yīng)表操作的表名(索引名)。 |
EST. ROWS | 估算該操作算子的輸出行數(shù)。 |
COST | 該操作算子的執(zhí)行代價(微秒)。 |
說明
在表操作中,NAME 字段會顯示該操作涉及的表的名稱(別名),如果是使用索引訪問,還會在名稱后的括號中展示該索引的名稱, 例如 t1(t1_c2) 表示使用了 t1_c2 這個索引。如果掃描的順序是逆序,還會在后面使用 RESERVE 關(guān)鍵字標識,例如
t1(t1_c2,RESERVE)。
OceanBase 數(shù)據(jù)庫 EXPLAIN 命令輸出的第一部分是執(zhí)行計劃的樹形結(jié)構(gòu)展示。其中每一個操作在樹中的層次通過其在 operator 中的縮進予以展示。樹的層次關(guān)系用縮進來表示,層次最深的優(yōu)先執(zhí)行,層次相同的以特定算子的執(zhí)行順序為標準來執(zhí)行。
上述 TPCDS Q3 示例的計劃展示樹如下:
OceanBase 數(shù)據(jù)庫 EXPLAIN 命令輸出的第二部分是各操作算子的詳細信息,包括輸出表達式、過濾條件、分區(qū)信息以及各算子的獨有信息(包括排序鍵、連接鍵、下壓條件等)。示例如下:
Outputs & filters:
-------------------------------------
0 - output([t1.c1], [t1.c2], [t2.c1], [t2.c2]), filter(nil), sort_keys([t1.c1, ASC], [t1.c2, ASC]), prefix_pos(1)
1 - output([t1.c1], [t1.c2], [t2.c1], [t2.c2]), filter(nil),
equal_conds([t1.c1 = t2.c2]), other_conds(nil)
2 - output([t2.c1], [t2.c2]), filter(nil), sort_keys([t2.c2, ASC])
3 - output([t2.c2], [t2.c1]), filter(nil),
access([t2.c2], [t2.c1]), partitions(p0)
4 - output([t1.c1], [t1.c2]), filter(nil),
access([t1.c1], [t1.c2]), partitions(p0) 本文標題:創(chuàng)新互聯(lián)OceanBase教程:OceanBaseSQL執(zhí)行計劃簡介
文章轉(zhuǎn)載:http://fisionsoft.com.cn/article/djdpcid.html


咨詢
建站咨詢
