DB2 char()函数引起全表扫描

解决方法是情愿多几次访问数据库,增加 Where 条件 缩小检索范围,不要用连接

原代码:

List l = new ArrayList();  
StringBuffer sql = new StringBuffer(); 

sql.append("select log.LOGID,log.LASID,customerinfo.REALNAME,customer.IDTYPE,
            char(date(log.OPERATEDATE))||' '||char(time(log.OPERATEDATE)),log.OPERATETYPE,");  
sql.append("log.OPERATECODE,log.PRINTSTATE,log.FIRSTPRINTDATE,log.LASTPRINTDATE,log.LASTPRINTCODE,"); 
sql.append("log.EMAILSTATE,log.FIRSTEMAILDATE,log.LASTEMAILDATE,log.LASTEMAILCODE,log.PRINTID,"); 
sql.append("log.EMAILID,log.FLAG,customer.IDNO,customerinfo.SEX,customer.email,customer.mobile 
            from 日志表 as log, 客户表 as customer, 客户详情表 as customerinfo 
            
where log.LASID = char(customer.CUSTOMERID) 
and char(customer.CUSTOMERID)= char(customerinfo.CUSTOMERID)");

中 Where  条件 用到了 char() 函数,原来 DB2 中 where 条件中 引用函数会引起全表扫描,逻辑没有什么错,就是线程卡在这里。

修改: 分两部分 先将 顾客ID搜出来,再去搜日志

  1.先将 顾客ID搜出来

select customer.CUSTOMERID from 客户表 as customer, 客户详情表 as customerinfo 
where customer.CUSTOMERID= customerinfo.CUSTOMERID for read only with ur

  2.再去搜日志

StringBuffer sql = new StringBuffer(); 

sql.append("select log.LOGID,log.LASID,customerinfo.REALNAME,customer.IDTYPE, 
            char(date(log.OPERATEDATE))||' '||char(time(log.OPERATEDATE)),log.OPERATETYPE,"); 
sql.append("log.OPERATECODE,log.PRINTSTATE,log.FIRSTPRINTDATE,log.LASTPRINTDATE,log.LASTPRINTCODE,"); 
sql.append("log.EMAILSTATE,log.FIRSTEMAILDATE,log.LASTEMAILDATE,log.LASTEMAILCODE,log.PRINTID,");  
sql.append("log.EMAILID,log.FLAG,customer.IDNO,customerinfo.SEX,customer.email,customer.mobile 
            from 日志表 as log, 客户表 as customer,客户详情表 as customerinfo 
            
where log.LASID in ("); 	
for(Object CustomerId : CustomerIds){
    sql.append("'"+CustomerId.toString()+"',"); 
}
sql.deleteCharAt(sql.lastIndexOf(","));
sql.append(") AND for read only with ur");

记得加上 " for read only with ur "


转载于:https://my.oschina.net/u/2277088/blog/538782

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值