问题描述
假设我有 10,000 行需要更新.什么会更快
Assume I have 10,000 rows that need to be updated. What would be faster
UPDATE DB.Servers SET Live = 1 where name = 'server1';
UPDATE DB.Servers SET Live = 1 where name = 'server2';
...
UPDATE DB.Servers SET Live = 1 where name = 'server100000';
OR
UPDATE DB.Servers SET Live = 1 where name in ('server1', 'server2'...'server10000');
我会假设第二个选项更快,但我不确定.让我担心的是,我不知道 SQL stm 是否有长度限制.在这种情况下会推荐什么?
I would assume the second option is faster, but I'm not sure. What worries me is that I don't know if there is a length limit for an SQL stm. What would be recommended in this type of situation?
谢谢
推荐答案
SQL应该是一种声明性语言;它不期望用户说如何"得到结果,只说什么"想要的结果.所以原则上我会使用 in()
构造,因为这是询问结果的最简洁(从逻辑角度来看)的方式,并让 DBMS(任何 DBMS!)决定什么是最好的.
SQL is supposed to be a declarative language; it does not expect from the user to say "how" to get the result, only "what" the desired result is. So in principal I would use the in()
construct, as this is the most concise (from a logical viewpoint) way to ask for the results, and let the DBMS (any DBMS!) decide what's best.
这篇关于SQL 多更新与单更新性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!