Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What data type I should preferably use for storing flag fields in sql server?

What data type I should preferably use for storing flag fields in sql server, bit(if I have only 0,1 and null), tinyint, smallint or int for flags which cannot be accommodated into bit? Does it effect my query performance?

like image 947
Niraj Avatar asked Nov 13 '13 12:11

Niraj


People also ask

What data type is a flag?

A flag is a logical concept, not a special type of variable. The concept is that a variable records the occurrence of an event. That it is set "one way" if the event happened, and set "the other way" if the event did not happen.

What can the storage type of a flag field be?

The storage types for flags can be string, integer, real number, or date/time.

What data type in SQL is the best choice to store a date?

The DATETIME type is used when you need values that contain both date and time information. MySQL retrieves and displays DATETIME values in 'YYYY-MM-DD HH:MM:SS' format. The supported range is '1000-01-01 00:00:00' to '9999-12-31 23:59:59'.


3 Answers

Use Bit Datatype which is set to 1 if true or 0 ir False, much smaller and faster to query against Bit values Your column could be saved as 1 Byte if you have 8 or less Bit columns in your table and I think if you have more then 8 then SQL Server uses 2 Bytes Column , Which is A lot smaller than any other options you might have, tinyint, smallint and int all requires more space then a bit column and querying a bigger datatype means more time to query the data.
My Suggestion
My Suggestion would be to use Bit Column as it requires less space to store it and queries can perform much faster.

like image 71
M.Ali Avatar answered Nov 15 '22 10:11

M.Ali


bit

its a boolean, so its only 1 or 0, and doesn't require a whole byte to store. Plus you can just make it nullable on the table design.

like image 30
Rich Avatar answered Nov 15 '22 09:11

Rich


I got the answer for what I was looking, we should consider using smallest data type sufficient enough to store flag values required. This is with respect to the performance of database as storage size is not a big concern these days.

have a look at this link for more insights.

As per this link think about how resources other than disk space are used, think about buffer pool and storage bandwidth. At the extreme end, CPU cache and memory bus bandwidth, and also about how SQL Server works.

like image 29
Niraj Avatar answered Nov 15 '22 09:11

Niraj