Edmond wrote:
Hi all
I found that using PreparedStatement and ? placeholder with the MSSQL server will result in serious performance degradation - factor of 5 - 8 slower. The query run much faster if you are not using the ? placeholder and assemble the query manually every time you make the query.
The issue is because your database is using varchar, but your database drivers are all using nvarchar parameters. Java supports unicode, so invariably all the TDS-related drivers default to using nvarchar. (This really has nothing to do with Hibernate.)
The problem is when you use an nvarchar parameter to search a varchar column, SQL Server does the convert the wrong way. Instead of converting your parameter to varchar to match the column, it converts the column to nvarchar. This means you lose any indexes and the system does a full table scan. You can pull out SQL Trace to watch the queries come through. Copy and paste these exact queries into SQL Query tool, and you'll see the table scans.
The trick is (as mentioned earlier) is to set your
database driver to pass parameters as varchar. I know for Inet software drivers, you add an "a" (to mean ascii), so your JDBC URL would look like "jdbc:inetdae7a:<host>"