Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
452 views
in Technique[技术] by (71.8m points)

mysql UPDATE statement - overhead for same values?

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...