gpt4 book ai didi

oracle - 对 View 使用提示?

转载 作者:行者123 更新时间:2023-12-03 15:29:28 24 4
gpt4 key购买 nike

我有一个 View ,我想像这样查询我的 View 以提示基表中的一些索引,我可以这样做吗?

我的意思是:

--view
create or replace view temp_view
as select col1,col2,col3
from table1,table2....

我有一个 索引 table1.col1 称为“ index1 ”。

我有一个 查询 :
--query
select *
from temp_view
where col1=12;

当我看到这个查询的解释计划时,它告诉我该查询不使用“index1”,我想指出它..

所以我希望它是,例如:
--query with hint
select /*+ index(temp_view index1)*/*
from temp_view
where col1=12;

我可以指出意见的提示吗?? (如果我不想在创建此 View 期间指出它)

最佳答案

您可以对针对 View 的查询使用提示,以强制 Oracle 使用基表上的索引。但是您需要知道基础 View 中基表(如果有)的别名。一般语法是 /*+ index(<<alias of view from query>> <<alias of table from view>> <<index name>>) */
一个例子

1) 创建一个包含 10,000 行相同行的表,并在该表上创建索引。索引不会有选择性,所以 Oracle 不会想要使用它

SQL> ed
Wrote file afiedt.buf

1 create table foo
2 as
3 select 1 col1
4 from dual
5* connect by level <= 10000
SQL> /

Table created.

SQL> create index idx_foo on foo(col1);

Index created.

2) 验证索引没有正常使用但Oracle会在提示下使用
SQL> set autotrace traceonly;
SQL> select * from foo where col1 = 1;

10000 rows selected.


Execution Plan
----------------------------------------------------------
Plan hash value: 1245013993

--------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 10000 | 126K| 7 (0)| 00:00:01 |
|* 1 | TABLE ACCESS FULL| FOO | 10000 | 126K| 7 (0)| 00:00:01 |
--------------------------------------------------------------------------

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

1 - filter("COL1"=1)

Note
-----
- dynamic sampling used for this statement (level=2)


Statistics
----------------------------------------------------------
9 recursive calls
0 db block gets
713 consistent gets
5 physical reads
0 redo size
172444 bytes sent via SQL*Net to client
7849 bytes received via SQL*Net from client
668 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
10000 rows processed

SQL> select /*+ index(foo idx_foo) */ *
2 from foo
3 where col1 = 1;

10000 rows selected.


Execution Plan
----------------------------------------------------------
Plan hash value: 15880034

----------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
----------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 10000 | 126K| 25 (0)| 00:00:01 |
|* 1 | INDEX RANGE SCAN| IDX_FOO | 10000 | 126K| 25 (0)| 00:00:01 |
----------------------------------------------------------------------------

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

1 - access("COL1"=1)

Note
-----
- dynamic sampling used for this statement (level=2)


Statistics
----------------------------------------------------------
7 recursive calls
0 db block gets
715 consistent gets
15 physical reads
0 redo size
172444 bytes sent via SQL*Net to client
7849 bytes received via SQL*Net from client
668 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
10000 rows processed

3) 现在创建 View 。通过指定查询中的 View 别名和 View 定义中的表别名,验证针对 View 的普通查询不使用索引而是强制使用索引
SQL> create view vw_foo
2 as
3 select col1
4 from foo f;

View created.

SQL> select col1
2 from vw_foo
3 where col1 = 1;

10000 rows selected.


Execution Plan
----------------------------------------------------------
Plan hash value: 1245013993

--------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 10000 | 126K| 7 (0)| 00:00:01 |
|* 1 | TABLE ACCESS FULL| FOO | 10000 | 126K| 7 (0)| 00:00:01 |
--------------------------------------------------------------------------

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

1 - filter("COL1"=1)

Note
-----
- dynamic sampling used for this statement (level=2)


Statistics
----------------------------------------------------------
16 recursive calls
0 db block gets
715 consistent gets
0 physical reads
0 redo size
172444 bytes sent via SQL*Net to client
7849 bytes received via SQL*Net from client
668 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
10000 rows processed

SQL> select /*+ index(vf f idx_foo) */ col1
2 from vw_foo vf
3 where col1 = 1;

10000 rows selected.


Execution Plan
----------------------------------------------------------
Plan hash value: 15880034

----------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
----------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 10000 | 126K| 25 (0)| 00:00:01 |
|* 1 | INDEX RANGE SCAN| IDX_FOO | 10000 | 126K| 25 (0)| 00:00:01 |
----------------------------------------------------------------------------

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

1 - access("COL1"=1)

Note
-----
- dynamic sampling used for this statement (level=2)


Statistics
----------------------------------------------------------
14 recursive calls
0 db block gets
717 consistent gets
0 physical reads
0 redo size
172444 bytes sent via SQL*Net to client
7849 bytes received via SQL*Net from client
668 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
10000 rows processed

SQL>

尽管如此,在尝试调整查询时,提示通常是最后的手段——通常最好弄清楚优化器缺少哪些信息并提供适当的统计信息,以便它可以自己做出正确的选择。这是一个更稳定的解决方案。当您减少到指定涉及多层别名的提示时,更是如此——例如,对于触摸 View 定义的人来说,通过更改表名的别名来破坏您的查询太容易了。

关于oracle - 对 View 使用提示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4222430/

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