SELECT(MIN)vs ORDER BY ASC with FOR UPDATE

时间:2014-04-18 11:54:09

标签: sql oracle oracle11g database-deadlocks sql-tuning

我有表test1(TXID主键,STATE,NEXTRUN,TARGET),并想获得一个状态最小的NEXTRUN行(作为输入传递)。

Table Data:
-----------
TXID    STATE   NEXTRUN TARGET
2   KA  2   ANY
1   TN  1   ANY
3   KA  2   ANY
4   TN  3   A
5   KA  1   ANY

使用MIN()查询1:

SELECT *
  FROM test1
 WHERE NEXTRUN = (SELECT MIN(NEXTRUN)
                    FROM test1
                   WHERE TARGET = 'ANY'
                     AND STATE = 'KA')
   AND TARGET = 'ANY'
   AND STATE = 'KA'
   FOR UPDATE

解释计划显示(2 TABLE ACCESS FULL)

使用ORDER BY和ROWNUM查询2:

SELECT *
  FROM TEST1
 WHERE TXID = (SELECT TXID
                 FROM (SELECT *
                         FROM TEST1
                        WHERE STATE = 'KA'
                          AND TARGET = 'ANY'
                        ORDER BY NEXTRUN ASC)
                WHERE ROWNUM = 1)
   FOR UPDATE

解释计划:

(1 TABLE ACCESS FULL , 1 TABLE ACCESS BY INDEX ROWID

哪一个在防止死锁和性能方面更好查询?从多个线程(连接)调用查询。有时我发现在第一种情况下发生死锁(其中2表访问已满)。因此我正在尝试查询2.

0 个答案:

没有答案