oracle – 使用提示进行查看?

前端之家收集整理的这篇文章主要介绍了oracle – 使用提示进行查看?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个看法,我想查询我的视图,从基表提示一些索引,我可以这样做吗?

我的意思是:

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

我在table1.col1上有一个名为“index1”的索引.

我有一个查询

  1. --query
  2. select *
  3. from temp_view
  4. where col1=12;

当我看到这个查询的解释计划时,它向我显示,查询不使用“index1”,我想指出它

所以我想要它,例如:

  1. --query with hint
  2. select /*+ index(temp_view index1)*/*
  3. from temp_view
  4. where col1=12;

我能否提供意见提示? (如果我不想在创建此视图时指出它)

您可以对视图使用查询提示,强制Oracle在基表上使用索引.但是您需要知道基础视图中的基表(如果有)的别名.一般语法是/ * index(<<来自查询的视图别名><<来自视图>><< index name>>>< 一个例子 1)创建一个具有10,000个相同行的表,并在表上创建一个索引.索引不会有选择性,所以Oracle不会使用它
  1. sql> ed
  2. Wrote file afiedt.buf
  3.  
  4. 1 create table foo
  5. 2 as
  6. 3 select 1 col1
  7. 4 from dual
  8. 5* connect by level <= 10000
  9. sql> /
  10.  
  11. Table created.
  12.  
  13. sql> create index idx_foo on foo(col1);
  14.  
  15. Index created.

2)验证索引是否不正常使用,但Oracle将使用它提示

  1. sql> set autotrace traceonly;
  2. sql> select * from foo where col1 = 1;
  3.  
  4. 10000 rows selected.
  5.  
  6.  
  7. Execution Plan
  8. ----------------------------------------------------------
  9. Plan hash value: 1245013993
  10.  
  11. --------------------------------------------------------------------------
  12. | Id | Operation | Name | Rows | Bytes | Cost (%cpu)| Time |
  13. --------------------------------------------------------------------------
  14. | 0 | SELECT STATEMENT | | 10000 | 126K| 7 (0)| 00:00:01 |
  15. |* 1 | TABLE ACCESS FULL| FOO | 10000 | 126K| 7 (0)| 00:00:01 |
  16. --------------------------------------------------------------------------
  17.  
  18. Predicate Information (identified by operation id):
  19. ---------------------------------------------------
  20.  
  21. 1 - filter("COL1"=1)
  22.  
  23. Note
  24. -----
  25. - dynamic sampling used for this statement (level=2)
  26.  
  27.  
  28. Statistics
  29. ----------------------------------------------------------
  30. 9 recursive calls
  31. 0 db block gets
  32. 713 consistent gets
  33. 5 physical reads
  34. 0 redo size
  35. 172444 bytes sent via sql*Net to client
  36. 7849 bytes received via sql*Net from client
  37. 668 sql*Net roundtrips to/from client
  38. 0 sorts (memory)
  39. 0 sorts (disk)
  40. 10000 rows processed
  41.  
  42. sql> select /*+ index(foo idx_foo) */ *
  43. 2 from foo
  44. 3 where col1 = 1;
  45.  
  46. 10000 rows selected.
  47.  
  48.  
  49. Execution Plan
  50. ----------------------------------------------------------
  51. Plan hash value: 15880034
  52.  
  53. ----------------------------------------------------------------------------
  54. | Id | Operation | Name | Rows | Bytes | Cost (%cpu)| Time |
  55. ----------------------------------------------------------------------------
  56. | 0 | SELECT STATEMENT | | 10000 | 126K| 25 (0)| 00:00:01 |
  57. |* 1 | INDEX RANGE SCAN| IDX_FOO | 10000 | 126K| 25 (0)| 00:00:01 |
  58. ----------------------------------------------------------------------------
  59.  
  60. Predicate Information (identified by operation id):
  61. ---------------------------------------------------
  62.  
  63. 1 - access("COL1"=1)
  64.  
  65. Note
  66. -----
  67. - dynamic sampling used for this statement (level=2)
  68.  
  69.  
  70. Statistics
  71. ----------------------------------------------------------
  72. 7 recursive calls
  73. 0 db block gets
  74. 715 consistent gets
  75. 15 physical reads
  76. 0 redo size
  77. 172444 bytes sent via sql*Net to client
  78. 7849 bytes received via sql*Net from client
  79. 668 sql*Net roundtrips to/from client
  80. 0 sorts (memory)
  81. 0 sorts (disk)
  82. 10000 rows processed

3)现在创建视图.验证针对视图的普通查询不使用索引,但强制使用索引,方法是在查询中指定视图别名,并从视图定义中指定表别名

  1. sql> create view vw_foo
  2. 2 as
  3. 3 select col1
  4. 4 from foo f;
  5.  
  6. View created.
  7.  
  8. sql> select col1
  9. 2 from vw_foo
  10. 3 where col1 = 1;
  11.  
  12. 10000 rows selected.
  13.  
  14.  
  15. Execution Plan
  16. ----------------------------------------------------------
  17. Plan hash value: 1245013993
  18.  
  19. --------------------------------------------------------------------------
  20. | Id | Operation | Name | Rows | Bytes | Cost (%cpu)| Time |
  21. --------------------------------------------------------------------------
  22. | 0 | SELECT STATEMENT | | 10000 | 126K| 7 (0)| 00:00:01 |
  23. |* 1 | TABLE ACCESS FULL| FOO | 10000 | 126K| 7 (0)| 00:00:01 |
  24. --------------------------------------------------------------------------
  25.  
  26. Predicate Information (identified by operation id):
  27. ---------------------------------------------------
  28.  
  29. 1 - filter("COL1"=1)
  30.  
  31. Note
  32. -----
  33. - dynamic sampling used for this statement (level=2)
  34.  
  35.  
  36. Statistics
  37. ----------------------------------------------------------
  38. 16 recursive calls
  39. 0 db block gets
  40. 715 consistent gets
  41. 0 physical reads
  42. 0 redo size
  43. 172444 bytes sent via sql*Net to client
  44. 7849 bytes received via sql*Net from client
  45. 668 sql*Net roundtrips to/from client
  46. 0 sorts (memory)
  47. 0 sorts (disk)
  48. 10000 rows processed
  49.  
  50. sql> select /*+ index(vf f idx_foo) */ col1
  51. 2 from vw_foo vf
  52. 3 where col1 = 1;
  53.  
  54. 10000 rows selected.
  55.  
  56.  
  57. Execution Plan
  58. ----------------------------------------------------------
  59. Plan hash value: 15880034
  60.  
  61. ----------------------------------------------------------------------------
  62. | Id | Operation | Name | Rows | Bytes | Cost (%cpu)| Time |
  63. ----------------------------------------------------------------------------
  64. | 0 | SELECT STATEMENT | | 10000 | 126K| 25 (0)| 00:00:01 |
  65. |* 1 | INDEX RANGE SCAN| IDX_FOO | 10000 | 126K| 25 (0)| 00:00:01 |
  66. ----------------------------------------------------------------------------
  67.  
  68. Predicate Information (identified by operation id):
  69. ---------------------------------------------------
  70.  
  71. 1 - access("COL1"=1)
  72.  
  73. Note
  74. -----
  75. - dynamic sampling used for this statement (level=2)
  76.  
  77.  
  78. Statistics
  79. ----------------------------------------------------------
  80. 14 recursive calls
  81. 0 db block gets
  82. 717 consistent gets
  83. 0 physical reads
  84. 0 redo size
  85. 172444 bytes sent via sql*Net to client
  86. 7849 bytes received via sql*Net from client
  87. 668 sql*Net roundtrips to/from client
  88. 0 sorts (memory)
  89. 0 sorts (disk)
  90. 10000 rows processed
  91.  
  92. sql>

然而,所有这一切,一般来说,试图调整查询是一个最后的手段 – 通常更好的是找出优化器丢失的信息,并提供适当的统计信息,以便它可以自己做出正确的选择.这是一个更稳定的解决方案.反之,如果您减少指定涉及多层别名的提示 – 例如,通过更改表名称的别名,触摸视图定义的人可以轻松地中断查询.

猜你在找的Oracle相关文章