Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ability to control byte size of INTEGER type

Tags:

sqlite

I am trying to keep an SQLite table as small as possible. My tables will only contain 1 byte unsigned integers. However, it is unclear when I create a new table what the underlying structure of the table is that gets created. For example:

 CREATE TABLE test (SmallNumbers INTEGER)

Will the resulting SmallNumbers field be 1, 2, 4...8 bytes in size?

If I were to create 1 million records all containing the number "1" using the above command to create the table, would the resulting .db file be any smaller than if I inserted 1 million records all containing the value of 412,321,294,967,295?

How do I ensure that such a table can be as small as possible as I insert 1 byte unsigned integers into the table (with regards to disk space)?

like image 396
instrumentally Avatar asked Apr 27 '16 14:04

instrumentally


People also ask

What is the size of integer?

The INTEGER data type stores whole numbers that range from -2,147,483,647 to 2,147,483,647 for 9 or 10 digits of precision. The number 2,147,483,648 is a reserved value and cannot be used.

What is a 2 byte integer?

An automation integer data type that can be either positive or negative. The most significant bit is the sign bit, which is 1 for negative values and 0 for positive values. The storage size of the integer is 2 bytes. A 2-byte signed integer can have a range from -32,768 to 32,767.


1 Answers

Per SQLite documentation: https://www.sqlite.org/datatype3.html

Each value stored in an SQLite database (or manipulated by the database engine) has one of the following storage classes:

INTEGER. The value is a signed integer, stored in 1, 2, 3, 4, 6, or 8 bytes depending on the magnitude of the value.

You don't need to do anything to ensure the table will be as small as possible. SQLite will choose the smallest storage class that can store the value you supply, on a value-by-value basis.

like image 169
Mike W. Avatar answered Oct 03 '22 19:10

Mike W.