improved performance for large parameters.

This commit is contained in:
wenshao
2017-07-12 20:18:34 +08:00
parent aaa895afac
commit 145d3d3dee
5 changed files with 267 additions and 1 deletions
@@ -49,7 +49,7 @@ public class PreparedStatementProxyImpl extends StatementProxyImpl implements Pr
private PreparedStatement statement;
protected final String sql;
private JdbcParameter[] parameters = new JdbcParameter[16];
private JdbcParameter[] parameters;
private int parametersSize = 0;
private Map<Integer, JdbcParameter> paramMap = null;
@@ -57,6 +57,32 @@ public class PreparedStatementProxyImpl extends StatementProxyImpl implements Pr
super(connection, statement, id);
this.statement = statement;
this.sql = sql;
char quote = 0;
int paramCount = 0;
for (int i = 0; i < sql.length();++i) {
char ch = sql.charAt(i);
if (ch == '\'') {
if (quote == 0) {
quote = ch;
} else if (quote == '\'') {
quote =0;
}
} else if (ch == '"') {
if (quote == 0) {
quote = ch;
} else if (quote == '"') {
quote =0;
}
}
if (quote == 0 && ch == '?') {
paramCount++;
}
}
parameters = new JdbcParameter[paramCount];
}
public Map<Integer, JdbcParameter> getParameters() {
@@ -0,0 +1,30 @@
package com.alibaba.druid.bvt.proxy;
import com.alibaba.druid.proxy.jdbc.JdbcParameter;
import com.alibaba.druid.proxy.jdbc.PreparedStatementProxyImpl;
import junit.framework.TestCase;
import java.lang.reflect.Field;
/**
* Created by wenshao on 12/07/2017.
*/
public class InsertValues extends TestCase {
public void test_insert_values() throws Exception {
String sql = "insert into t (f0, f1, f2, f3, f4) values ";
for (int i = 0; i < 1000; ++i) {
if (i != 0) {
sql += ", (?, ?, ?, ?, ?)";
} else {
sql += "(?, ?, ?, ?, ?)";
}
}
PreparedStatementProxyImpl proxy = new PreparedStatementProxyImpl(null, null, sql, 0);
Field field = PreparedStatementProxyImpl.class.getDeclaredField("parameters");
field.setAccessible(true);
JdbcParameter[] params = (JdbcParameter[]) field.get(proxy);
assertEquals(5000, params.length);
}
}
@@ -0,0 +1,24 @@
package com.alibaba.druid.bvt.proxy;
import com.alibaba.druid.proxy.jdbc.JdbcParameter;
import com.alibaba.druid.proxy.jdbc.PreparedStatementProxyImpl;
import junit.framework.TestCase;
import java.lang.reflect.Field;
/**
* Created by wenshao on 12/07/2017.
*/
public class InsertValues_1 extends TestCase {
public void test_insert_values() throws Exception {
String sql = "insert into t (f0, f1, f2, f3, f4) values ('????????????????????????', '????????????????????????', '????????????????????????', '????????????????????????', '????????????????????????')";
PreparedStatementProxyImpl proxy = new PreparedStatementProxyImpl(null, null, sql, 0);
Field field = PreparedStatementProxyImpl.class.getDeclaredField("parameters");
field.setAccessible(true);
JdbcParameter[] params = (JdbcParameter[]) field.get(proxy);
assertEquals(0, params.length);
}
}
@@ -0,0 +1,24 @@
package com.alibaba.druid.bvt.proxy;
import com.alibaba.druid.proxy.jdbc.JdbcParameter;
import com.alibaba.druid.proxy.jdbc.PreparedStatementProxyImpl;
import junit.framework.TestCase;
import java.lang.reflect.Field;
/**
* Created by wenshao on 12/07/2017.
*/
public class InsertValues_2 extends TestCase {
public void test_insert_values() throws Exception {
String sql = "insert into t (f0, f1, f2, f3, f4) values (\"????????????????????????\", \"????????????????????????\", \"????????????????????????\", \"????????????????????????\", \"????????????????????????\")";
PreparedStatementProxyImpl proxy = new PreparedStatementProxyImpl(null, null, sql, 0);
Field field = PreparedStatementProxyImpl.class.getDeclaredField("parameters");
field.setAccessible(true);
JdbcParameter[] params = (JdbcParameter[]) field.get(proxy);
assertEquals(0, params.length);
}
}
@@ -0,0 +1,162 @@
/*
* Copyright 1999-2017 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.bvt.sql.oracle.select;
import com.alibaba.druid.sql.OracleTest;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.dialect.oracle.parser.OracleStatementParser;
import com.alibaba.druid.sql.dialect.oracle.visitor.OracleSchemaStatVisitor;
import org.junit.Assert;
import java.util.List;
public class OracleSelectTest69 extends OracleTest {
public void test_0() throws Exception {
String sql = //
"select count(:\"SYS_B_0\") from (select a.nsrsbh,b.xh,b.hc,b.name,b.fjxh,b.UUIDXH from JCFX_SQ_NSRCKZHZHBGB a,JXT_CLIENT_FJLIST b where b.xh=a.UUIDXH and a.UUIDXH=:\"SYS_B_1\" and b.HC>:\"SYS_B_2\") WHERE NSRSBH=:\"SYS_B_3"; //
OracleStatementParser parser = new OracleStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
SQLStatement stmt = statementList.get(0);
print(statementList);
Assert.assertEquals(1, statementList.size());
OracleSchemaStatVisitor visitor = new OracleSchemaStatVisitor();
stmt.accept(visitor);
System.out.println("Tables : " + visitor.getTables());
System.out.println("fields : " + visitor.getColumns());
System.out.println("coditions : " + visitor.getConditions());
System.out.println("relationships : " + visitor.getRelationships());
System.out.println("orderBy : " + visitor.getOrderByColumns());
Assert.assertEquals(3, visitor.getTables().size());
Assert.assertEquals(10, visitor.getColumns().size());
{
String text = SQLUtils.toOracleString(stmt);
assertEquals("INSERT INTO SB_ZZS_LDSK_2016\n" +
"SELECT DJXH, :B1\n" +
"\t, NVL(SUM(CASE \n" +
"\t\tWHEN ZZSLX = '2'\n" +
"\t\tAND LDLX = '1' THEN LDSE\n" +
"\tEND), 0) AS SQLDSE_BQ\n" +
"\t, NVL(SUM(CASE \n" +
"\t\tWHEN ZZSLX = '1'\n" +
"\t\tAND LDLX = '1' THEN LDSE\n" +
"\tEND), 0) AS SQLDSE_LJ\n" +
"\t, NVL(SUM(CASE \n" +
"\t\tWHEN LDLX = '2' THEN LDSE\n" +
"\tEND), 0) AS SQLDSE_JZ\n" +
"\t, 0, 0, 0, SYSDATE\n" +
"FROM (\n" +
"\tSELECT ZZSLX, LDLX, DJXH, SUM(BCLDBHS) AS LDSE\n" +
"\tFROM (\n" +
"\t\tSELECT ZZSLX, LDLX, DJXH, SUM(T.BCLDBHS) AS BCLDBHS\n" +
"\t\tFROM HX_SB.SB_ZZS_LDSKLSZ@JSCX T\n" +
"\t\tWHERE T.SKSSQZ < ADD_MONTHS(:B1, -1) + 1\n" +
"\t\t\tAND DJXH IN (\n" +
"\t\t\t\tSELECT NSRDZDAH\n" +
"\t\t\t\tFROM DJ_NSRXX_JS_GZSB\n" +
"\t\t\t)\n" +
"\t\t\tAND T.CZLX_DM IN (\n" +
"\t\t\t\t'10', \n" +
"\t\t\t\t'66', \n" +
"\t\t\t\t'68', \n" +
"\t\t\t\t'53', \n" +
"\t\t\t\t'56', \n" +
"\t\t\t\t'61', \n" +
"\t\t\t\t'64'\n" +
"\t\t\t)\n" +
"\t\tGROUP BY ZZSLX, LDLX, DJXH\n" +
"\t\tUNION ALL\n" +
"\t\tSELECT ZZSLX, LDLX, DJXH, -SUM(T.BCLDBHS) AS BCLDBHS\n" +
"\t\tFROM HX_SB.SB_ZZS_LDSKLSZ@JSCX T\n" +
"\t\tWHERE T.SKSSQZ = ADD_MONTHS(:B1, -1)\n" +
"\t\t\tAND DJXH IN (\n" +
"\t\t\t\tSELECT NSRDZDAH\n" +
"\t\t\t\tFROM DJ_NSRXX_JS_GZSB\n" +
"\t\t\t)\n" +
"\t\t\tAND T.CZLX_DM IN ('53', '56', '61', '64')\n" +
"\t\tGROUP BY ZZSLX, LDLX, DJXH\n" +
"\t)\n" +
"\tGROUP BY ZZSLX, LDLX, DJXH\n" +
") A\n" +
"GROUP BY DJXH", text);
}
{
String text = SQLUtils.toOracleString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION);
Assert.assertEquals("insert into SB_ZZS_LDSK_2016\n" +
"select DJXH, :B1\n" +
"\t, NVL(sum(case \n" +
"\t\twhen ZZSLX = '2'\n" +
"\t\tand LDLX = '1' then LDSE\n" +
"\tend), 0) as SQLDSE_BQ\n" +
"\t, NVL(sum(case \n" +
"\t\twhen ZZSLX = '1'\n" +
"\t\tand LDLX = '1' then LDSE\n" +
"\tend), 0) as SQLDSE_LJ\n" +
"\t, NVL(sum(case \n" +
"\t\twhen LDLX = '2' then LDSE\n" +
"\tend), 0) as SQLDSE_JZ\n" +
"\t, 0, 0, 0, sysdate\n" +
"from (\n" +
"\tselect ZZSLX, LDLX, DJXH, sum(BCLDBHS) as LDSE\n" +
"\tfrom (\n" +
"\t\tselect ZZSLX, LDLX, DJXH, sum(T.BCLDBHS) as BCLDBHS\n" +
"\t\tfrom HX_SB.SB_ZZS_LDSKLSZ@JSCX T\n" +
"\t\twhere T.SKSSQZ < ADD_MONTHS(:B1, -1) + 1\n" +
"\t\t\tand DJXH in (\n" +
"\t\t\t\tselect NSRDZDAH\n" +
"\t\t\t\tfrom DJ_NSRXX_JS_GZSB\n" +
"\t\t\t)\n" +
"\t\t\tand T.CZLX_DM in (\n" +
"\t\t\t\t'10', \n" +
"\t\t\t\t'66', \n" +
"\t\t\t\t'68', \n" +
"\t\t\t\t'53', \n" +
"\t\t\t\t'56', \n" +
"\t\t\t\t'61', \n" +
"\t\t\t\t'64'\n" +
"\t\t\t)\n" +
"\t\tgroup by ZZSLX, LDLX, DJXH\n" +
"\t\tunion all\n" +
"\t\tselect ZZSLX, LDLX, DJXH, -sum(T.BCLDBHS) as BCLDBHS\n" +
"\t\tfrom HX_SB.SB_ZZS_LDSKLSZ@JSCX T\n" +
"\t\twhere T.SKSSQZ = ADD_MONTHS(:B1, -1)\n" +
"\t\t\tand DJXH in (\n" +
"\t\t\t\tselect NSRDZDAH\n" +
"\t\t\t\tfrom DJ_NSRXX_JS_GZSB\n" +
"\t\t\t)\n" +
"\t\t\tand T.CZLX_DM in ('53', '56', '61', '64')\n" +
"\t\tgroup by ZZSLX, LDLX, DJXH\n" +
"\t)\n" +
"\tgroup by ZZSLX, LDLX, DJXH\n" +
") A\n" +
"group by DJXH", text);
}
// Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("acduser.vw_acd_info", "xzqh")));
// Assert.assertTrue(visitor.getOrderByColumns().contains(new TableStat.Column("employees", "last_name")));
}
}