[英]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.