2010-11-19 3 views
10

보기가 있는데 기본보기의 일부 색인에 대한 힌트를보기 위해 내보기를 조회하고 싶습니다. 그럴 수 있습니까?보기에 힌트를 사용 하시겠습니까?

는 말은 :

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

내가 " 로부터 index1"라는 table1.col1인덱스 있습니다.

..

--query 
select * 
from temp_view 
where col1=12; 

내가이 쿼리의 계획을 설명 볼 때 그것은 "index1의"를 사용하지 않는 나에게 그 쿼리를 표시하고 나는 그것을 표시 할 :

나는 쿼리

그래서 예 :

--query with hint 
select /*+ index(temp_view index1)*/* 
from temp_view 
where col1=12; 

보기에 대한 힌트를 나타낼 수 있습니까 ?? (이 뷰를 생성하는 동안 표시하지 않으려는 경우)

+0

테스트를 거쳤지만 작동하지 않습니다./* + 인덱스 (temp_view index1) */작동하지 않습니다. 여기에 힌트를 나타낼 수있는 다른 방법이 있기를 원하기 때문에 여기에 작성했습니다. 왜냐하면이 뷰는 다른 사용자가 만들고 뷰를 변경하는 것이 옳지 않기 때문에 뷰를 변경하고 싶지 않습니다. – kupa

+0

그리고 내가 물어보고 싶은 한 가지 더 ... 힌트로 쿼리를 최적화하는 방법에 대한 좋은 지식을 얻을 수있는 유용한 자습서를 알고 있습니까? 제발 – kupa

+0

@ACP 무엇을 편집하셨습니까 ?? : D : D 내 게시물에서 어떤 에디션도 찾지 못했습니다. D – kupa

답변

12

뷰에 대한 쿼리에 힌트를 사용하여 Oracle에서 기본 테이블의 인덱스를 사용하도록 할 수 있습니다. 그러나 기본보기에서 기본 표 (있는 경우)의 별명을 알아야합니다. 일반 구문 /*+ index(<<alias of view from query>> <<alias of table from view>> <<index name>>) */

1) 10,000 동일한 행 테이블을 생성하고 테이블에 인덱스를 생성 할 것이다. 오라클은이

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) 인덱스는 일반적으로 사용되지 않았는지 확인 사용하지 않도록 인덱스는 선택되지 않습니다하지만 오라클 힌트

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) 이제보기를 작성하십시오. 그러나, 뷰 정의

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> 

말했다 모두에서 쿼리의 뷰 별칭 테이블 별칭을 모두 지정하여 사용하는 인덱스를 사용하지만 인덱스를 강제하지 않는 뷰에 대한 그 정상 쿼리를 확인, 일반적으로 힌트는 쿼리를 조정할 때 최후의 수단이됩니다. 옵티마이 저가 누락 된 정보를 파악하고 적절한 통계를 제공하여 올바른 선택을 스스로 할 수있는 것이 일반적으로 훨씬 낫습니다. 그것은 훨씬 더 안정적인 해결책입니다. 많은 경우 별칭을 여러 개 사용하는 힌트를 지정하는 경우가 줄어들어 테이블 정의의 다른 부분이 테이블 이름의 별칭을 변경하여 쿼리를 중단하기가 너무 쉽습니다.

+0

+1 방금 포기한 답변보다 포괄적 인 방법 :) 최후의 수단으로 힌트를 조정하는 방법에 대한 조언에 동의합니다. – APC

+0

대단히 감사합니다. 최고의 답변을드립니다 ... 도와 주셔서 감사합니다 ... 정말 놀랍습니다 ...그리고 SQL 힌트에 대한 내 지식을 깊이 파고 드는 방법에 대해 물어볼 수 있습니까? 좋은 튜토리얼을 찾고 있는데요 – kupa

+0

그리고 어떤 sqls이 "나쁜"것인지 확인하고 그것을 잘 조정했는지 확인하는 좋은 방법은 무엇입니까 ... "autotrace traceonly; v $ sql_longops 또는 ADDM을 사용하지 않습니까? – kupa

관련 문제