Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server: String vs Binary?

Tags:

sql-server

I have a load of records to store in a SQL Server 2008 database. Each of the records has a SHA-1 hash. Obviously storing the SHA in String form will take up 80 bytes compared to 20 if stored as Bytes.

When quering the database, which is SQL better at:

  1. Comparing Strings?
  2. Comparing Binary?

I need help deciding how to store the hashes as it has a huge storage impact on the database. Thanks for any help.

like image 574
Bram Avatar asked Jul 05 '10 00:07

Bram


2 Answers

Use BINARY(20). There are no performance issues (it should be faster, if anything). There are minor inconveniences with such values, such as the need to use byte[] instead of string in C#.

like image 52
Marcelo Cantos Avatar answered Sep 25 '22 06:09

Marcelo Cantos


HashBytes will return varbinary(8000), so I would definitely stick to binary - http://msdn.microsoft.com/en-us/library/ms174415.aspx - you'll be able to use it more easily in SQL Server

like image 42
Cade Roux Avatar answered Sep 24 '22 06:09

Cade Roux