[英]How to improve update query performance
为什么更新查询需要太多时间?
表格1
Id table2Id(Unique key)
101 201
102 205
表2
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(one(table2) to many(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 之类的条件下更改连接,则更新查询将在 5 秒内执行。
知道为什么吗?
这是您的查询,很适合您。 但是,就任何其他 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) 具有主键约束或索引,则上述性能表现良好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.