select * from tab1 t where t.usercode=(select usercode from tab2 tt where tt.pcode='123456');
上面的查询语句中tab1在usercode字段上有索引,通过上面的查询可以按索引进行查询.
但是
CREATE OR REPLACE VIEW V_tab AS
select * from tab1 t 
再通过视图查询时,则查询时不会按索引进行查询.
select * from V_tab  t where t.usercode=(select usercode from tab2 tt where tt.pcode='123456');
请问这是什么原因?
是因为查询的是视图吗???有没有办法在查询视图时也能用到表的索引?

解决方案 »

  1.   

    和视图没有关系,你这里有个子查询,所哟导致这样的问题
    类似select * from (select * from tab1) t where t.usercode=(select usercode from tab2 tt where tt.pcode='123456'); 
      

  2.   

    是这样一种情况.
    在使用这个查询语句时
    select * from V_tab  t where t.usercode=(select usercode from tab2 tt where tt.pcode='123456');
    无法使用索引.
    但使用
    select * from V_tab  t where t.usercode='234556';
    是可以使用到索引的.