作者:赵师的工作日(赵明中)
现役Oracle ACE、MySQL 8.0 ocp、TiDB PCTA\PCTP、Elasticsearch Certified Engineer
微信号:mzzhao23
微信公众号:赵师的工作日
墨天轮社区:赵师的工作日
CSND:赵师的工作日
MySQL 9.2都来了
确实有时间没上MySQL官网(www.mysql.com),MySQL都出9.2版本了。现在业内大头都是8.x版本,少部分保守派还是5.7甚至5.6,还有些激进派生产已经上8.4了。
MySQL 9.2 于 2025 年 1 月 21 号正式亮相!9.2属于“创新版本”,所谓“创新版本”差不多一季一发,类似追剧,隔三差五就给你整点新鲜玩意儿。
先说说背景:创新版本是啥?
MySQL从8.0开始,分了多条路走:一个是LTS(长期支持版本),像8.0.x和8.4.x这种稳定如老狗的;另一个就是创新版本,像这次的9.2和之前的8.1。创新版本的好处是新功能来得快,重在体验,主要是给爱吃新鲜的卷王玩。至于生产环境还是老老实实上LTS。
简单过下9.2的亮点内容
新特性:支持可重用的 JavaScript 库!
简单说,就是MySQL现在可以让你写JavaScript代码,还能压缩成“库”,以后用的时候直接调。同时JavaScript也可以直接访问用户定义的函数及变量。不过该功能只在企业版里有,社区版的兄弟们只能干瞪眼了。
使用示例:
CREATE LIBRARY mylib AS 'function sayHello() { return "Hello, MySQL!"; }';
然后写一个存储过程:
CREATE PROCEDURE hello_world()
BEGIN
SELECT mylib.sayHello();
END;
执行后打印“Hello, MySQL!”
还支持SHOW CREATE LIBRARY看代码,DROP LIBRARY删库,管理起来也方便。
以下为原文内容
JavaScript Programs
The MySQL ENUM and SET types are now supported for arguments of JavaScript stored routines. For more detailed information, including rules for conversion between these MySQL types and JavaScript types, see Conversion to and from MySQL ENUM and SET. (WL #16599)
JavaScript programs supported by the MLE Component, available as part of MySQL Enterprise Edition, now supports access from JavaScript routines to user-defined functions, procedures, and variables. MySQL stored functions and procedures can be accessed using the Schema methods getFunction() and getProcedure()which return Function objects which can be used with arguments to invoke the routines. OUT and INOUT parameters of a stored procedure use placeholders created with mysql.arg(). For additional information and examples, see Stored Routine API.
The MLE component also now provides a JavaScript MySQL transaction API which performs the actions of most MySQL transactional SQL statements, such as START TRANSACTION, COMMIT, ROLLBACK, and SET AUTOCOMMIT. Support for savepoints is also included. Support for an SqlError object is included. For more information, see JavaScript Transaction API, and SqlError Object.
MySQL user variables can be accessed directly as properties of the JavaScript global Session object. For example, a user variable named myvar can be read or set as session.myvar. See Accessing Session Variables from JavaScript, for more information and examples.
This release also adds support for direct access to the MySQL builtin functions RAND(), SLEEP(), UUID(), and IS_UUID(), as, respectively, rand(), sleep(), uuid(), and isUUID(). See MySQL Functions.
For further information about JavaScript programs and the MLE Component, see JavaScript Stored Programs, and Multilingual Engine Component (MLE). (WL #16585)
The MLE component, available as part of MySQL Enterprise Edition, now supports reusable JavaScript libraries containing functions which can be called from other JavaScript stored programs. JavaScript libraries can be managed using the CREATE LIBRARY and DROP LIBRARY SQL statements added in this release; they can be included in other stored JavaScript programs with the USING clause added to CREATE FUNCTION and CREATE PROCEDURE; USING supports a list of one or more library names.
CREATE LIBRARY creates a new JavaScript library in a given database. Library code is parsed and checked for validity at creation time, and is rejected if it contains errors. DROP LIBRARY drops a JavaScript library. Library functions can be referred to in other JavaScript stored programs using library_name.function_name notation. Libraries can be aliased with USING when including them in JavaScript programs.
You can obtain the code from a library using the SHOW CREATE LIBRARY statement, also new in this release. Two new related Information Schema tables have also been added: The LIBRARIES table provides information about JavaScript libraries, and the ROUTINE_LIBRARIES provides information about stored routines which include JavaScript libraries.
Counts of library DDL and SHOW CREATE LIBRARY statements which have been issued on the server are available as the status variables Com_create_library, Com_drop_library, and Com_show_create_library.
For more information and examples, see Using JavaScript Libraries, as well as Multilingual Engine Component (MLE). (WL #16359, WL #16360, WL #16362, WL #16555)
EXPLAN升级
EXPLAIN FORMAT=JSON升级了。以前通过JSON输出看着有点乱,现在加了explain_json_format_version=2,把查询属性和迭代器属性分开,整得更清晰。跑个复杂的sql,分析起来就不那么痛苦了。对经常优化SQL的兄弟们,这是个小福利。
union优化
当YEAR和BOOL属性的列(其实就是TINYINT)在UNION的时候,以前老出错(错误 #116415,错误 #37192491)。现在直接统一改成SMALLINT,规避了这个小bug。
hash antijoins优化
当哈希表不适合连接缓冲区并溢出到磁盘时,使用哈希反连接的某些查询会返回不正确的结果。(触发问题的查询实际上指定了LEFT JOIN,但这在内部从左外连接转换为反连接。)当将探测行写入块文件时,探测表中的一些行被跳过,跳过的行是那些在部分连接键中具有NULL的行。对于内部连接和半连接,可以跳过这些行,因为它们在构建表中没有匹配,但是对于外部连接和反连接,探测表中没有匹配的行应该是连接结果的一部分,因此它们必须包含在块文件中。
9.2版本中通过在块文件中为外部连接保留了这些行来修复此问题。
mysqlslap优化
还有mysqlslap这个工具,修复了–ssl-mode=disable不生效的问题。
最后
还有很多细节优化,具体更新内容可查看官方文档。
https://dev.mysql.com/doc/relnotes/mysql/9.2/en/news-9-2-0.html#mysqld-9-2-0-js-programs