ByteHouse Support project Pass the EXPLAIN statement is SQL statement generate the corresponding Run Plan, Help you Understood how ByteHouse Run Search, making it easier for you to optimize SQL statements in a targeted manner. This article uses the SQL in TPCH-Q1 as an example to demonstrate how to use different Type of EXPLAIN statements.
In ByteHouse, the optimizer (Query Optimizer) achieves end-to-end query optimization through Rules-based optimization (Rule-Based Optimization, RBO), cost-based optimization (Cost-Based Optimization, CBO), Native distributed Plan collaboration, and advanced capabilities (dynamic filter pushdown/materialized view rewriting). After Activate the optimizer, executing the EXPLAIN statement will generate the corresponding Run Plan for the SQL statement, Show the Details of the Run Plan, and generate a more efficient Run Plan. This can Help developers quickly identify SQL performance bottlenecks and optimize index design, Expression complexity, or distributed execution Strategy in a targeted manner, ensuring results are returned quickly with Min Resources consumption.
ByteHouse currently supports the following Type of EXPLAIN statements:
| | | \
| Type | Definition | |
|---|---|---|
| \ | ||
| EXPLAIN | Used to Show the logical Run Plan. | |
| \ | ||
| [EXPLAIN DISTRIBUTED](/docs/6517/1279299#c98e72fc | UsedtoShowthedistributedRunPlan. | |
| \ | ||
| [EXPLAIN ANALYZE](/docs/6517/1279299#a24ba283 | UsedtoShowtheRunPlanwithruntimeinformation,whichcanbesubdividedintothefollowingType: | |) |
| \ | ||
| * EXPLAIN ANALYZE | \ | |
| * [EXPLAIN ANALYZE DISTRIBUTED](/docs/6517/1279299#5ac8bd50:UsedtoShowthedistributedRunPlanwithruntimeinformation. | ||
| \ | ||
| [EXPLAIN AST](/docs/6517/1279299#9c1ffdba | Dumpstheabstractsyntaxtree(AST)ofthequery,supportsallTypeofqueries,notlimitedtoSELECT. | |
| \ | ||
| [EXPLAIN SYNTAX](/docs/6517/1279299#e38a3fa2 | Returnsthequeryaftersyntaxoptimization. | |
| \ | ||
| [EXPLAIN METADATA](/docs/6517/1279299#fb138645 | ThisstatementisusedtoShowsomeinformationparsedduringtheSQLAnalyzerphase.TheparsedinformationmainlyincludesDatabasename,tablename,Columnname,functionname,settings,andothers. | |
EXPLAIN [DISTRIBUTED | ANALYZE | AST | SYNTAX | METADATA ] [setting = value, ...] [ SELECT ... | tableFunction(...) [COLUMNS (...)] [ORDER BY ...] [PARTITION BY ...] [PRIMARY KEY] [SAMPLE BY ...] [TTL ...] ] [FORMAT ...]
| | | | \
| Param | Required | Description | ||||
|---|---|---|---|---|---|---|
| \ | ||||||
| DISTRIBUTED | ANALYZE | AST | SYNTAX | METADATA | No | EXPLAIN Type, Instructions for use can be found in the subsequent [EXPLAIN Type](/docs/6517/1279299#868a679fsection. |
| \ | ||||||
| setting = value | No | settings customize Custom Sql Query execution parameters. | \ | |||
For example, you can use json to settings whether to print the Plan in JSON Format. Set to 1 to print the Plan in JSON Format, set to 0 to not print. The effect of this parameter setting is the same as settings FORMAT. Example: json = 1. | ||||||
| \ | ||||||
| SELECT | Either one | Either one with tableFunction(...). Set to the SELECT query statement to be Analysis. | ||||
| \ | ||||||
| tableFunction(...) | Either one | Either one with SELECT. Set the table function to be analyzed. | ||||
| \ | ||||||
| COLUMNS (...) | No | Define the column names and types that the table function needs to return. | ||||
| \ | ||||||
| ORDER BY | No | Define the sort keys for the table function. | ||||
| \ | ||||||
| PARTITION BY | No | Define the data partition rules for the table function. | ||||
| \ | ||||||
| PRIMARY KEY | No | Set the index primary key for the table function. | ||||
| \ | ||||||
| SAMPLE BY | No | Declare the sampling expression for the table function. | ||||
| \ | ||||||
| TTL | No | Configure the data expiration strategy for the table function. | ||||
| \ | ||||||
| FORMAT | No | Define how to convert the result into the specified output format. |
EXPLAIN json=0 SELECT l_returnflag, l_linestatus, sum(l_quantity) AS sum_qty, sum(l_extendedprice) AS sum_base_price, sum(l_extendedprice * (1 - l_discount)) AS sum_disc_price, sum((l_extendedprice * (1 - l_discount)) * (1 + l_tax)) AS sum_charge, avg(l_quantity) AS avg_qty, avg(l_extendedprice) AS avg_price, avg(l_discount) AS avg_disc, count(*) AS count_order FROM lineitem WHERE l_shipdate <= (toDate('1998-12-01') - toIntervalDay('90')) GROUP BY l_returnflag, l_linestatus ORDER BY l_returnflag ASC, l_linestatus ASC
Output result
This statement is used to show the distributed execution plan.
EXPLAIN DISTRIBUTED <SQL>;
EXPLAIN DISTRIBUTED SELECT l_returnflag, l_linestatus, sum(l_quantity) AS sum_qty, sum(l_extendedprice) AS sum_base_price, sum(l_extendedprice * (1 - l_discount)) AS sum_disc_price, sum((l_extendedprice * (1 - l_discount)) * (1 + l_tax)) AS sum_charge, avg(l_quantity) AS avg_qty, avg(l_extendedprice) AS avg_price, avg(l_discount) AS avg_disc, count(*) AS count_order FROM lineitem WHERE l_shipdate <= (toDate('1998-12-01') - toIntervalDay('90')) GROUP BY l_returnflag, l_linestatus ORDER BY l_returnflag ASC, l_linestatus ASC
Output result
EXPLAIN ANALYZE is used to show the execution plan with runtime information, and also supports showing the distributed execution plan with runtime information through EXPLAIN ANALYZE DISTRIBUTED.
The general setting parameters supported by this type of statement are as follows:
| | | | \
| Parameter | Required | Description | |
|---|---|---|---|
| \ | |||
| json | No | Set whether to print the plan in JSON format. The default value is 0, which means the plan is not output in JSON format. Set to 1 to print the plan in JSON format. Example: json = 1 | |
| \ | |||
| stats | No | Set whether to output statistics row count and cost. The default value is 1, which means output. Set to 0 to not output. Example: stats = 1. | |
| \ | |||
| profile | No | Set whether to output profiles during execution. The default value is 1, which means output. Set to 0 to not output. Example: profile = 1. | |
| \ | |||
| verbose | No | Set whether to output detailed content of logical plan nodes. The default value is 1, which means output. Set to 0 to not output. Example: verbose = 1. | |
| \ | |||
| indexes | No | Set whether to print index usage. Set to 1 to print, set to 0 to not print. Example: indexes = 1. | |
| \ | |||
| segment_id | No | Set the segment id. You can specify printing specific segment content when printing distributed plans. | |
| \ | |||
| segment_profile | No | Set whether to print segment time when printing distributed plans. Set to 1 to print, set to 0 to not print. Example: segment_profile = 1. |
This statement is used to show the execution plan with runtime information. For specific field descriptions, see Explain Analyze field description.
EXPLAIN ANALYZE <SQL>;
In addition to general parameters, EXPLAIN ANALYZE also supports the following setting parameters:
processor_profile_events = 1.EXPLAIN ANALYZE SELECT l_returnflag, l_linestatus, sum(l_quantity) AS sum_qty, sum(l_extendedprice) AS sum_base_price, sum(l_extendedprice * (1 - l_discount)) AS sum_disc_price, sum((l_extendedprice * (1 - l_discount)) * (1 + l_tax)) AS sum_charge, avg(l_quantity) AS avg_qty, avg(l_extendedprice) AS avg_price, avg(l_discount) AS avg_disc, count(*) AS count_order FROM lineitem WHERE l_shipdate <= (toDate('1998-12-01') - toIntervalDay('90')) GROUP BY l_returnflag, l_linestatus ORDER BY l_returnflag ASC, l_linestatus ASC
Output
This statement is used to display the distributed execution plan with runtime information. For detailed field descriptions, see Explain Analyze field description.
EXPLAIN ANALYZE DISTRIBUTED <SQL>;
In addition to general parameters, EXPLAIN ANALYZE DISTRIBUTED also supports the following setting parameters:
processor_profile_events = 1.profile_events_list='QueryAnalyzerTime'. If there are multiple parameter values, use a comma (,) to separate them.aggregate_profiles = 1 to print the aggregated profile for each worker. Set aggregate_profiles = 0 and the system will print profiles for each worker separately.EXPLAIN ANALYZE DISTRIBUTED SELECT l_returnflag, l_linestatus, sum(l_quantity) AS sum_qty, sum(l_extendedprice) AS sum_base_price, sum(l_extendedprice * (1 - l_discount)) AS sum_disc_price, sum((l_extendedprice * (1 - l_discount)) * (1 + l_tax)) AS sum_charge, avg(l_quantity) AS avg_qty, avg(l_extendedprice) AS avg_price, avg(l_discount) AS avg_disc, count(*) AS count_order FROM lineitem WHERE l_shipdate <= (toDate('1998-12-01') - toIntervalDay('90')) GROUP BY l_returnflag, l_linestatus ORDER BY l_returnflag ASC, l_linestatus ASC
Output
Dumps the abstract syntax tree (AST) of the query, supporting all types of queries, not limited to SELECT.
EXPLAIN AST <SQL>;
EXPLAIN AST SELECT l_returnflag, l_linestatus, sum(l_quantity) AS sum_qty, sum(l_extendedprice) AS sum_base_price, sum(l_extendedprice * (1 - l_discount)) AS sum_disc_price, sum((l_extendedprice * (1 - l_discount)) * (1 + l_tax)) AS sum_charge, avg(l_quantity) AS avg_qty, avg(l_extendedprice) AS avg_price, avg(l_discount) AS avg_disc, count(*) AS count_order FROM lineitem WHERE l_shipdate <= (toDate('1998-12-01') - toIntervalDay('90')) GROUP BY l_returnflag, l_linestatus ORDER BY l_returnflag ASC, l_linestatus ASC
Output
Returns the query after syntax optimization.
EXPLAIN SYNTAX <SQL>;
EXPLAIN SYNTAX SELECT l_returnflag, l_linestatus, sum(l_quantity) AS sum_qty, sum(l_extendedprice) AS sum_base_price, sum(l_extendedprice * (1 - l_discount)) AS sum_disc_price, sum((l_extendedprice * (1 - l_discount)) * (1 + l_tax)) AS sum_charge, avg(l_quantity) AS avg_qty, avg(l_extendedprice) AS avg_price, avg(l_discount) AS avg_disc, count(*) AS count_order FROM lineitem WHERE l_shipdate <= (toDate('1998-12-01') - toIntervalDay('90')) GROUP BY l_returnflag, l_linestatus ORDER BY l_returnflag ASC, l_linestatus ASC
Output
This statement is used to display information parsed during the SQL Analyzer phase. The parsed information mainly includes database name, table name, column name, function name, settings, and others.
EXPLAIN METADATA <SQL>;EXPLAIN METADATA <SQL>;
EXPLAIN METADATA supports the following setting parameters:
format_json = 1 to output content in JSON format.lineage = 1.explain metadata select t1.a, t2.b from metadata t1 join metadata2 t2 on t1.a=t2.a settings enable_optimizer=1;
Output
| | | | | | | \
| tables | databases | column_lists | userd_function_names | settings | insert_info | |
|---|---|---|---|---|---|---|
| \ | ||||||
| ["metadata","metadata2"] | ["2100xxxxxx.test","2100xxxxxx.test"] | [["a"],["a","b"]] | ["equals"] | {"enable_optimizer":"1"} | [] |
The field descriptions are as follows:
EXPLAIN METADATA format_json = 1, lineage = 1 WITH t3 AS ( SELECT count() AS a, sum(b) AS b FROM cache2 GROUP BY c ) SELECT a, b FROM ( ( SELECT t3.b AS a, t2.b AS b FROM cache AS t2 INNER JOIN t3 ON t2.a = t3.a ) UNION ALL ( SELECT * FROM t3 ) ) AS t1 WHERE t1.a = 1 SETTINGS enable_optimizer = 1 FORMAT TabSeparatedRaw
Output
{ "UsedTablesInfo" : [ { "Database" : "test", "Table" : "cache", "Columns" : [ "a", "b" ] }, { "Database" : "test", "Table" : "cache2", "Columns" : [ "b", "c" ] } ], "UsedFunctions" : [ "count", "equals", "sum" ], "UsedSettings" : { "enable_optimizer" : "1" }, "InsertInfo" : { }, "FunctionsInfo" : [ ], "LineageInfo" : { "TableSources" : [ { "Database" : "test", "Table" : "cache2", "Columns" : [ { "Id" : 3, "Name" : "b" }, { "Id" : 2, "Name" : "c" } ] }, { "Database" : "test", "Table" : "cache", "Columns" : [ { "Id" : 1, "Name" : "a" }, { "Id" : 0, "Name" : "b" } ] } ], "ExpressionSources" : [ { "Sources" : [ { "Database" : "test", "Table" : "cache2" } ], "Expression" : [ { "Id" : 4, "Name" : "expr#count()" } ] } ], "OutputLineageInfo" : [ { "Name" : "b", "SourceIds" : [ 3, 0 ] }, { "Name" : "a", "SourceIds" : [ 4, 3 ] } ], "InsertLinageInfo" : { } } }
The field descriptions are as follows: