TEXT values in sqlite databases
Do sqlite databases use anything like a flyweight pattern for TEXT values. If I'm repeatedly using only a few different,开发者_如何学编程 short TEXT values in a column, would it be better to replace them with integers?
SQLite stores each copy of the text column individually:
$ sqlite3 pancakes.sqlt
sqlite> create table pancakes (s text);
sqlite> insert into pancakes (s) values ('where is pancakes house?');
sqlite> insert into pancakes (s) values ('where is pancakes house?');
sqlite> insert into pancakes (s) values ('where is pancakes house?');
sqlite> insert into pancakes (s) values ('where is pancakes house?');
sqlite> insert into pancakes (s) values ('where is pancakes house?');
sqlite> insert into pancakes (s) values ('where is pancakes house?');
sqlite> insert into pancakes (s) values ('where is pancakes house?');
sqlite> insert into pancakes (s) values ('where is pancakes house?');
$ strings pancakes.sqlt
SQLite format 3
Itablepancakespancakes
CREATE TABLE pancakes (s text)
=where is pancakes house?
=where is pancakes house?
=where is pancakes house?
=where is pancakes house?
=where is pancakes house?
=where is pancakes house?
=where is pancakes house?
=where is pancakes house?
So if you want to avoid duplicating your strings you should use integers, set up a separate table to map your integers to strings, and then add a foreign key to this new table so that you can ensure referential integrity.
精彩评论