I have quite a few million records into a table which supports WCF application. The application basically check for the existence of the recored and if not found it inserts that specific record.
one of the key field on which it checks for the existence is VARBINARY(256).
I am currently using HASHBYTES()
to bring down the byte counts to 32 bytes by using SHA2_256
algorithm as below.
CREATE TABLE BlobTable ( BlobID INT, Blob VARBINARY(256), BlobHash VARBINARY(32))
DECLARE @Bin VARBINARY(256) = CRYPT_GEN_RANDOM(256)
DECLARE @BinHash VARBINARY(32) = HASHBYTES('SHA2_256', @Bin)
DECLARE @Bin VARBINARY(256)
SELECT @_Bin = Blob FROM dbo.BlobTable WITH (ROWLOCK, READPAST) WHERE BlobHash = @BinHash
IF (@_Bin IS NULL)
BEGIN
INSERT INTO dbo.BlobTable (Blob, BlobHash) VALUES (@Bin, @BinHash)
END
Is there a way to bring down query cost in the select statement above? Is there a way to get the unique value for VARBINARY(256)
field which is shorter like VARBINARY(16)
or less, but still avoiding the duplicates?
Thanks