Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL: Is it efficient to use tinyint instead of Integer if my max value is 255?

Lets assume I want to save the count of datagrid rows which can be max. 24 because each row is 1 hour.

To save the row index in the database a tinyint field would be totally enough. But back in my mind I remember slightly that databases are optimized for integers?!

So is it worth to use tinyint?

like image 300
msfanboy Avatar asked Jul 07 '10 17:07

msfanboy


People also ask

When should I use Tinyint in MySQL?

If you include 2 or 3, you have to use tinyint (at the very smallest scale). "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)." I'd use an ENUM for such a thing.

What is the use of Tinyint in SQL?

The TINYINT data type is an integer value from 0 to 255. TINYINT is the smallest integer data type and only uses 1 byte of storage. An example usage of TINYINT is a person's age since no person reaches the age of 255.


1 Answers

With a narrower table, the database will fit more records in a single IO page, and will therefore require fewer hard disk reads.

The rule of thumb is to always use the data type that will require the least storage size.

like image 160
Daniel Vassallo Avatar answered Nov 01 '22 12:11

Daniel Vassallo