使用视图提示?
我有一个视图,我想像这样查询我的视图以暗示基表中的某些索引,我可以这样做吗?
我的意思是:
--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;
我可以指出视图提示吗? (如果我不想在创建此视图期间指出它)
I have a view and I want to query my view like that to hint some index from a base table,can I do that?
I mean:
--view
create or replace view temp_view
as select col1,col2,col3
from table1,table2....
I have an index on table1.col1 called "index1".
I have a query:
--query
select *
from temp_view
where col1=12;
And when I see explain plan of this query it shows me that query doesn't use "index1" and I want to indicate it..
So I want it to be,for example:
--query with hint
select /*+ index(temp_view index1)*/*
from temp_view
where col1=12;
Can I indicate hints for views?? (If I don't want to indicate it during creation of this view)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以在针对视图的查询上使用提示来强制 Oracle 使用基表上的索引。但是您需要知道基础视图中基表(如果有)的别名。一般语法为
/*+ index(<<查询中视图的别名>> <<视图中表的别名>> <<索引名称>>) */< /code>
示例
1) 创建一个包含 10,000 个相同行的表,并在该表上创建索引。该索引不会是选择性的,因此 Oracle 不会想要使用它
2) 验证该索引没有正常使用,但 Oracle 将通过提示使用它
3) 现在创建视图。验证针对视图的正常查询不使用索引,而是通过指定查询中的视图别名和视图定义中的表别名来强制使用索引。
尽管如此,提示通常是最后的手段当尝试调整查询时,通常最好找出优化器缺少哪些信息并提供适当的统计信息,以便它可以自行做出正确的选择。这是一个更加稳定的解决方案。更重要的是,当您减少到指定涉及多层别名的提示时,例如,对于接触视图定义的人来说,通过更改表名的别名来破坏您的查询太容易了。
You can use a hint on a query against a view to force Oracle to use an index on the base table. But you need to know the alias of the base table (if any) in the underlying view. The general syntax would be
/*+ index(<<alias of view from query>> <<alias of table from view>> <<index name>>) */
An example
1) Create a table with 10,000 identical rows and create an index on the table. The index won't be selective, so Oracle won't want to use it
2) Verify that the index is not used normally but that Oracle will use it with a hint
3) Now create the view. Verify that normal queries against the view don't use the index but force the index to be used by specifying both the view alias in the query and the table alias from the view definition
All that said, however, hints in general are a last resort when trying to tune a query-- it's generally far preferable to figure out what information the optimizer is missing and provide appropriate statistics so that it can make the correct choice on its own. That's a much more stable solution going forward. Doubly so when you're reduced to specifying hints that involve multiple layers of aliases-- it's way too easy for someone touching the view definition to break your query by changing the alias of the table name, for example.
我尝试了贾斯汀·凯夫(Justin Cave)的(旁边的答案)语法
,但它对我不起作用。接下来是
我在 Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production 上尝试的工作
I tried Justin Cave's (the answer beside) syntax
, but it doesn't work for me. The next is worked
I tried on Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production