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?
-
Brian Munier commented
The conversion slows down the compare.