denisenkom / go-mssqldb

Microsoft SQL server driver written in go language
BSD 3-Clause "New" or "Revised" License
1.81k stars 493 forks source link

Default string data type should be VARCHAR #724

Open LK opened 2 years ago

LK commented 2 years ago

We have a database with an indexed VARCHAR column, and we noticed significant performance degradation as the size of the table grew. After some digging, we realized this is caused by the fact that the default string data type is NVARCHAR, which has a higher data type precedence than VARCHAR — this causes SQL server to use the following query plan when performing the query:

image

This happens because SQL Server must cast every value in the index to an NVARCHAR to perform the comparison.

image

When we changed the type of the parameter to an mssql.VarChar, we saw the expected query plan and query performance was as expected:

image

Because SQL Server will always convert up from VARCHAR to NVARCHAR (because of data type precedence), it would make sense for the default string type to be VARCHAR to avoid unintentionally triggering this edge case.

sinhashubham95 commented 5 months ago

https://github.com/denisenkom/go-mssqldb/pull/797