Don't Compare VARCHAR columns as NVARCHAR
Don't Compare VARCHAR columns as NVARCHAR
I have a Table
with 3 columns, 2 ints and a VARCHAR(10)
CompanyID INT NOT NULL
CustomerID INT NOT NULL
YearMonth VARCHAR(10) NOT NULL -- Stores '2024-12' yes I know it should be CHAR(7)
I populate the table, and take a Snapshot of the database.
I then populate the table with new code
I use SQL Data Compare to validate the data and see if I get different results. I am expecting the same, but could also expect differences.
Table has apx 5 billion rows.
Problem, I see The Active expensive queries generated are
SELECT
CompanyID,
CustomerID,
CAST(YearMonth AS NVARCHAR(10))
So the query to compare is more expensive than just a straight clustered index scan.
This query is used on both Database and the Database Snapshot.
Why do you need to convert if the data types are the same?
![](https://secure.gravatar.com/avatar/139c5bd0b84ef20b2988e1235333eccb?size=40&default=https%3A%2F%2Fassets.uvcdn.com%2Fpkg%2Fadmin%2Ficons%2Fuser_70-6bcf9e08938533adb9bac95c3e487cb2a6d4a32f890ca6fdc82e3072e0ea0368.png)
-
Brian Munier commented
The conversion slows down the compare.