在写java的时候,有一个方法查询速度比其他方法慢很多,但在数据库查询很快,原来是因为程序中使用参数化查询时参数类型错误的原因
1 select * 2 from TransactionNo, 3 fmis_AccountRecord AccountRecord, 4 UserInfo InputUser, 5 UserInfo CheckUser, 6 transspecialoperation a, 7 AccountInfo c 8 where InputUser.ID(+) = TransactionNo.nInputUserID 9 and CheckUser.ID(+) = TransactionNo.nCheckUserID 10 and TransactionNo.id(+) = AccountRecord.nTransactionNoID 11 and a.nTransactionNoID = TransactionNo.ID 12 and AccountRecord.nDirectionID = 2 13 and AccountRecord.nOfficeID = 1 14 and AccountRecord.nCurrencyID = 1 15 and a.npayaccountid = c.ID(+)18 and AccountRecord.dtExecute between ? and ? 21 and nvl(AccountRecord.nStatusID, 0) >= 0 22 and TransactionNo.nTransactionTypeID = 559
修改后程序很快:
// ps.setTimestamp(1, tsDateStart); // ps.setTimestamp(2, tsDateEnd); ps.setDate(1, new java.sql.Date(tsDateStart.getTime())); ps.setDate(2, new java.sql.Date(tsDateEnd.getTime()));
参考c#:
代码如下
string sql = "SELECT * FROM LIS_V_LABTESTSAMPLE WHERE PATIENT_ID=:P";
HlsAccess.CommandText = sql;
HlsAccess.Parameters.Clear();
HlsAccess.Parameters.Add(":P", Oracle.DataAccess.Client.OracleDbType.Varchar2).Value = patId;
这里指定了参数的类型为varchar2,但是数据库确实number,这时就会造成字段类型错误导致了表扫描,最后导致查询速度慢。
原文地址:https://www.cnblogs.com/tashaxing/p/9217114.html
时间: 2024-11-07 23:25:37