简体   繁体   English

我应该在使用字符串替换进行更新时添加WHERE子句

[英]Should I add a WHERE clause when updating with string replacements

I want to perform a string replacement on an entire column, changing all instances of one phrase to another: 我想在整个列上执行字符串替换,将一个短语的所有实例更改为另一个:

UPDATE `some_records`
SET `some_column` = REPLACE(`some_column`, 'foo', 'bar');

Since many of the rows do not contain the string 'foo' they will be unaffected by this query, which is fine; 由于许多行不包含字符串'foo',因此它们不受此查询的影响,这很好; I only care about the rows that do contain it. 我只关心包含它的行。 My question is, is there any reason to add a WHERE clause to explicitly target the rows that will be affected? 我的问题是,有没有理由添加一个WHERE子句来明确地定位将受影响的行? eg 例如

UPDATE `some_records`
SET `some_column` = REPLACE(`some_column`, 'foo', 'bar')
WHERE `some_column` LIKE '%foo%';

As far as I can tell, both queries have the exact same effect. 据我所知,两个查询都具有完全相同的效果。 Is there any advantage to the 2nd version? 第二版有什么优势吗? Does it provide better performance or any other benefits? 它是否提供更好的性能或任何其他好处? So far I haven't found documentation to say one is better than the other. 到目前为止,我还没有找到文件说一个比另一个好。

AFAIK, if you have an index on a column which is used as a condition in the WHERE clause it should speed up the lookup of the rows which are supposed to be updated. AFAIK,如果在列中使用索引作为WHERE子句中的条件,它应该加速查找应该更新的行。

If you don't have a where clause, the database default reads all the rows from the disk and then does replace. 如果您没有where子句,则数据库默认从磁盘读取所有行,然后进行替换。 For strings which don't qualify for the replace it is an unnecessary lookup from the disk. 对于不符合替换条件的字符串,它是磁盘上不必要的查找。

If there's a BEFORE / AFTER UPDATE trigger defined on the table, the difference in the queries is whether the trigger is fired for all rows in the table, or just the rows that satisfy the predicate in the WHERE clause. 如果在表上定义了BEFORE / AFTER UPDATE触发器,则查询的不同之处在于是否为表中的所有行触发了触发器,或者仅触发了WHERE子句中满足谓词的行。

Otherwise, in MySQL, these two queries are equivalent. 否则,在MySQL中,这两个查询是等价的。 MySQL doesn't count (or report) a row as being "affected" by an UPDATE if the value assigned to the column is identical the value already in the column. 如果分配给列的值与列中已有的值相同,则MySQL不会将行计数(或报告)为受UPDATE“影响”。 (Other relational databases do count such rows in the "affected" count. (其他关系数据库会对“受影响”计数中的此类行进行计数。

Because of the leading percent sign in the LIKE comparison, that condition will need to be evaluated for every row in the table, so there's not going to be any difference in performance. 由于LIKE比较中的前导百分号,需要对表中的每一行评估该条件,因此性能不会有任何差异。 If there's an index on some_records(some_column), MySQL might choose to a full index scan which might be slightly faster in some cases.) 如果some_records(some_column)上有索引,MySQL可能会选择完整索引扫描,在某些情况下可能会略快一些。)

If you're familiar with other relational databases (Oracle, SQL Server, et al.) then adding the WHERE clause is second nature. 如果您熟悉其他关系数据库(Oracle,SQL Server等),那么添加WHERE子句是第二天性。

Aside from those issues, it doesn't really matter if you add the WHERE clause or not. 除了这些问题之外,如果添加WHERE子句并不重要。

The reasons I could see with bothering with adding a WHERE clause: 我可以通过添加WHERE子句来看到的原因:

  • avoid firing BEFORE / AFTER UPDATE triggers 避免BEFORE AFTER UPDATE触发AFTER UPDATE触发
  • familiar pattern used in other relational databases 熟悉的模式在其他关系数据库中使用
  • possibly improved performance (if the rows are really long, if the index is much, much shorter, and a small fraction of the rows will satisfy the condition) 可能性能得到改善(如果行非常长,如果索引更长,更短,并且一小部分行将满足条件)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM