视图与表嵌套的查询的效率问题,原理何在?
1:有表INSTITUTION , 同时为该表建立一个视图v_st_inst_inuse(create view v_st_inst_inuse as select * from institution).INSTITUTION中有8万条记录,institution_id 与,Chinese_Name都建立有索引.现在实验如下:
a: sql语句一
select institution_id,FIRSTTRANS_DATE
from INSTITUTION
where institution_id in (
select v_st_inst_inuse.institution_id
from v_st_inst_inuse
where lower( v_st_inst_inuse.Chinese_Name)like '%77%')
//运行次数四次,运行时间分别为 0.12秒 0.97秒 0.13秒 0.97秒 0.13秒
b: sql语句二
select institution_id,FIRSTTRANS_DATE
from INSTITUTION
where institution_id in (
select b.institution_id
from INSTITUTION b
where lower( b.Chinese_Name)like '%77%')
//运行次数五次,运行时间分别为 1.53秒 1.68秒 1.00秒 0.97秒 1.69秒
c: sql语句三
select institution_id,FIRSTTRANS_DATE
from INSTITUTION a
where a.institution_id in (
select b.institution_id
from INSTITUTION b
where lower( b.Chinese_Name)like '%77%')
//运行次数五次,运行时间分别为 1.16秒 1.85秒 1.00秒 1.16秒 1.69秒
d: sql语句四
select CHINESE_NAME,SPELLING_NAME
from v_st_inst_inuse
where institution_id in (
select b.institution_id
from INSTITUTION b
where lower( b.Chinese_Name)like '%77%')
//运行次数三次,运行时间分别为 50.00秒 70.34秒 47.71秒
e: sql语句五
select CHINESE_NAME,SPELLING_NAME
from v_st_inst_inuse
where institution_id in (
select b.institution_id
from v_st_inst_inuse b
where lower( b.Chinese_Name)like '%77%')
//运行次数三次,运行时间分别为54.03秒 54.09秒,54.80秒
结论: 视图放在查询内层的效率比较高,如果视图放在查询的外层效率比较低。原因暂时不明.请各位高手分析一下该结论,谢谢