日期:2014-05-17  浏览次数:20818 次

如何使用视图时用索引,而不是全表扫描?

现在用table1和table2

create   or   replace   view   table3   as
select   t1.fcode,   t1.fname
from   table1
union   all
select   t2.fcode,   t2.fname
from   table2

table1和table2中的FCODE都是主键,
现在我用table3关联检索数据的时候,
发现table3这个视图是全表扫描的
这个导致速度很慢,

哪位有解决办法呀?

------解决方案--------------------
视图除了物化视图是不能创建索引的
------解决方案--------------------
语法是:
create materialized view yourview
...

create index on yourview(col1,col2,...);
------解决方案--------------------
如果是select t3.*
from table3 t3, TmpPickup m
where t3.fcode = mycode
的话,9i以上数据库会优化sql并使用索引,如果临时表里数据不多,使用动态构造sql的方式实现吧
------解决方案--------------------
创建视图时使用暗示索引
------解决方案--------------------
create view myview as
select /*+ index(mytable myindex) */ from mytable;

就这样