sql语句优化
我的数据量如果达到几十万的话 程序反应很慢跟死了一样
具体如下:
//得到查询后所有未定制该栏目客户的总数
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;
}
哪为朋友帮忙优化下 一旦可以提高工作效率马上给分谢谢.
------解决方案--------------------因为不知道表的结构,所以...无从考虑.
------解决方案--------------------首先要给大家讲明白你要干什么,看着头大
------解决方案--------------------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 = ?
)
------解决方案--------------------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
------解决方案--------------------千万不要in 、not in!!!都改exsit
------解决方案--------------------不知道您用的是ORACLE还是DB2所以把两种都写了一下.
Oracle:
select
a.customerid
from
b_customer a,b_customer_program_relation b
where
a.customerid = b.customerid