应该说5张表才对,大家请看看。
select a.p_id,a.p_sid,a.p_cardid,a.p_bankid,f.p_state,f.p_psnum,f.p_panum from p_detail a,p_card d, (select e.p_sid,b.p_state,c.p_psnum,c.p_panum,c.p_mbankid from p_pss b,p_mccinfo c,p_sid e where b.p_id=e.p_id and b.p_id=c.p_id and c.p_mbankid=b.p_mbankid and b.p_state in ('01','03','04','05')and (c.p_psnum is not null and c.p_pastate='00')) f where a.p_caardid=d.p_cardid and a.p_sid=f.p_sid and f.p_mbankid=d.p_bankid and (a.p_state in ('01','04') and a.p_cardid is not null and a.p_cardid not in (select p_cardid from p_card where p_rec=1)) and d.p_rec=0 order by a.p_sid

解决方案 »

  1.   

    select a.p_id,a.p_sid,a.p_cardid,a.p_bankid,f.p_state,f.p_psnum,f.p_panum 
    from (select p_id,p_sid,p_cardid,p_bankid from  p_detail 
    where (p_state in ('01','04') and p_cardid is not null 
    and p_cardid not in (
    select p_cardid from p_card where p_rec=1)
    )) a,
    p_card d, 
    (
    select e.p_sid,b.p_state,c.p_psnum,c.p_panum,c.p_mbankid 
    from p_pss b,p_mccinfo c,p_sid e 
    where b.p_id=e.p_id and b.p_id=c.p_id and c.p_mbankid=b.p_mbankid 
    and b.p_state in ('01','03','04','05') and (c.p_psnum is not null and c.p_pastate='00')) f  
    where a.p_caardid=d.p_cardid and a.p_sid=f.p_sid and f.p_mbankid=d.p_bankid  and d.p_rec=0 order by a.p_sid
      

  2.   

    数据库一定是比程序运行要快的多。
    要是用sql都不行。那么程序就不要向了。单单从这语句没法看出问体。因为我们根本不知道你那张表的数据量多。。
    要尽量避免多这样的表进行重复查询。或他们之间的连接。
      

  3.   

    有index的键先判断,速度会快很多,最好,连接关系的所有key都有index
      

  4.   

    in的效率非常低,能不用就尽量避免用。
    select能不嵌套尽量不要嵌套。
    这2个指导原则,会使你的sql快很多。找点sql优化的资料看看吧
      

  5.   

    select能不嵌套尽量不要嵌套。
    但我觉得SELECT象狗皮膏药,百搭,而且是绝招,一般不轻易出手。。嘻嘻
      

  6.   

    (select e.p_sid,b.p_state,c.p_psnum,c.p_panum,c.p_mbankid 
    from p_pss b,p_mccinfo c,p_sid e 
    where b.p_id=e.p_id 
    and b.p_id=c.p_id 
    and c.p_mbankid=b.p_mbankid 
    and b.p_state in ('01','03','04','05')
    and (c.p_psnum is not null and c.p_pastate='00')) f
    将表f建成一个临时表,使用时先生成这个临时表,在和这个表做关联,就可以省去很多的遍例了
    可以用存储过程来实现临时表的生成与删除。
      

  7.   

    把in语句改成or的连接,可以考虑用视图来实现吧(个人认为)
      

  8.   

    谢谢各位的解答,我查过SQL优化的资料,对表进行了优化,还有我也把临时表f和其他表分开,临时表查找后的数据用LinkedList()保存起来,其他表的查询也一样,然后用程序来实现它们间的关联,这样给数据库带的遍历减少很多,只是在程序里增加些负担,速度快多了。