0

我有表 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 TABLE ACCESS FULL)正在发生。因此,我正在尝试使用查询 2。

4

0 回答 0