select b.SJNM as zznm,b.sfhy,count(1) as n
from p_tzzjbda_2j b inner join p_tyjbda c on b.ZZNM = c.zznm
where (c.zzmm='3' or c.zzmm='1')
and c.CSRQ >= to_date(to_char(2005 - 19)||'-12-31','yyyy-mm-dd') --出生日期
group by b.SJNM,b.sfhy
这句SQL中关系到了p_tzzjbda_2j和p_tyjbda 两个表,p_tzzjbda_2j表中的记录为三万多,p_tyjbda 表的记录为50多万,这么点记录,这个查询花了20多秒,条件中的相字段我已经建立过索引了。
请求各位大哥大姐帮忙一下!!
select b.SJNM as zznm,b.sfhy,count(1) as n
from p_tzzjbda_2j b inner join p_tyjbda c on b.ZZNM = c.zznm
where (c.zzmm='3' or c.zzmm='1')
and c.CSRQ < to_date(to_char(2005 - 19)||'-12-31','yyyy-mm-dd')
and c.csrq >= to_date(to_char(2005 - 26)||'-12-31','yyyy-mm-dd')
group by b.SJNM,b.sfhy
这句要花费200秒左右。。
from p_tzzjbda_2j b inner join p_tyjbda c on b.ZZNM = c.zznm
where (c.zzmm='3' or c.zzmm='1')
and c.CSRQ >= to_date(to_char(2005 - 19)||'-12-31','yyyy-mm-dd') --出生日期
group by b.SJNM,b.sfhy
这句SQL中关系到了p_tzzjbda_2j和p_tyjbda 两个表,p_tzzjbda_2j表中的记录为三万多,p_tyjbda 表的记录为50多万,这么点记录,这个查询花了20多秒,条件中的相字段我已经建立过索引了。
请求各位大哥大姐帮忙一下!!
select b.SJNM as zznm,b.sfhy,count(1) as n
from p_tzzjbda_2j b inner join p_tyjbda c on b.ZZNM = c.zznm
where (c.zzmm='3' or c.zzmm='1')
and c.CSRQ < to_date(to_char(2005 - 19)||'-12-31','yyyy-mm-dd')
and c.csrq >= to_date(to_char(2005 - 26)||'-12-31','yyyy-mm-dd')
group by b.SJNM,b.sfhy
这句要花费200秒左右。。
create materialized view MVIEWP_TYJBDA
refresh complete on demand
start with sysdate next sysdate+0.5
as
select * from p_tyjbda where zzmm = '1' or zzmm = '3'
from p_tzzjbda_2j b inner join p_tyjbda c on b.ZZNM = c.zznm
where c.zzmm IN ('3','1')
and c.CSRQ BETWEEN to_date(to_char(2005 - 26)||'-12-31','yyyy-mm-dd') AND to_date(to_char(2005 - 19)||'-12-30','yyyy-mm-dd')+0.99999
group by b.SJNM,b.sfhy
首先看看执行计划是否使用到了索引,其次再看看你的索引是否有必要建,再次因为用到了group by,涉及到了排序,耗的时间自然长一点.从语句表面上看,zzmm字段似乎没必要建索引?如果索引建的不好,反而会使时间更久.(这只是猜测,你要根据实际情况分析)同一条语句在不同的环境下可能效率差别很大,关键是看执行计划.