gpt4 book ai didi

jdbc - Oracle defaultNChar=true SLOW on NCHAR/NVARCHAR2

转载 作者:行者123 更新时间:2023-12-03 21:39:29 49 4
gpt4 key购买 nike

我正在使用带有一组参数的 JDBC Prepared Statement 使用 setString(pos, value)。表上的基础列都是 NCHAR 和 NVARCHAR2。我已设置 Oracle JDBC 驱动程序的“defaultNChar=true”,以便 Oracle DB 始终将我的参数视为本国语言字符。驱动程序文件是“ojdbc6.jar”。

我的问题:我的参数化查询是 “defaultNChar=true” 非常慢 .但是一旦我设置了 “defaultNChar=false” 查询超快 (3 秒)。

查询用法如下所示:

String sql = "INSERT INTO MYTABLE_ERROR(MY_NAME,MY_FLAG,MY_VALUE) "
+ "SELECT ? AS MY_NAME,"
+ "? AS MY_FLAG,v.MY_VALUE"
+ " FROM OTHER_TABLE v"
+ " JOIN ( SELECT * FROM ... iv ... WHERE iv.MY_NAME = ? ) rule1 "
+ " ON v.\"MY_NAME\"=rule1.\"MY_NAME\" AND v.\"MY_VALUE\"=rule1.\"MY_VALUE\""
+ " WHERE rule1.\"MY_NAME\" = ? AND v.\"MY_VALUE\" = ?";

preStatement = conn.prepareStatement (sql);
int count = 1;
for (String p : params)
{
// SLOW
//preStatement.setNString (count++, p);
// SLOW
//preStatement.setObject (count++, p, Types.NVARCHAR);
// SLOW
preStatement.setString (count++, p);
}

我一直在试图找出为什么我的准备好的语句对“Oracle Database 11g Release 11.2.0.3.0 - 64bit Production”数据库执行时使用 JDBC 驱动程序“Oracle JDBC driver, 11.2.0.3.0”很慢的根本原因。我找不到任何线索!

我什至得到了 DB NLS 配置,希望能找到任何东西,但我也不确定:
NLS_LANGUAGE    AMERICAN
NLS_TERRITORY AMERICA
NLS_CURRENCY $
NLS_ISO_CURRENCY AMERICA
NLS_NUMERIC_CHARACTERS .,
NLS_CALENDAR GREGORIAN
NLS_DATE_FORMAT DD-MON-RR
NLS_DATE_LANGUAGE AMERICAN
NLS_CHARACTERSET AL32UTF8
NLS_SORT BINARY
NLS_TIME_FORMAT HH.MI.SSXFF AM
NLS_TIMESTAMP_FORMAT DD-MON-RR HH.MI.SSXFF AM
NLS_TIME_TZ_FORMAT HH.MI.SSXFF AM TZR
NLS_TIMESTAMP_TZ_FORMAT DD-MON-RR HH.MI.SSXFF AM TZR
NLS_DUAL_CURRENCY $
NLS_NCHAR_CHARACTERSET AL16UTF16
NLS_COMP BINARY

请帮忙!

谢谢,
G。

更新:以某种方式使用“defaultNChar=true”时,查询似乎卡住了。我在使用 JConsole 时看到了这一点:
Total blocked: 1  Total waited: 1

Stack trace:
java.net.SocketInputStream.socketRead0(Native Method)
java.net.SocketInputStream.read(Unknown Source)
java.net.SocketInputStream.read(Unknown Source)
oracle.net.ns.Packet.receive(Packet.java:311)
oracle.net.ns.DataPacket.receive(DataPacket.java:103)
oracle.net.ns.NetInputStream.getNextPacket(NetInputStream.java:312)
oracle.net.ns.NetInputStream.read(NetInputStream.java:257)
oracle.net.ns.NetInputStream.read(NetInputStream.java:182)
oracle.net.ns.NetInputStream.read(NetInputStream.java:99)
oracle.jdbc.driver.T4CSocketInputStreamWrapper.readNextPacket(T4CSocketInputStreamWrapper.java:121)
oracle.jdbc.driver.T4CSocketInputStreamWrapper.read(T4CSocketInputStreamWrapper.java:77)
oracle.jdbc.driver.T4CMAREngine.unmarshalUB1(T4CMAREngine.java:1173)
oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:309)
oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.java:200)
oracle.jdbc.driver.T4C8Oall.doOALL(T4C8Oall.java:543)
oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:238)
oracle.jdbc.driver.T4CPreparedStatement.executeForRows(T4CPreparedStatement.java:1446)
oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1757)
oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:4372)
oracle.jdbc.driver.OraclePreparedStatement.executeUpdate(OraclePreparedStatement.java:4539)
- locked oracle.jdbc.driver.T4CConnection@7f2315e5
oracle.jdbc.driver.OraclePreparedStatementWrapper.executeUpdate(OraclePreparedStatementWrapper.java:5577)
com.mycompany.test.DriverTest.fireStatement(DriverTest.java:253)

最佳答案

您可以使用 PreparedStatement.setFormOfUse对于性能问题:

However, if you set defaultNChar to true and then access CHAR columns, then the database will implicitly convert all CHAR data into NCHAR. This conversion has a substantial performance impact. To avoid this, call setFormOfUse(4,OraclePreparedStatement.FORM_CHAR) for each CHAR column referred to in the statement. For example:

    PreparedStatement pstmt =
conn.prepareStatement("insert into TEST values(?,?,?)");
pstmt.setInt(1, 1); // NUMBER column
pstmt.setString(2, myUnicodeString1); // NVARCHAR2 column
pstmt.setString(3, myUnicodeString2); // NCHAR column
pstmt.setFormOfUse(4, OraclePreparedStatement.FORM_CHAR);
pstmt.setString(4, myString); // CHAR column
pstmt.execute();


https://docs.oracle.com/cd/B19306_01/java.102/b14355/global.htm#CHDHHJDB

关于jdbc - Oracle defaultNChar=true SLOW on NCHAR/NVARCHAR2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27398301/

49 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com