天天看點

技術大牛養成指南:吃的草夠多,你也能成為大牛(附思維導圖)

非常高興的向大家宣布MySQL 8.0 GA版本釋出,MySQL 8.0是一個得到全面增強且極具吸引力的新版本。不限于下面幾點:

We proudly announce General Availability of MySQL 8.0. Download now! MySQL 8.0 is an extremely exciting new version of the world’s most popular open source database with improvements across the board. Some key enhancements include:

1、SQL方面:視窗函數,公共表達式,NOWAIT, SKIP LOCKED, 降序索引,分組,正規表達式,字元集,CBO優化模式,直方圖

1、SQL Window functions, Common Table Expressions, NOWAIT and SKIP LOCKED, Descending Indexes, Grouping, Regular Expressions, Character Sets, Cost Model, and Histograms.

2、對JSON的支援:擴充文法,新函數,排序增強,JSON列部分更新。基于JSON表的特性,可以調用SQL語句處理JSON資料。

2、JSON Extended syntax, new functions, improved sorting, and partial updates. With JSON table functions you can use the SQL machinery for JSON data.

3、對地理資訊系統的支援—空間引用系統(SRS),包括SRS空間資料類型,空間索引,空間函數

3、GIS Geography support. Spatial Reference Systems (SRS), as well as SRS aware spatial datatypes, spatial indexes, and spatial functions.

4、可靠性:DDL語句支援原子性和崩潰安全恢複(元資訊資料被存在了一個基于InnoDB的單獨事務性資料字典中)。

4、Reliability DDL statements have become atomic and crash safe, meta-data is stored in a single, transactional data dictionary. Powered by InnoDB!

5、可觀察性:對P_S,I_S,配置參數,錯誤日志的記錄有顯著增強5、Observability Significant enhancements to Performance Schema, Information Schema, Co

nfiguration Variables, and Error Logging.

6、可管理性:遠端管理,Undo表空間管理,快速DDL

6、Manageability Remote management, Undo tablespace management, and new instant DDL.

7、安全性:OpenSSL的改進,新的預設驗證方式,SQL角色權限,分解super權限,密碼強度提升等等

7、Security OpenSSL improvements, new default authentication, SQL Roles, breaking up the super privilege, password strength, and more.

8、性能:InnoDB在讀/寫負載,高IO負載,熱資料高并發競争等場景表現更好。新增的資源組特性給使用者在特定負載和特定硬體情況下将使用者線程映射到指定的CPU上的可選項

8、Performance InnoDB is significantly better at Read/Write workloads, IO bound workloads, and high contention “hot spot” workloads. Added Resource Group feature to give users an option optimize for specific workloads on specific hardware by mapping user threads to CPUs.

以上是8.0版本的部分亮點,我(原文作者)推薦您仔細閱讀GA版本前幾個版本的釋出資訊,甚至這些特性和實作方法的的項目日志。或者您可以選擇直接在Github上閱讀源碼。

The above represents some of the highlights and I encourage you to further drill into the complete series of Milestone blog posts—8.0.0, 8.0.1, 8.0.2, 8.0.3, and 8.0.4 —and even further down in to the individual worklogs with their specifications and implementation details. Or perhaps you prefer to just look at the source code at github.com/mysql.

面向開發人員的特性

MySQL 8.0應面向MySQL開發人員的需求,帶來了SQL,JSON,正規表達式,地理資訊系統等方面的特性,因為很多開發人員有存儲EmoJi表情的需求,在新版本中UTF8MB4成為預設的字元集。除此之外,還有對Binary資料類型按位操作,和對IPV6和UUID函數的改進。

MySQL Developers want new features and MySQL 8.0 delivers many new and much requested features in areas such as SQL, JSON, Regular Expressions, and GIS. Developers also want to be able to store Emojis, thus UTF8MB4 is now the default character set in 8.0. Finally there are improvements in Datatypes, with bit-wise operations on BINARY datatypes and improved IPv6 and UUID functions.

SQL

1、視窗函數

MySQL 8.0帶來了标準SQL的視窗函數功能,視窗函數與分組聚合函數相類似的是都提供了對一組行資料的統計計算。但與分組聚合函數将多行合并成一行不同是視窗函數會在結果結果集中展現每一行的聚合。

MySQL 8.0 delivers SQL window functions. Similar to grouped aggregate functions, window functions perform some calculation on a set of rows, e.g. COUNT or SUM. But where a grouped aggregate collapses this set of rows into a single row, a window function will perform the aggregation for each row in the result set.

視窗函數有兩種使用方式,首先是正常的SQL聚合功能函數和特殊的視窗函數。

正常的聚合功能函數如:COUNT,SUM等函數。而視窗函數專有的則是RANK, DENSE_RANK, PERCENT_RANK, CUME_DIST, NTILE, ROW_NUMBER, FIRST_VALUE, LAST_VALUE, NTH_VALUE, LEADand LAG等函數。

Window functions come in two flavors: SQL aggregate functions used as window functions and specialized window functions. This is the set of aggregate functions in MySQL that support windowing: COUNT, SUM, AVG, MIN, MAX, BIT_OR, BIT_AND, BIT_XOR, STDDEV_POP (and its synonyms STD, STDDEV), STDDEV_SAMP, VAR_POP (and its synonym VARIANCE) and VAR_SAMP. The set of specialized window functions are: RANK, DENSE_RANK, PERCENT_RANK, CUME_DIST, NTILE, ROW_NUMBER, FIRST_VALUE, LAST_VALUE, NTH_VALUE, LEADand LAG

對視窗函數的支援上,是使用者呼聲比較頻繁。視窗函數早在SQL2003規範中就成為了标準SQL的一部分。

Support for window functions (a.k.a. analytic functions) is a frequent user request. Window functions have long been part of standard SQL (SQL 2003). See blog post by Dag Wanvik here as well as blog post by Guilhem Bichot here.

2、公用表表達式(CTE)

MySQL 8.0 帶來了支援遞歸的公用表表達式的功能。非遞歸的公用表表達式由于允許由from子句派生的臨時表的原因可以被多次引用,因而被解釋為改進型的派生表(from子句中的臨時表)。

而遞歸的公用表表達式則由一組原始資料,經過處理後得到新的一組資料,再被帶入處理得到更多的新資料,循環往複直到再也無法産生更多新資料為止。公用表達式也是一個使用者呼聲頻繁的SQL功能。

MySQL 8.0 delivers [Recursive] Common Table Expressions (CTEs). Non-recursive CTEs can be explained as “improved derived tables” as it allow the derived table to be referenced more than once. A recursive CTE is a set of rows which is built iteratively: from an initial set of rows, a process derives new rows, which grow the set, and those new rows are fed into the process again, producing more rows, and so on, until the process produces no more rows. CTE is a commonly requested SQL feature, see for example feature request 16244 and 32174 . See blog posts by Guilhem Bichot here, here, here, and here.

3、新的NOWAIT、SKIP LOCKED選項

MySQL 8.0 給SQL的上鎖子句帶來了NOWAIT和SKIP LOCKED兩個可選項。在原來的版本中,當行資料被UPDATE或者SELECT ... FOR UPDATE語句上鎖後,其他的事務需要等待鎖釋放才能通路這行資料。

但在某些場景下,有馬上獲得(不等待鎖)資料的需求。使用NOWAIT參數後如果請求的資料中包括了被鎖住的行,将馬上會收到查詢失敗的報錯資訊。使用SKIP LOCKED參數後,傳回的資料将會跳過被鎖住的行。

MySQL 8.0 delivers NOWAIT and SKIP LOCKED alternatives in the SQL locking clause. Normally, when a row is locked due to an UPDATE or a SELECT ... FOR UPDATE, any other transaction will have to wait to access that locked row. In some use cases there is a need to either return immediately if a row is locked or ignore locked rows. A locking clause using NOWAIT will never wait to acquire a row lock. Instead, the query will fail with an error. A locking clause using SKIP LOCKED will never wait to acquire a row lock on the listed tables. Instead, the locked rows are skipped and not read at all. NOWAIT and SKIP LOCKED are frequently requested SQL features. See for example feature request 49763 . We also want to say thank you to Kyle Oppenheim for his code contribution! See blog post by Martin Hansson here.

4、降序索引

MySQL 8.0 帶來了對降序索引的支援。在 8.0降序索引中,資料被倒序組織,正向查找。而在之前的版本中,雖然支援建立降序排列的索引,但其實作方式是通過建立常見的正序索引,然後進行反向查找來實作的。

一方面,正序查找要比逆序查找更快;

另一方面,真正的降序索引在複合的order by語句(即有asc又有desc)中,可以提高索引使用率,消除filesort。

MySQL 8.0 delivers support for indexes in descending order. Values in such an index are arranged in descending order, and we scan it forward. Before 8.0, when a user create a descending index, we created an ascending index and scanned it backwards. One benefit is that forward index scans are faster than backward index scans. Another benefit of a real descending index is that it enables us to use indexes instead of filesort for an ORDER BY clause with mixed ASC/DESC sort key parts. Descending Indexes is a frequently requested SQL feature. See for example feature request 13375 . See blog post by Chaithra Gopalareddy here.

5、分組函數

MySQL 8.0 帶來了GROUPING()分組函數,這個功能可以把group by子句擴充功能(如ROLLUP)産生的過聚合NULL值,通過0和1進行區分,1為NULL,這樣就可以在having子句中對過聚合的無效值進行過濾。

MySQL 8.0 delivers GROUPING(), SQL_FEATURE T433. The GROUPING() function distinguishes super-aggregate rows from regular grouped rows. GROUP BY extensions such as ROLLUP produce super-aggregate rows where the set of all values is represented by null. Using the GROUPING() function, you can distinguish a null representing the set of all values in a super-aggregate row from a NULL in a regular row. GROUPING is a frequently requested SQL feature. See feature requests 3156 and 46053. Thank you to Zoe Dong and Shane Adams for code contributions in feature request 46053 ! See blog post by Chaithra Gopalareddy here.

6、優化器建議

在5.7版本中我們引入了新的優化器建議的文法,借助這個新的文法,優化器建議可以被用/*+ */包裹起來,直接放在SELECT | INSERT | REPLACE | UPDATE | DELETE關鍵字的後面。

在8.0的版本中我們又加入了新的姿勢:

In 5.7 we introduced a new hint syntax for optimizer hints. With the new syntax, hints can be specified directly after the SELECT | INSERT | REPLACE | UPDATE | DELETEkeywords in an SQL statement, enclosed in /*+ */ style comments. (See 5.7 blog post by Sergey Glukhov here). In MySQL 8.0 we complete the picture by fully utilizing this new style:

技術大牛養成指南:吃的草夠多,你也能成為大牛(附思維導圖)

8.0版本增加了INDEX_MERGE和NO_INDEX_MERGE,允許使用者在單個查詢中控制是否使用索引合并特性。

MySQL 8.0 adds hints for INDEX_MERGE and NO_INDEX_MERGE. This allows the user to control index merge behavior for an individual query without changing the optimizer switch.

技術大牛養成指南:吃的草夠多,你也能成為大牛(附思維導圖)

8.0版本增加了JOIN_FIXED_ORDER, JOIN_ORDER, JOIN_PREFIX, 和 JOIN_SUFFIX,允許使用者控制join表關聯的順序。

MySQL 8.0 adds hints for JOIN_FIXED_ORDER, JOIN_ORDER, JOIN_PREFIX, and JOIN_SUFFIX. This allows the user to control table order for the join execution.

技術大牛養成指南:吃的草夠多,你也能成為大牛(附思維導圖)

8.0版本增加了SET_VAR,該優化器建議可以設定一個隻在下一條語句中生效的的系統參數。

MySQL 8.0 adds a hint called SET_VAR. The SET_VAR hint will set the value for a given system variable for the next statement only. Thus the value will be reset to the previous value after the statement is over. See blog post by Sergey Glukhov here.

相對于之前的優化器建議和優化器特性開關參數,我們更傾向于推薦新形式的優化器建議模式,新形式的優化器建議可以在不侵入SQL語句(指修改語句的非注釋的業務部分)的情況下,注入查詢語句的很多位置。與直接修改語句的優化器建議相比,新形勢的優化器建議在SQL語義上更加清晰。

We prefer the new style of optimizer hints as preferred over the old-style hints and setting of optimizer_switch values. By not being inter-mingled with SQL, the new hints can be injected in many places in a query string. They also have clearer semantics in being a hint (vs directive).

JSON

8.0版本追加了新的JSON函數,并可以提高在排序與分組JSON資料情況下的性能。

MySQL 8.0 adds new JSON functions and improves performance for sorting and grouping JSON values.

1、JSON path表達式中擴充的範圍性文法

MySQL 8.0 擴充了JSON path表達式中範圍性的文法,比如:SELECT JSON_EXTRACT('[1, 2, 3, 4, 5]', '$[1 to 3]');可以得出[2, 3, 4]的結果。

MySQL 8.0 extends the syntax for ranges in JSON path expressions. For example SELECT JSON_EXTRACT('[1, 2, 3, 4, 5]', '$[1 to 3]'); results in [2, 3, 4].

The new syntax introduced is a subset of the SQL standard syntax, described in SQL:2016, 9.39 SQL/JSON path language: syntax and semantics. See also Bug#79052reported by Roland Bouman.

2、JSON表函數

MySQL 8.0 增加了可以在JSON資料上使用SQL處理工具的JSON 表函數。JSON_TABLE()函數可以建立JSON資料的關系型視圖。可以将JSON資料估算到關系型的行列之中,使用者可以對此函數傳回的資料按照正常關系型資料表的方式進行SQL運算。

MySQL 8.0 adds JSON table functions which enables the use of the SQL machinery for JSON data. JSON_TABLE() creates a relational view of JSON data. It maps the result of a JSON data evaluation into relational rows and columns. The user can query the result returned by the function as a regular relational table using SQL, e.g. join, project, and aggregate.

3、JSON 聚合函數

MySQL 8.0 增加了用于生成JSON陣列的聚合函數JSON_ARRAYAGG(),和用于生成JSON對象的JSON_OBJECTAGG()函數,令多行的JSON文檔組合成JSON陣列或者JSON對象成為可能。

MySQL 8.0 adds the aggregation functions JSON_ARRAYAGG() to generate JSON arrays and JSON_OBJECTAGG() to generate JSON objects . This makes it possible to combine JSON documents in multiple rows into a JSON array or a JSON object. See blog post by Catalin Besleaga here.

4、JSON 合并函數

JSON_MERGE_PATCH() 函數可執行JavaScript的文法,在合并時發生重複鍵值對時将會優先選用第二個文檔的鍵值對,并删除第一個文檔對應的重複鍵值。

The JSON_MERGE_PATCH() function implements the semantics of JavaScript (and other scripting languages) specified by RFC7396, i.e. it removes duplicates by precedence of the second document. For example, JSON_MERGE('{"a":1,"b":2 }','{"a":3,"c":4 }');# returns {"a":3,"b":2,"c":4}.

JSON_MERGE_PRESERVE()函數與5.7版本中的JSON_MERGE()含義相同,都是在合并的時候保留所有值。

The JSON_MERGE_PRESERVE() function has the semantics of JSON_MERGE() implemented in MySQL 5.7 which preserves all values, for example JSON_MERGE('{"a": 1,"b":2}','{"a":3,"c":4}'); # returns {"a":[1,3],"b":2,"c":4}.

5.7原來的JSON_MERGE() 函數在8.0版本中為減少merge操作的不明确性,而被棄用。

The existing JSON_MERGE() function is deprecated in MySQL 8.0 to remove ambiguity for the merge operation. See also proposal in Bug#81283 and blog post by Morgan Tocker here.

5、JSON 美化函數

8.0版本增加了可以接收JSON原生資料類型和用字元串形式表達的JSON,并傳回一行縮進的易讀的JSON格式化後的的字元串。

MySQL 8.0 adds a JSON_PRETTY() function in MySQL. The function accepts either a JSON native data-type or string representation of JSON and returns a JSON formatted string in a human-readable way with new lines and indentation.

6、JSON 檔案大小函數

8.0版本增加了和指定JSON對象空間占用相關的函數,JSON_STORAGE_SIZE() 可以用位元組為機關傳回JSON某個資料類型的實際大小, JSON_STORAGE_FREE() 可以傳回該JSON資料類型的剩餘空間(包括碎片和用來适應更改後發生長度變化的預備空間)

MySQL 8.0 adds JSON functions related to space usage for a given JSON object. The JSON_STORAGE_SIZE() returns the actual size in bytes for a JSON datatype. The JSON_STORAGE_FREE() returns the free space of a JSON binary type in bytes, including fragmentation and padding saved for inplace update.

7、JSON 改進型的排序

8.0版本通過使用變長的排序鍵提升了JSON排序分組的性能。在某些場景下,Preliminary 的壓測結果出現了1.2到18倍的提升。

MySQL 8.0 gives better performance for sorting/grouping JSON values by using variable length sort keys. Preliminary benchmarks shows from 1.2 to 18 times improvement in sorting, depending on use case.

8、JSON的部分更新

8.0版本增加了對 JSON_REMOVE(), JSON_SET() 和 JSON_REPLACE() 函數的部分更新的支援。如果JSON文檔的某部分被更新,我們會将更改的詳情給到句柄。這樣存儲引擎和複制關系就不必寫入整個JSON文檔。

在之前的複制環境中由于無法確定JSON文檔的排列(layout)在主從上完全一緻,是以在基于行的複制情況下實體檔案的差異并不能用來削減傳輸複制資訊帶來的網絡IO消耗。

是以,8.0版本提供了在邏輯上區分差異的方法,可以在行複制的情況下傳輸并應用到從庫上。

MySQL 8.0 adds support for partial update for the JSON_REMOVE(), JSON_SET() and JSON_REPLACE() functions. If only some parts of a JSON document are updated, we want to give information to the handler about what was changed, so that the storage engine and replication don’t need to write the full document. In a replicated environment, it cannot be guaranteed that the layout of a JSON document is exactly the same on the slave and the master, so the physical diffs cannot be used to reduce the network I/O for row-based replication. Thus, MySQL 8.0 provides logical diffs that row-based replication can send over the wire and reapply on the slave. See blog post by Knut Anders Hatlen here.

原文釋出時間為:2018-05-15

本文作者:張銳志

本文來自雲栖社群合作夥伴“

老葉茶館

”,了解相關資訊可以關注“

”。

繼續閱讀