Why update query takes too much time?
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
Table 1 will contain only one table2 Id(one to one). But table2 contain multiple 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')
This query takes time to update around 6mins. If I run inner select query within 2sec getting results.
If I change join on condition like t1.Id=t2.table1Id then update query executes within 5sec.
Any idea why?
This is your query which works well for you. However, this is not valid SQL
update query in terms of any other DBMS Product.
update table 2 set
where Id( select t2.Id from table2 join table1 on t2.Id(PKey)=t1.table2Id(PKey))
So, let me correct you with SQL update quert with above
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
Now, in terms of performance the above is perform well if table1(Id) has primary key constraint or index.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.