principlesqlCritical
When to use TINYINT over INT?
Viewed 0 times
tinyintwhenuseintover
Problem
In general, I always use Ints. I know that in theory this is not the best practice, though, since you should use the smallest data type that will be guaranteed to store the data.
For example, it's better to use
However, the only reason I know for doing this is for storage purposes--using 1 byte on a row instead of 4 bytes.
What are the impacts of using
For example, it's better to use
tinyint when you know that the only data you will store is a 1, 0 or null (with a very small chance of expanding that to a 2 or 3 later).However, the only reason I know for doing this is for storage purposes--using 1 byte on a row instead of 4 bytes.
What are the impacts of using
tinyint (or smallint or even bigint) over just int, other than saving space on your hard drive?Solution
Disk space is cheap... that's not the point!
Stop thinking in terms of storage space, think instead about buffer pool and storage bandwidth. At the extreme end, CPU cache and memory bus bandwidth. The linked article is part of the series highlighting issues with poor clustered key selection (INT vs GUID vs Sequential GUID) but it highlights the difference bytes can make.
The overriding message is design matters. The difference won't show up in an individual database on an appropriately spec'd server until you hit VLDB territory but if you can save a few bytes, why not do so.
I'm reminded of the environment described in an earlier question. 400+ databases, ranging in size from 50mb-50GB, per SQL instance. Scrubbing a few bytes per record, per table, per database across that environment could make a significant difference.
Stop thinking in terms of storage space, think instead about buffer pool and storage bandwidth. At the extreme end, CPU cache and memory bus bandwidth. The linked article is part of the series highlighting issues with poor clustered key selection (INT vs GUID vs Sequential GUID) but it highlights the difference bytes can make.
The overriding message is design matters. The difference won't show up in an individual database on an appropriately spec'd server until you hit VLDB territory but if you can save a few bytes, why not do so.
I'm reminded of the environment described in an earlier question. 400+ databases, ranging in size from 50mb-50GB, per SQL instance. Scrubbing a few bytes per record, per table, per database across that environment could make a significant difference.
Context
StackExchange Database Administrators Q#4968, answer score: 101
Revisions (0)
No revisions yet.