Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysql UPDATE statement - overhead for same values?

Tags:

mysql

i have a large MYSQL database with hundreds of thousands of records. i want to update a field in a large number of them, but I am unaware if that field has been updated yet or not.

if i call an update statement that sets authortype=10 and authortype is already 10 will this be faster than doing a separate query to only select those that aren't authortype=10 and then update them?

in other words, if I set a value equal to what it is already, is that any faster than if I am updating a value to something new? again this is with tons and tons of records and I want to be efficient.

thanks in advance

like image 544
JiminyCricket Avatar asked Jul 06 '10 14:07

JiminyCricket


1 Answers

No, MySQL is smart and won't be slower. Don't go through the trouble of checking for that, MySQL will do it for you.

If you set a column to the value it currently has, MySQL notices this and does not update it. No write action is performed. (Source)

BUT,

MySQL can use the WHERE-clause on the column-to-update to determine which index to use (and thus which rows to examine), in which case it might speed up your UPDATE-operation. If your column is indexed, do include it.

like image 88
Konerak Avatar answered Oct 16 '22 08:10

Konerak