Coleman
Coleman

Reputation: 670

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; 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? e.g.

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.

Upvotes: 3

Views: 45

Answers (2)

spencer7593
spencer7593

Reputation: 108490

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.

Otherwise, in MySQL, these two queries are equivalent. 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. (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. 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.)

If you're familiar with other relational databases (Oracle, SQL Server, et al.) then adding the WHERE clause is second nature.

Aside from those issues, it doesn't really matter if you add the WHERE clause or not.

The reasons I could see with bothering with adding a WHERE clause:

  • avoid firing BEFORE/AFTER UPDATE triggers
  • 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)

Upvotes: 1

Bajji
Bajji

Reputation: 2393

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.

If you don't have a where clause, the database default reads all the rows from the disk and then does replace. For strings which don't qualify for the replace it is an unnecessary lookup from the disk.

Upvotes: 2

Related Questions