MySQL 8.0.26版本升級32版本查詢資料為空的跟蹤

GreatSQL發表於2024-03-11

某業務系統將MySQL 8.0.26升級為GreatSQL 8.0.32-24 後,某些特定的SQL語句不能查詢到資料。經測試 MySQL 8.0.32也存在相同的問題

file

此BUG已在 GreatSQL 8.0.32-25 版本中解決

MySQL 8.0.26版本升級32版本查詢資料為空的跟蹤

接到客戶反饋的問題後,對問題進行了復現和分析。

版本資訊

greatsql> select version();
+-----------+
| version() |
+-----------+
| 8.0.32-24 |
+-----------+
1 row in set (0.00 sec)

建表語句

greatsql> show create table t_student;
CREATE TABLE `t_student1` (
  `id` int NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  `age` int NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

表資料

greatsql> select * from t_student;
+----+--------+-----+
| id | name   | age |
+----+--------+-----+
|  1 | abc    |  10 |
|  2 | 湯姆   |  20 |
+----+--------+-----+
2 rows in set (0.08 sec)

查詢資料

greatsql>  select * from (select * from t_student union select * from t_student) temp where name='湯姆';
Empty set, 2 warnings (0.00 sec)

greatsql> show warnings;
+---------+------+-------------------------------------------------------------------------+
| Level   | Code | Message                                                                 |
+---------+------+-------------------------------------------------------------------------+
| Warning | 1300 | Cannot convert string '\xE6\xB1\xA4\xE5\xA7\x86' from utf8mb4 to binary |
| Warning | 1300 | Cannot convert string '\xE6\xB1\xA4\xE5\xA7\x86' from utf8mb4 to binary |
+---------+------+-------------------------------------------------------------------------+
2 rows in set (0.00 sec)

greatsql> select * from (select * from t_student union select * from t_student) temp;
+----+--------+-----+
| id | name   | age |
+----+--------+-----+
|  1 | abc    |  10 |
|  2 | 湯姆   |  20 |
+----+--------+-----+
2 rows in set (0.00 sec)

greatsql> select * from (select * from t_student union select * from t_student) temp where name='abc';
+----+------+-----+
| id | name | age |
+----+------+-----+
|  1 | abc  |  10 |
+----+------+-----+
1 row in set (0.00 sec)

可以看到 直接查詢temp這個view或者 在檢視上新增英文過濾條件均能查詢出資料,在view上新增中文過濾條件sql語句返回結果為空,有warnings提醒,warnings具體內容為Cannot convert string

在官方網站進行搜尋,以下連線有相似內容:

https://bugs.mysql.com/bug.php?id=110228

https://bugs.mysql.com/bug.php?id=110955

MySQL 8.0.32版本中對於UNION/UNION ALL 後的結果中的字串欄位過濾篩選不生效,客戶端中提示:

Cannot convert string '%\x...' from utf8mb4 to binary

官方已確認是8.0.32中的一個bug,已在8.0.33版本中修復,對於8.0.32版本,官方給出的解決方式如下:

set optimizer_switch="derived_condition_pushdown=off";

讓所有Session都生效,需要在配置檔案中將optimizer_switch設定為off

此BUG已在 GreatSQL 8.0.32-25 版本中解決

解決方法

方法一:

設定set optimizer_switch="derived_condition_pushdown=off";後SQL語句能正常執行

greatsql> set optimizer_switch='derived_condition_pushdown=off';
Query OK, 0 rows affected (0.00 sec)

greatsql> select * from (select * from t_student union select * from t_student) temp where name='湯姆';
+----+--------+-----+
| id | name   | age |
+----+--------+-----+
|  2 | 湯姆   |  20 |
+----+--------+-----+
1 row in set (0.00 sec)

方法二:

對單個SQL語句設定NO_DERIVED_CONDITION_PUSHDOWN hint

greatsql> set optimizer_switch='derived_condition_pushdown=on';
Query OK, 0 rows affected (0.00 sec)

greatsql> select * from (select * from t_student union select * from t_student) temp where name='湯姆';
Empty set, 2 warnings (0.00 sec)

greatsql> select  /*+ NO_DERIVED_CONDITION_PUSHDOWN(temp) */ * from (select * from t_student union select * from t_student) temp where name='湯姆';
+----+--------+-----+
| id | name   | age |
+----+--------+-----+
|  2 | 湯姆   |  20 |
+----+--------+-----+
1 row in set (0.00 sec)

Enjoy GreatSQL 😃

關於 GreatSQL

GreatSQL是適用於金融級應用的國內自主開源資料庫,具備高效能、高可靠、高易用性、高安全等多個核心特性,可以作為MySQL或Percona Server的可選替換,用於線上生產環境,且完全免費併相容MySQL或Percona Server。

相關連結: GreatSQL社群 Gitee GitHub Bilibili

GreatSQL社群:

社群部落格有獎徵稿詳情:https://greatsql.cn/thread-100-1-1.html

image-20230105161905827

技術交流群:

微信:掃碼新增GreatSQL社群助手微信好友,傳送驗證資訊加群

image-20221030163217640

相關文章