当中间列可以是任何东西时,是否使用3列SQL索引?

试图证明当前的东西,看看是否有必要添加索引.

如果我在列A,B,C和I上有一个索引,那么在where子句中创建一个只显式使用A和C的查询,我能获得索引的好处吗?

在这种情况下,想象where子句是这样的:

A = 'Q' AND (B is not null OR B is null) AND C='G'

我在Oracle中使用EXPLAIN PLAN对此进行了调查,但它似乎没有使用索引.此外,根据我对如何创建和使用索引的理解,它将无法受益,因为由于缺乏细节,索引无法利用列B.

目前在MSSQL或ORACLE中查看此内容.不确定一个优化是否与另一个不同.

任何建议表示赞赏!谢谢!

最佳答案

Connected to Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 

SQL> create table t$(a integer not null, b integer, c integer, d varchar2(100 char));

Table created

SQL> insert into t$select rownum, rownum, rownum, lpad('0', '1', 100) from dual connect by level <= 1000000;

1000000 rows inserted

SQL> create index t$i on t$(a, b, c);

Index created

SQL> analyze table t$estimate statistics;

Table analyzed

SQL> explain plan for select * from t$where a = 128 and c = 128;

Explained

SQL> select * from table(dbms_xplan.display());

PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
Plan hash value: 3274478018
--------------------------------------------------------------------------------
| Id  | Operation                           | Name | Rows  | Bytes | Cost (%CPU)
--------------------------------------------------------------------------------
|   0 | SELECT STATEMENT                    |      |     1 |    13 |     4   (0)
|   1 |  TABLE ACCESS BY INDEX ROWID BATCHED| T$  |     1 |    13 |     4   (0)
|*  2 |   INDEX RANGE SCAN                  | T$I  |     1 |       |     3   (0)
--------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
   2 - access("A"=128 AND "C"=128)
       filter("C"=128)
15 rows selected

任何问题?

点赞