我的数据量如果达到几十万的话 程序反应很慢跟死了一样
具体如下:
//得到查询后所有未定制该栏目客户的总数
public List getCustomerCount(String customerName,String mobile,int groupId,int proId,String whe){
init();
List list = new ArrayList();
StringBuffer sb = new StringBuffer("select a.customerid from b_customer a" +
" where a.customerid not in(select customerid from b_customer_program_relation where proid="+proId+") "+whe+" ");
if(groupId!=0){
sb.delete(0, sb.length());
sb.append("select a.customerid from b_customer a, b_customer_group_relation b,"
                 +" b_customer_group c where a.customerid not in(select customerid from b_customer_program_relation where proid="+proId+") and b.GroupID="+groupId+" " 
                 + " and  b.GroupID=c.GroupID and b.CustomerID=a.CustomerID "
                 +" "+whe+"   ");
}
if(!customerName.equals("") && customerName!=null){
sb.append("and a.name like '%"+customerName+"%' ");
}
if(!mobile.equals("") && mobile!=null){
sb.append(" and a.mobile="+mobile+" ");
}

try{
pstmt=con.prepareStatement(sb.toString());
rs=pstmt.executeQuery();
while(rs.next()){
list.add(String.valueOf(rs.getInt(1)));
}
}catch(Exception e){
e.printStackTrace();
}finally{
try{
rs.close();
pstmt.close();
close();
}catch(Exception ex){
ex.printStackTrace();
}
}

return list;

}哪为朋友帮忙优化下 一旦可以提高工作效率马上给分谢谢.

解决方案 »

  1.   

    b_customer表有用的字段:  `CustomerID` int(11) NOT NULL auto_increment,
                   `UserID` int(11) NOT NULL,
                    PRIMARY KEY  (`CustomerID`)
    b_customer_program_relation 表字段:`ProID` int(11) NOT NULL,
                       `CustomerID` int(11) NOT NULL
    我个人觉得我的sql语句中含有not in,大家有没有什么好的办法替换not in的功能语句.if语句里的sql都不需要考虑,就是头一条sql优化下.
      

  2.   

    恩 就是一条sql语句select a.customerid from b_customer a where a.customerid not in(select customerid from b_customer_program_relation where proid=1)如果查询结果有几十万条数据的话就死掉了。 我觉得这条sql 写的不好,所以想让大家帮忙优化下提高效率!
      

  3.   

    select a.customerid 
    from b_customer a 
    where not exists 
    (
    select 1 
    from b_customer_program_relation b 
    where a. customerid = b.customerid 
    and b.proid = ?
    )
      

  4.   

    select distinct a.customerid from b_customer A left outer join b_customer_program_relation B on   A.customerid = B.customerid and  B.proid !=1
      

  5.   

    select  A.customerid from b_customer A ,b_customer_program_relation B where  A.customerid = B.customerid and  B.proid !=1
    这个,上面写错了
      

  6.   

    千万不要in 、not in!!!都改exsit
      

  7.   

    不知道您用的是ORACLE还是DB2所以把两种都写了一下.
    Oracle:
    select 
        a.customerid 
    from 
        b_customer a,b_customer_program_relation b
    where 
        a.customerid = b.customerid
    and 
        b.proid!=1
    DB2:
    select 
        a.customerid 
    from 
        b_customer a 
    inner join 
        b_customer_program_relation b
    on
    where 
        a.customerid = b.customerid
    and 
        proid!=1
      

  8.   

    笔误,更正一下:
    不知道您用的是ORACLE还是DB2所以把两种都写了一下.
    Oracle:
    select 
        a.customerid 
    from 
        b_customer a,b_customer_program_relation b
    where 
        a.customerid = b.customerid
    and 
        b.proid!=1
    DB2:
    select 
        a.customerid 
    from 
        b_customer a 
    inner join 
        b_customer_program_relation b
    where 
        a.customerid = b.customerid
    on
        proid!=1
      

  9.   

    非常感谢 youbin_()  只有你一个人写的是对的其余都错
     我的b_customer_program_relation表是多对多的,如果有proid!=1,但在别的proid里肯定有不在proid!=1的customerid. 结贴!
      

  10.   

    哦忘了告诉大家了 我用的是mysql数据库,自己顶下.希望高人来指点
      

  11.   

    youbin_()如果还不能解决的话,就只能写存储过程了。
    mysql5.0支持存储过程
      

  12.   

    在编写SQL语句时,如果要实现一张表有而另一张表没有的数据库时,通常第一直觉的写法就是:
    select * from table1 where table1.id not in (select id from table2),这种方法虽然很直观,但是in及not in的写法经常会影响其执行的效率,对于大数据量时,这个原因经常是性能的瓶颈。可以通过左连接的方法来解决,其替代写法如下:
    select a.* from table1 a left join table2 b on a.id=b.id where b.id is null
    同理,这个方法也适用于in的情况。
      

  13.   

    不用not in的话用not exit
      

  14.   

    我自己顶下还是没人帮我解决啊.
    比如说吧 我表里有b_customer 有三条记录 customerid分别为1,2,3  在表b_customer_program_relation有记录1,1和1,2  和2,1和 2,2和2,3
    当proid!=1的时候我只要查出customerid为3. 不要用 not in 和 not exists语句.在线等待!
      

  15.   

    解决效率问题是要在保证逻辑正确的情况下才可以的!!
    在大数据量下not in 和 not exists的效率差别是很大的!!