MySQL8.0新特性-CTE語法支援
CTE(common table expression),針對同一個FROM子查詢在SQL中出現多次的情況,在建立計劃的時候,只對其中一個子查詢建立計劃,並將結果放到臨時表中,其它的子查詢直接使用臨時表。比如Oracle中經常使用的with as /*+materialize*/ 用法。
首先,我們看一下簡單非遞迴的CTE的工作過程
CREATE TABLE t(a int); INSERT INTO t VALUES(1),(2); mysql>WITH abc as (SELECT * FROM t) SELECT * FROM abc; +-------------+ | a | +-------------+ | 1 | | 2 | +-------------+ 返回行數:[2],耗時:9 ms. --為了清楚的看到OPTIMIZER的最佳化過程,我們先暫且關閉derived_merge特性。 mysql>SET OPTIMIZER_SWITCH='derived_merge=off'; 執行成功,耗時:9 ms. mysql>explain WITH abc as (SELECT * FROM t) SELECT * FROM abc; +----+-------------+------------+------------+------+---------------+-----+---------+-----+------+----------+-------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+------------+------------+------+---------------+-----+---------+-----+------+----------+-------+ | 1 | PRIMARY | <derived2> | | ALL | | | | | 2 | 100 | | | 2 | DERIVED | t | | ALL | | | | | 2 | 100 | | +----+-------------+------------+------------+------+---------------+-----+---------+-----+------+----------+-------+ 返回行數:[2],耗時:9 ms.
mysql>SET OPTIMIZER_SWITCH='derived_merge=on'; 執行成功,耗時:9 ms. mysql>explain WITH abc as (SELECT * FROM t) SELECT * FROM abc; +----+-------------+-------+------------+------+---------------+-----+---------+-----+------+------------+-------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+-------+------------+------+---------------+-----+---------+-----+------+------------+-------+ | 1 | SIMPLE | t | | ALL | | | | | 2 | 100 | | +----+-------------+-------+------------+------+---------------+-----+---------+-----+------+------------+-------+ 返回行數:[1],耗時:9 ms.
啊
mysql>EXPLAIN format = json WITH cte(x) as (SELECT * FROM t) SELECT * FROM (SELECT * FROM cte) AS t1, (SELECT * FROM cte) AS t2; ----------------------------------------- | { "query_block": { "select_id": 1, "cost_info": { "query_cost": "5.65" }, "nested_loop": [ { "table": { "table_name": "t1", "access_type": "ALL", "rows_examined_per_scan": 2, "rows_produced_per_join": 2, "filtered": "100.00", "cost_info": { "read_cost": "2.52", "eval_cost": "0.20", "prefix_cost": "2.73", "data_read_per_join": "32" }, "used_columns": [ "x" ], "materialized_from_subquery": { "using_temporary_table": true, "dependent": false, "cacheable": true, "query_block": { "select_id": 2, "cost_info": { "query_cost": "2.72" }, "table": { "table_name": "cte", "access_type": "ALL", "rows_examined_per_scan": 2, "rows_produced_per_join": 2, "filtered": "100.00", "cost_info": { "read_cost": "2.52", "eval_cost": "0.20", "prefix_cost": "2.73", "data_read_per_join": "32" }, "used_columns": [ "x" ], "materialized_from_subquery": { "using_temporary_table": true, "dependent": false, "cacheable": true, "query_block": { "select_id": 3, "cost_info": { "query_cost": "0.45" }, "table": { "table_name": "t", "access_type": "ALL", "rows_examined_per_scan": 2, "rows_produced_per_join": 2, "filtered": "100.00", "cost_info": { "read_cost": "0.25", "eval_cost": "0.20", "prefix_cost": "0.45", "data_read_per_join": "32" }, "used_columns": [ "a" ] } } } } } } } }, { "table": { "table_name": "t2", "access_type": "ALL", "rows_examined_per_scan": 2, "rows_produced_per_join": 4, "filtered": "100.00", "using_join_buffer": "Block Nested Loop", "cost_info": { "read_cost": "2.53", "eval_cost": "0.40", "prefix_cost": "5.65", "data_read_per_join": "64" }, "used_columns": [ "x" ], "materialized_from_subquery": { "using_temporary_table": true, "dependent": false, "cacheable": true, "query_block": { "select_id": 4, "cost_info": { "query_cost": "2.72" }, "table": { "table_name": "cte", "access_type": "ALL", "rows_examined_per_scan": 2, "rows_produced_per_join": 2, "filtered": "100.00", "cost_info": { "read_cost": "2.52", "eval_cost": "0.20", "prefix_cost": "2.73", "data_read_per_join": "32" }, "used_columns": [ "x" ], "materialized_from_subquery": { "sharing_temporary_table_with": { "select_id": 3 } } } } } } } ] } } |
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29990276/viewspace-2685719/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- MySQL8.0 新特性 top10MySql
- Mysql8.0部分新特性MySql
- mysql8.0新特性--隱藏索引MySql索引
- MySQL8.0 新特性:Partial Update of LOB ColumnMySql
- MySQL8.0新特性-臨時表的改善MySql
- 【新特性速遞】當法語遇上FineUI(Bonjour)!UI
- 如何給 PHP 新增新的語法特性 (譯)PHP
- MySQL8.0新特性隨筆:NOWAIT以及SKIPLOCKEDMySqlAI
- PostgreSQL用CTE語法+繼承實現拆分大表SQL繼承
- Objective-C 的現代語法和新特性Object
- PostgreSQL用CTE語法+繼承實現平滑拆分大表SQL繼承
- mysql8.0 部分sql語法報錯問題MySql
- Flutter - Dart特性語法FlutterDart
- java支援with語法麼Java
- Seata 新特性,APM 支援 SkyWalking
- C++11新特性(二):語言特性C++
- C++11新特性(一):語言特性C++
- C++11新特性(三):語言特性C++
- MYSQL8.0特性—無select注入MySql
- 好程式設計師大資料培訓分享MySQL8.0新特性程式設計師大資料MySql
- LightDB 23.1相容Oracle新特性支援Oracle
- Android N新特性--多視窗支援Android
- C# 7.0 語言新特性C#
- Sublime Text 支援 SCSS 語法高亮CSS
- phpstorm新增laravle語法支援PHPORM
- JavaScript 新語法 「雙問號語法」與「可選鏈語法」JavaScript
- 【Java8新特性】Lambda表示式基礎語法,都在這兒了!!Java
- 快速掌握es6+新特性及es6核心語法盤點
- Flutter學習之Dart語法特性FlutterDart
- MySQL8.0 · 引擎特性 · 關於undo表空間的一些新變化MySql
- Chrome 71 新特性[雙語+視訊]Chrome
- swift語言新特性:可選值Swift
- JDK1.8新特性:Lambda表示式語法和內建函式式介面JDK函式
- ES6新語法
- WITH AS 用法-CTE
- LightDB23.1新特性支援Oracle hint增強DB2Oracle
- Kubernetes1.6新特性:全面支援多顆GPUGPU
- Entity Framework 6 Code First新特性:支援儲存過程Framework儲存過程