gpt4 book ai didi

sql - 'greater than' 查询的索引

转载 作者:行者123 更新时间:2023-12-04 21:31:10 26 4
gpt4 key购买 nike

我有几个疑问,其中大部分是:

select * from Blah where col > 0


select * from Blah where date > current_date

由于它们都是一个范围,col 和 date 上的非聚集 b+ 树索引是否是加速查询的好主意?还是哈希索引?还是没有索引会更好?

最佳答案

创建 索引 中使用的列上过滤谓词 作为 日期范围条件 应该很有用,因为它会做 索引范围扫描 .

这是关于How to create, display and read EXPLAIN PLAN in Oracle的演示.

让我们看看这两种场景的测试用例:

测试#1:没有索引

SQL> EXPLAIN PLAN FOR
2 SELECT * FROM emp WHERE hiredate > to_date('01/04/1981','mm/dd/yyyy');

Explained.

SQL>
SQL> SELECT * FROM TABLE(dbms_xplan.display);

PLAN_TABLE_OUTPUT
-----------------------------------------------------------------------------------------------
Plan hash value: 3956160932

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

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

PLAN_TABLE_OUTPUT
-----------------------------------------------------------------------------------------------

1 - filter("HIREDATE">TO_DATE(' 1981-01-04 00:00:00', 'syyyy-mm-dd
hh24:mi:ss'))

14 rows selected.

SQL>

测试#1:使用索引
SQL> CREATE INDEX emp_idx ON emp(hiredate);

Index created.

SQL>
SQL> EXPLAIN PLAN FOR
2 SELECT * FROM emp WHERE hiredate > to_date('01/04/1981','mm/dd/yyyy');

Explained.

SQL>
SQL> SELECT * FROM TABLE(dbms_xplan.display);

PLAN_TABLE_OUTPUT
-----------------------------------------------------------------------------------------------
Plan hash value: 3589413211

-----------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-----------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 14 | 518 | 2 (0)| 00:00:01 |
| 1 | TABLE ACCESS BY INDEX ROWID BATCHED| EMP | 14 | 518 | 2 (0)| 00:00:01 |
|* 2 | INDEX RANGE SCAN | EMP_IDX | 14 | | 1 (0)| 00:00:01 |
-----------------------------------------------------------------------------------------------

Predicate Information (identified by operation id):

PLAN_TABLE_OUTPUT
-----------------------------------------------------------------------------------------------
---------------------------------------------------

2 - access("HIREDATE">TO_DATE(' 1981-01-04 00:00:00', 'syyyy-mm-dd hh24:mi:ss'))

14 rows selected.

SQL>

因此,在第二个测试用例中,您会看到索引范围扫描。我建议您也对您的环境进行类似的测试。

关于sql - 'greater than' 查询的索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29823586/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com