Quick revision
The short answer
The JDBC driver sends the String parameter as NVARCHAR by default, so SQL Server has to convert the VARCHAR column to NVARCHAR before comparing (an implicit conversion), and depending on the collation that prevents a normal index seek. The fix is sendStringParametersAsUnicode=false in the URL when the columns are VARCHAR, or making column and parameter types match. Two other causes must be ruled out: parameter sniffing, a cached plan built for an unusual value, which the actual plan and Query Store reveal, and the fact that SSMS uses different SET options, such as ARITHABORT, so it may get a different plan; Hibernate's nationalized setting also changes which type is sent.
What to remember
In this answer, we saw that the slow query was not the same query SSMS ran: the JDBC driver sends Strings as NVARCHAR by default, so SQL Server converted the VARCHAR column with CONVERT_IMPLICIT and, under a SQL collation, could not seek the index. We fixed it with sendStringParametersAsUnicode=false or by aligning column and parameter types, reproduced it properly with sp_executesql, ruled out parameter sniffing with the actual plan and Query Store, remembered that SSMS's different SET options such as ARITHABORT give it its own plan, and checked Hibernate's nationalized setting.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
The JDBC driver sends the String parameter as NVARCHAR by default, so SQL Server has to convert the VARCHAR column to NVARCHAR before comparing (an implicit conversion), and depending on the collation that prevents a normal index seek. The fix is sendStringParametersAsUnicode=false in the URL when the columns are VARCHAR, or making column and parameter types match. Two other causes must be ruled out: parameter sniffing, a cached plan built for an unusual value, which the actual plan and Query Store reveal, and the fact that SSMS uses different SET options, such as ARITHABORT, so it may get a different plan; Hibernate's nationalized setting also changes which type is sent.
Why this question is asked
'It is fast in SSMS but slow from the application' is one of the most common SQL Server performance puzzles. It usually means the two are not actually running the same query, even though the SQL text looks identical. Interviewers ask this to see whether we can find the real difference instead of guessing. In this answer, we will reproduce the problem the way the driver really sends it, read the plan, fix the type mismatch, and then look at the two other usual suspects, parameter sniffing and session settings, and at Hibernate's role.
1What the Application Really Sends
In SSMS, we typed the value as a literal, WHERE customer_code = 'C-1001', a VARCHAR. The application used a PreparedStatement with setString, and the Microsoft JDBC driver sends String parameters as NVARCHAR(4000) by default, because the connection property sendStringParametersAsUnicode is true unless we change it. So on the server, the application's query is really this:
Running the query through sp_executesql with the same parameter type is the most important debugging step, because it is the only way to compare like with like.
2Why the Type Mismatch Prevents a Seek
When SQL Server compares values of two types, it converts the one with lower type precedence to the higher one. NVARCHAR has higher precedence than VARCHAR, so it is the column that gets converted, on every row, not the single parameter value. The plan shows this as CONVERT_IMPLICIT(nvarchar(20), customer_code) in the predicate, with a warning that the conversion may affect the seek plan.
Whether this actually forces a scan depends on the column's collation. With a legacy SQL collation such as SQL_Latin1_General_CP1_CI_AS, VARCHAR and NVARCHAR sort differently, so the index order cannot be used for the converted values and SQL Server scans every row. With a Windows collation such as Latin1_General_CI_AS, the optimizer can often still seek by computing a range from the parameter, at some extra cost. A 2-second scan suggests the first case.
- Capture the real statementQuery Store, Extended Events, or driver logging
- Reproduce with sp_executesqlsame parameter types as the driver
- CONVERT_IMPLICIT on the column?YesType mismatch: align typesNoCheck the next cause
- Plan built for an unusual value?YesParameter sniffing: fix the plan choiceNoCompare SET options
- Compare SET optionsARITHABORT and others differ between SSMS and JDBC
5 steps in order. Play them, or step through them one at a time.
3Fixing the Type Mismatch
If the database uses VARCHAR for these columns, we tell the driver to send Strings as VARCHAR. This is a connection property, so it applies to every String parameter on that connection.
There is a catch: with the property false, any String bound with setString is sent as VARCHAR, so characters outside the column's code page can be lost when the column is NVARCHAR. If the database mixes both types, the code must use setNString for the NVARCHAR columns. The alternative fix runs the other way: change the column to NVARCHAR so the types match, which costs more storage and an index rebuild, but removes the mismatch for good. What matters is that the parameter and the column have the same type.
4Ruling Out Parameter Sniffing
A second common cause looks the same from outside. When SQL Server compiles a parameterised query, it builds the plan for the first parameter value it sees, called sniffing, and caches that plan for later calls. If the first value was unusual, say a customer code shared by half the table, the cached plan may be a scan, and every later call reuses it even for values that match one row.
The way to tell the difference is the actual execution plan of the slow call, which shows the compiled parameter value next to the runtime value, and Query Store, which keeps the history of plans and their run times for each query. If Query Store shows the query switched to a worse plan at some point, sniffing is likely. Fixes include forcing the good plan in Query Store, OPTION (RECOMPILE) for queries where values vary widely, or OPTIMIZE FOR hints, each with its own costs.
5Why SSMS Can Get a Different Plan Anyway
Even with the same parameter types, SSMS and the JDBC driver use different session SET options. The best known is ARITHABORT, which SSMS turns ON while most drivers leave it OFF. SET options are part of the plan cache key, so SSMS compiles its own fresh plan, for its own sniffed value, while the application keeps using its cached one. That is why 'it is fast in SSMS' proves very little. To compare fairly, we set the same options in SSMS as the application uses, or look at the application's actual plan from Query Store or the plan cache instead of creating a new one.
6Where Hibernate Fits In
With Hibernate the same rules apply, with one more switch. By default Hibernate binds Java Strings with setString, so the driver property decides the type. If hibernate.use_nationalized_character_data is true, or a field is annotated @Nationalized, Hibernate binds with setNString and maps the column as NVARCHAR, so those parameters stay NVARCHAR even with sendStringParametersAsUnicode=false. Check your version's mapping, and keep the entity mapping consistent with the real column types, so VARCHAR columns are bound as VARCHAR and NVARCHAR columns as NVARCHAR.
7Follow-up questions
How do you capture the exact statement the application sends?
Query Store records the parameterised text of every query, including the parameter declaration such as @P0 nvarchar(4000). An Extended Events session on rpc_completed captures each call with its parameters, and the driver's own logging can show what it sends from the Java side.
Why not just add OPTION (RECOMPILE) to the query?
It solves parameter sniffing, because each call gets a plan for its own value, but it does not fix a type mismatch: a recompiled plan with CONVERT_IMPLICIT on the column still scans. It also costs compile time on every call, so it suits occasional queries better than very frequent ones.
Does the same problem appear with other types?
Yes, any mismatch between parameter and column type can cause it, for example a DATETIME2 parameter against a DATETIME column, or a BIGINT against an INT. The rule is always to bind parameters with the column's exact type.
8Conclusion
In this answer, we saw that the slow query was not the same query SSMS ran: the JDBC driver sends Strings as NVARCHAR by default, so SQL Server converted the VARCHAR column with CONVERT_IMPLICIT and, under a SQL collation, could not seek the index. We fixed it with sendStringParametersAsUnicode=false or by aligning column and parameter types, reproduced it properly with sp_executesql, ruled out parameter sniffing with the actual plan and Query Store, remembered that SSMS's different SET options such as ARITHABORT give it its own plan, and checked Hibernate's nationalized setting.
Open with
The SSMS test and the application are not running the same query. In SSMS the value was a VARCHAR literal. From Java, setString with the Microsoft driver sends an NVARCHAR(4000) parameter, because sendStringParametersAsUnicode defaults to true.
Then, if they want more
NVARCHAR has higher type precedence, so SQL Server converts the column, not the parameter, and the plan shows CONVERT_IMPLICIT on customer_code. With a SQL collation that conversion prevents a normal seek, so it scans, which is the two seconds. First I would reproduce it properly with sp_executesql and an nvarchar parameter, and look at the actual plan.
The fix is to align types: set sendStringParametersAsUnicode=false in the URL if the database uses VARCHAR, and use setNString for real NVARCHAR columns, or change the column to NVARCHAR. I would also rule out two other causes.
Parameter sniffing, where the cached plan was built for an unusual value; the actual plan shows the compiled value and Query Store shows plan changes over time, and fixes include forcing a plan or RECOMPILE.
And SET options: SSMS runs with ARITHABORT ON and drivers usually do not, so SSMS compiles its own plan, which is why fast in SSMS proves little. With Hibernate, I would check whether the nationalized setting or @Nationalized makes it bind NVARCHAR on purpose.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- PostgreSQL documentation, MySQL reference manual
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "A query on SQL Server filters on a VARCHAR(20) indexed column with a JDBC...": https://scrivio-interview-library.netlify.app/databases/oracle-and-sql-server/d3246615ee/