开发者

How to optimize UPDATE statement?

I have a table with almost 1 million records 开发者_开发问答and another table with a few hundred records. I would like to update the large table with values from the small table with the following

UPDATE tableA ta, tableB tb
SET price=tb.price
WHERE ta.id=tb.id

Using the above SQL statement, the query is taking a really long time (more than 1 hour). Is there a method that I can use to make this operation faster?

Here is the schema for both tables.

Table A

id         name   descrip   region   price
0          a      abc       def      1.7
1          b      abc       def      2.2
3          c      abc       def      3.4
4          d      abc       def      5.3
....       .      ...       ...      ...
999999     e      abc       def      4.5
1000000    f      abc       def      7.9

Table B

id      price
0       0.7
1       2.5
3       1.9
4       7.9

Result Table A

Table A

id         name   descrip   region   price
0          a      abc       def      0.7
1          b      abc       def      2.5
3          c      abc       def      1.9
4          d      abc       def      7.9
....       .      ...       ...      ...
999999     e      abc       def      4.5
1000000    f      abc       def      7.9


If your tableB has index, that covers id (obviously it is) - then you have no other ways to speed it up. Since the slowest thing here is physical changing of the value.

Probably you can change your where to:

WHERE ta.id=tb.id and ta.price <> tb.price

to avoid of updating the price to the same value

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜