开发者

optimizing SQL UPDATE with LIKE

开发者 https://www.devze.com 2023-03-15 04:13 出处:网络
I have tried running the following UPDATE on a table with about 25k rows and its been running for over 24hours and it has not compl开发者_运维技巧eted.I only need to run this UPDATE once so time is no

I have tried running the following UPDATE on a table with about 25k rows and its been running for over 24hours and it has not compl开发者_运维技巧eted. I only need to run this UPDATE once so time is not too much of an issue, but anything that long is just not going to work for me. The UPDATE statement does work as I have tried running it in isolation on just a few records, but when you apply it over the full table is when it bogs down.

Im certain that the LIKE is causing the slow down, but I do not have any idea how to make this any simpler or faster, any clues would be appreciated:

UPDATE INVENTORY i2

SET i2.BVRTLPRICE01 =

(SELECT i1.BVRTLPRICE01 FROM INVENTORY i1

WHERE 

i1.CODE = REPLACE(LEFT(i2.CODE,1), 'S', 'C') + SUBSTRING(i2.CODE,2,LENGTH(i2.CODE)))        

WHERE 

i2.CODE like 'S%'


I've only worked with Pervasive once or twice, but I can't imagine that the engine is that horrible that the problem would be a simple LIKE as you have it. The issue is more likely the subquery.

I would try this instead:

UPDATE
    I2
SET
    BVRTLPRICE01 = I1.BVRTLPRICE01
FROM
    INVENTORY I2
INNER JOIN INVENTORY I1 ON
    I1.CODE = REPLACE(LEFT(I2.CODE, 1), 'S', 'C') +
              SUBSTRING(I2.CODE, 2, LENGTH(I2.CODE)))
WHERE
    I2.CODE LIKE 'S%'

Also, make sure that you're only joining on one row from I1 and not getting many rows for each row in I2.


i have a small hunch. might be wrong but based upon the crazy runtime and small dataset. try to add

LIMIT 1

to your sub query.


use proper join instead of subquery

UPDATE INVENTORY i2
SET i2.BVRTLPRICE01 = i1.BVRTLPRICE01 
FROM INVENTORY i1,INVENTORY i2
WHERE i1.CODE = REPLACE(LEFT(i2.CODE,1), 'S', 'C') + SUBSTRING(i2.CODE,2,LENGTH(i2.CODE)))  
AND i2.CODE like 'S%'
0

精彩评论

暂无评论...
验证码 换一张
取 消