Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple files for a single SQLite database

Tags:

sqlite

Afaik, SQLite stores a single database in a single file. Since this would decrease the performance when working with large databases, is it possible to explicitly tell SQLite not to store the whole DB in a single file and store different tables in different files instead?

like image 564
thameera Avatar asked Feb 23 '12 09:02

thameera


Video Answer


3 Answers

The question is about three years old but I landed here when I searched for the very same question. Late I found out, that it IS possible:

Use

sqlite3.exe MainDB.db

ATTACH DATABASE 'SomeTableFile.db' AS stf;

Access the table from the other database file

SELECT * FROM stf.SomeTable;

You can even join over several files

SELECT * FROM MainTable mt JOIN stf.SomeTable st ON (mt.id = st.mt_id); 

https://www.sqlite.org/lang_attach.html

tameera said there is a limit of 62 attached databases but I never hit that limit so I can't confirm that.

The big advantage besides some special cases is that you limit the fragmentation in the database files and you can use the VACUUM command separately on each table!

like image 175
Henning Avatar answered Oct 10 '22 15:10

Henning


If you don't need a join between these tables you can manually split the DB and say which tables are in which DB (=file).

I don't think that it's possible to let SQLite split your DB in multiple files, because you connect to a DB by telling the filename.

like image 20
yoooshi Avatar answered Oct 10 '22 13:10

yoooshi


SQLite database files can grow quite large without any performance penalties.

The things that might degrade performance are:

  • file-locking contention
  • table size (if using indexes and issuing write queries)

Also, by default, SQLite limits the number of attached databases to 10.

Anyway, try partition your tables. You'll see that SQLite can grow enormously this way.

like image 39
Alix Axel Avatar answered Oct 10 '22 13:10

Alix Axel