如何提高更新查询性能



为什么更新查询需要太多时间?

table1

Id      table2Id(Unique key)    
101        201
102        205

table2

Id    table1Id Name bool
201      101  'A'    0
202      101  'B'    0
203      101  'C'    0
205      102  'A'    0
206      102  'B'    0

表1仅包含一个Table2 ID(一对一(。但是Table2包含多个Table1 ID(一个(Table2(到许多(Table1((

update table 2 set bool=1
where Id( select t2.Id from table2 join table1 on t2.Id(PKey)=t1.table2Id(PKey) and t2.Name='A')

此查询需要时间才能更新6分钟。如果我在2秒内获取结果中的内部选择查询。

如果我在条件下更改加入,例如> t1.id = t2.table1id 然后更新查询在5sec内执行。

任何想法为什么?

这是您的查询,对您有效。但是,根据任何其他DBMS产品,这不是有效的SQL更新查询。

update table 2 set 
where Id( select t2.Id from table2 join table1 on t2.Id(PKey)=t1.table2Id(PKey))

所以,让我使用SQL Update Quert用上述

纠正您
UPDATE t1 SET table2Id = t2.Id -- Whaterver going to update
FROM table1 t1 
INNER JOIN table2 t2 
ON t2.Id = t1.Id -- whatever input parameter you have for logical/physical join operation 

现在,就性能而言,如果Table1(ID(具有主要的密钥约束或索引,则以上表现良好。

最新更新