If I want to update a field with the same value.
For instance, a table T, with severals columns: A, B, C
Is it better to run this statement:
UPDATE `T` SET `B` = '0';
or this one:
UPDATE `T` SET `B` = '0' WHERE `B` <> '0';
I think the first one could be faster (even if I have an index on B)?
If I have millions of rows.. will it be long or really fast to apply this update?
(I try to search an existing similar question… but it’s quite hard with the keywords I used.. too many different questions… do not hesitate to point me to a good one…)
The relative performance depends on the selectivity of
WHERE B <> 0.No
WHEREclause will perform a full table scan.No index on B will perform a full table scan.
An index on B with a low selectivity, i.e. many hits will generate a high rate of random I/O, as the rows found in the index are adressed for update. In most systems, this will be a performance bottleneck. If the selectivity is very low, you might end up with a big part of a full table scan, but pseudo-random instead of sequential – this is worst case. This might be optimized out during query plan generation and replaced by a full table scan.
a high selectivity, i.e. few hits will still create random I/O, but only as little as necessary – this is best case.
So depending on the percentage of rows needing updating, you should chose between a full table scan (= no
WHERE) and a targeted approach (=WHEREwith index)