如何让Oracle In 语句走索引

如何让Oracle In 语句走索引
最新回答
灵魂的出口

2026-01-19 23:29:54

SQL> select * from tt;

ID NAME
---------- ------------------------------
0 OpiVRDxFYj
1 xFQDNnnzEa
2 qnInbDemzF
3 KwnfZFaDbZ
4 SOlJYxyzcx
5 qJSHeqsMIR
6 JICLOWKqHo
7 MGNqkOkHnE
8 AUwSMpmgrh
9 WkMdkhkwAg
10 mLFpnbYDOS

ID NAME
---------- ------------------------------
11 uthEjWxorG
12 EhbTSwnxml
13 vXSttovVWb
14 HgZZlGHJRc
15 npdczEItnZ
16 MTahLohdZF
17 yEVRfqoicj
18 ZanQNubbmh
19 OIpxJSVHCL
20 MFNmtSfSKc

21 rows selected.

SQL> create index idx_tt_id on tt(id);

Index created.

SQL> analyze table tt compute statistics;

Table analyzed.

SQL> set autot trace
SQL> select * from tt where id in (1,10,20);

Execution Plan
----------------------------------------------------------
Plan hash value: 831939183

--------------------------------------------------------------------------------
----------

| Id | Operation | Name | Rows | Bytes | Cost (%CPU)|
Time |

--------------------------------------------------------------------------------
----------

| 0 | SELECT STATEMENT | | 3 | 36 | 2 (0)|
00:00:01 |

| 1 | INLIST ITERATOR | | | | |
|

| 2 | TABLE ACCESS BY INDEX ROWID| TT | 3 | 36 | 2 (0)|
00:00:01 |

|* 3 | INDEX RANGE SCAN | IDX_TT_ID | 3 | | 1 (0)|
00:00:01 |

--------------------------------------------------------------------------------
----------

Predicate Information (identified by operation id):
---------------------------------------------------

3 - access("ID"=1 OR "ID"=10 OR "ID"=20)

Statistics
----------------------------------------------------------
1 recursive calls
0 db block gets
5 consistent gets
0 physical reads
0 redo size
689 bytes sent via SQL*Net to client
523 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
3 rows processed

SQL> select * from tt where name in ('npdczEItnZ','EhbTSwnxml');

Execution Plan
----------------------------------------------------------
Plan hash value: 264906180

--------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 2 | 24 | 3 (0)| 00:00:01 |
|* 1 | TABLE ACCESS FULL| TT | 2 | 24 | 3 (0)| 00:00:01 |
--------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

1 - filter("NAME"='EhbTSwnxml' OR "NAME"='npdczEItnZ')

Statistics
----------------------------------------------------------
1 recursive calls
0 db block gets
7 consistent gets
0 physical reads
0 redo size
662 bytes sent via SQL*Net to client
523 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
2 rows processed