gpt4 book ai didi

mysql - 为什么 MySQL 安全更新模式不允许使用 LIKE 谓词?

转载 作者:行者123 更新时间:2023-11-29 09:42:18 25 4
gpt4 key购买 nike

请考虑以下场景:

  • 我有一个名为 actor 的 MySQL 表。 (是的,它来自 Sakila 示例数据库)。
  • 表中有一个名为 last_name 的 varchar 列。
  • 该列上有一个(非主)KEY。
  • MySQL 安全更新模式已开启。

当我运行此查询时,它有效:

DELETE FROM actor WHERE last_name = 'foo';

当我运行此查询时:

DELETE FROM actor WHERE last_name LIKE '%fo' OR last_name LIKE 'fo%';

失败并显示以下错误消息:

Error Code: 1175. You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column. To disable safe mode, toggle the option in Preferences -> SQL Editor and reconnect.

为什么安全更新模式会停止第二个命令并允许第一个命令?

最佳答案

根据documentation :

It is possible for UPDATE and DELETE statements to produce an error in safe-updates mode even with a key specified in the WHERE clause, if the optimizer decides not to use the index on the key column.

LIKE 'fo%' 这样的测试通常会使用索引(索引可用于匹配列的开头),因此它不会导致错误。但是像 LIKE '%fo' 这样的测试无法建立索引(它没有指定列的开头,因此需要完整扫描),因此您会收到错误。

关于mysql - 为什么 MySQL 安全更新模式不允许使用 LIKE 谓词?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56366697/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com