Take this sqlite3 interactive session:
sqlite> create table d(t date);
sqlite> insert into d values('1913-12-23');
sqlite> select t,typeof(t) from d;
1913-12-23|text
sqlite>
Why is the date field showing up as type text? Would storage space be smaller and queries be faster if it were numeric?
Sqlite3 datatype documentation shows that the affinity for date columns is numeric according to the table in §2.2. I would guess that date fields stored as numeric values as opposed to text would take up less space, so that would be ideal. I could very well not have a good grasp of sqlite yet, so my concerns may be invalid.
For easy copy/paste:
create table d(t date);
insert into d values('1913-12-23');
select t,typeof(t) from d;
The reason that
typeof('1913-12-23')returnstextis that'1913-12-23'is a string, and is stored in the table as a string.Type affinity comes into play only when the conversion would be lossless; e.g., the value
'123456'would be converted into a number.SQLite does not have a native date data type.
To allow dates to be handled by the built-in date functions, you have to store them either as
yyyy-mm-ddstrings or as numbers (either Julian day numbers, or seconds since the Unix epoch).Integers take up less storage than strings, so queries are faster if the DB’s bottleneck is I/O, which is likely.