---
title: "Jdbc执行很慢，而PLSQL执行快的问题解决"
author: "luweifeng1983"
date: "2016-04-14T13:51:50Z"
tags: []
categories: ["编程语言"]
slug: "jdbc-plsql"
draft: false
lastmod: "2016-04-14T13:55:44Z"
id: 2291101
url: "https://quicker.iteye.com/blog/2291101"
---

最近在检查一方法时发现程序执行SQL查询时非常慢，但使用PLSQL DEV工具执行查询语句又很快。

看以下代码：

```
public boolean isExistSonoByStoreCode(String storeCode, String soNo, String billId)
    {
    	StringBuffer sql = new StringBuffer();
    	sql.append("select bill_id, so_no from store_in_head where store_code = ? and UPPER(so_no) = UPPER(?) ");
    	
        DBUtil dbu = null;
        PreparedStatement preStmt = null;
        ResultSet rs = null;
        boolean isExistSono = false;
        
        try 
        {
            dbu = new DBUtil();
            preStmt = dbu.getConnection().prepareStatement(sql.toString());
            preStmt.setString(1, storeCode);
            preStmt.setString(2, soNo);
            rs = preStmt.executeQuery();
            while (rs.next()) 
            {
            	String oldBillId = rs.getString("bill_id");
            	
            	if (!oldBillId.equalsIgnoreCase(billId))
            	{
            		isExistSono = true;
            	}
            }
        }
        catch (Exception e) 
        {
            log.error(e.getMessage(),e);
        }
        finally 
        {
            try 
            {
                rs.close();
            }
            catch (Exception e) 
            {
            	;
            }
            
            try 
            {
                preStmt.close();
            }
            catch (Exception e) 
            {
            	;
            }
            closeDButil(dbu);
        }
       
    	return isExistSono;
    }
```

 经DEBUG发现速度慢确实发生在查询时，即执行rs = preStmt.executeQuery();时

一开始怀疑是语句使用UPER函数导致的，但是把语句修改为不使用函数也一样慢。。怀疑是使用了PreparedStatement 参数需要动态绑定问题，于是将PreparedStatement 改为Statement ，执行却很快，修改后的代码如下：

```
 public boolean isExistSonoByStoreCode(String storeCode, String soNo, String billId)
    {
    	StringBuffer sql = new StringBuffer();
    	sql.append("select bill_id, so_no from store_in_head where store_code = '" + storeCode + "' and UPPER(so_no) = UPPER('"+ soNo +"') ");
    	
        DBUtil dbu = null;
        Statement preStmt = null;
        ResultSet rs = null;
        boolean isExistSono = false;
        
        try 
        {
            dbu = new DBUtil();
            preStmt = dbu.getConnection().createStatement();
            rs = preStmt.executeQuery(sql.toString());
//            preStmt.setString(1, storeCode);
//            preStmt.setString(2, soNo);
//            rs = preStmt.executeQuery();
            while (rs.next()) 
            {
            	String oldBillId = rs.getString("bill_id");
            	
            	if (!oldBillId.equalsIgnoreCase(billId))
            	{
            		isExistSono = true;
            	}
            }
        }
        catch (Exception e) 
        {
            log.error(e.getMessage(),e);
        }
        finally 
        {
            try 
            {
                rs.close();
            }
            catch (Exception e) 
            {
            	;
            }
            
            try 
            {
                preStmt.close();
            }
            catch (Exception e) 
            {
            	;
            }
            closeDButil(dbu);
        }
       
    	return isExistSono;
    }
```

 字段类型：STORE\_CODE CHAR(3)，SO\_NO VARCHAR2(100)

类似问题：[http://bbs.csdn.net/topics/320181076](http://bbs.csdn.net/topics/320181076)