Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prepared statement performance fixes #11

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
final class ParsedSQLCacheItem {
/** The SQL text AFTER processing. */
String processedSQL;
int parameterCount;
int[] parameterPositions;
String procedureName;
boolean bReturnValueSyntax;

ParsedSQLCacheItem(String processedSQL, int parameterCount, String procedureName, boolean bReturnValueSyntax) {
ParsedSQLCacheItem(String processedSQL, int[] parameterPositions, String procedureName, boolean bReturnValueSyntax) {
this.processedSQL = processedSQL;
this.parameterCount = parameterCount;
this.parameterPositions = parameterPositions;
this.procedureName = procedureName;
this.bReturnValueSyntax = bReturnValueSyntax;
}
Expand Down
52 changes: 34 additions & 18 deletions src/main/java/com/microsoft/sqlserver/jdbc/SQLServerConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import java.sql.Statement;
import java.sql.Struct;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.HashMap;
Expand Down Expand Up @@ -123,11 +124,13 @@ static class CityHash128Key {

CityHash128Key(String sql,
String parametersDefinition) {
this(String.format("%s%s", sql, parametersDefinition));
this(sql + parametersDefinition);
}

CityHash128Key(String s) {
segments = CityHash.cityHash128(s.getBytes(), 0, s.length());
byte[] bytes = new byte[s.length()];
s.getBytes(0, s.length(), bytes, 0);
segments = CityHash.cityHash128(bytes, 0, bytes.length);
Copy link

@brettwooldridge brettwooldridge Jun 27, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@RDearnaley I would suggest going ahead with the slightly safer:

byte[] bytes = new byte[s.length() * 2];
for (int i = 0; i < s.length(); i++) {
   final int c = s.charAt(i);
   bytes[i * 2] = (byte) c;
   bytes[(i * 2) + 1] = (byte)(c >> 8);
}
segments = CityHash.cityHash128(bytes, 0, bytes.length);

1 Receiving the charAt() into an int avoids one implicit conversion, as shift operators are not supported on char directly and the (byte) c cast also likely inflates to an int before casting.

}

public boolean equals(Object obj) {
Expand Down Expand Up @@ -260,9 +263,9 @@ static ParsedSQLCacheItem parseAndCacheSQL(CityHash128Key key, String sql) thro
String parsedSql = translator.translate(sql);
String procName = translator.getProcedureName(); // may return null
boolean returnValueSyntax = translator.hasReturnValueSyntax();
int paramCount = countParams(parsedSql);
int[] parameterPositions = locateParams(parsedSql);

ParsedSQLCacheItem cacheItem = new ParsedSQLCacheItem (parsedSql, paramCount, procName, returnValueSyntax);
ParsedSQLCacheItem cacheItem = new ParsedSQLCacheItem (parsedSql, parameterPositions, procName, returnValueSyntax);
parsedSQLCache.putIfAbsent(key, cacheItem);
return cacheItem;
}
Expand All @@ -283,22 +286,28 @@ static ParsedSQLCacheItem parseAndCacheSQL(CityHash128Key key, String sql) thro
private boolean disableStatementPooling = true;

/**
* Find statement parameters.
* Locate statement parameters.
*
* @param sql
* SQL text to parse for number of parameters to intialize.
* SQL text to parse for positions of parameters to intialize.
*/
private static int countParams(String sql) {
int nParams = 0;

// Figure out the expected number of parameters by counting the
// parameter placeholders in the SQL string.
int offset = -1;
while ((offset = ParameterUtils.scanSQLForChar('?', sql, ++offset)) < sql.length())
++nParams;

return nParams;
}
private static int[] locateParams(String sql) {
List<Integer> parameterPositions = new ArrayList<Integer>(0);

// Locate the parameter placeholders in the SQL string.
int offset = -1;
while ((offset = ParameterUtils.scanSQLForChar('?', sql, ++offset)) < sql.length()) {
parameterPositions.add(offset);
}

// Convert to int[]
int[] result = new int[parameterPositions.size()];
int i = 0;
for (Integer parameterPosition : parameterPositions) {
result[i++] = parameterPosition;
}
return result;
}

SqlFedAuthToken getAuthenticationResult() {
return fedAuthToken;
Expand Down Expand Up @@ -5309,6 +5318,7 @@ public void endRequest() throws SQLFeatureNotSupportedException {
static final char[] OUT = {' ', 'O', 'U', 'T'};

/* L0 */ String replaceParameterMarkers(String sqlSrc,
int[] paramPositions,
Parameter[] params,
boolean isReturnValueSyntax) throws SQLServerException {
final int MAX_PARAM_NAME_LEN = 6;
Expand All @@ -5319,7 +5329,13 @@ public void endRequest() throws SQLFeatureNotSupportedException {

int paramIndex = 0;
while (true) {
int srcEnd = ParameterUtils.scanSQLForChar('?', sqlSrc, srcBegin);
int srcEnd;
if (paramIndex >= paramPositions.length) {
srcEnd = sqlSrc.length();
}
else {
srcEnd = paramPositions[paramIndex];
}
sqlSrc.getChars(srcBegin, srcEnd, sqlDst, dstBegin);
dstBegin += srcEnd - srcBegin;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,7 @@ private void checkClosed() throws SQLServerException {
if (con.getServerMajorVersion() >= SQL_SERVER_2012_VERSION) {
// new implementation for SQL verser 2012 and above
String preparedSQL = con.replaceParameterMarkers(((SQLServerPreparedStatement) stmtParent).userSQL,
((SQLServerPreparedStatement) stmtParent).userSQLParamPositions,
((SQLServerPreparedStatement) stmtParent).inOutParam, ((SQLServerPreparedStatement) stmtParent).bReturnValueSyntax);

SQLServerCallableStatement cstmt = (SQLServerCallableStatement) con.prepareCall("exec sp_describe_undeclared_parameters ?");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ public class SQLServerPreparedStatement extends SQLServerStatement implements IS
/** Processed SQL statement text, may not be same as what user initially passed. */
final String userSQL;

/** Parameter positions in processed SQL statement text. */
final int[] userSQLParamPositions;

/** SQL statement with expanded parameter tokens */
private String preparedSQL;

Expand Down Expand Up @@ -204,7 +207,8 @@ String getClassNameInternal() {
procedureName = parsedSQL.procedureName;
bReturnValueSyntax = parsedSQL.bReturnValueSyntax;
userSQL = parsedSQL.processedSQL;
initParams(parsedSQL.parameterCount);
userSQLParamPositions = parsedSQL.parameterPositions;
initParams(userSQLParamPositions.length);
}

/**
Expand Down Expand Up @@ -342,7 +346,7 @@ private boolean buildPreparedStrings(Parameter[] params,
preparedTypeDefinitions = newTypeDefinitions;

/* Replace the parameter marker '?' with the param numbers @p1, @p2 etc */
preparedSQL = connection.replaceParameterMarkers(userSQL, params, bReturnValueSyntax);
preparedSQL = connection.replaceParameterMarkers(userSQL, userSQLParamPositions, params, bReturnValueSyntax);
if (bRequestedGeneratedKeys)
preparedSQL = preparedSQL + identityQuery;

Expand Down