Small. Fast. Reliable.
Choose any three.

Internal Versus External BLOBs in SQLite

If you have a database of large BLOBs, do you get better read performance when you store the complete BLOB content directly in the database or is it faster to store each BLOB in a separate file and store just the corresponding filename in the database?

To try answer this, we ran 49 test cases with various BLOB sizes and SQLite page sizes on a Linux workstation (Ubuntu circa 2011 with the Ext4 filesystem on a fast SATA disk). For each test case, a database was created that contains 100MB of BLOB content. The sizes of the BLOBs ranged from 10KB to 1MB. The number of BLOBs varied in order to keep the total BLOB content at about 100MB. (Hence, 100 BLOBs for the 1MB size and 10000 BLOBs for the 10K size and so forth.) SQLite version 3.7.8 was used.

The matrix below shows the time needed to read BLOBs stored in separate files divided by the time needed to read BLOBs stored entirely in the database. Hence, for numbers larger than 1.0, it is faster to store the BLOBs directly in the database. For numbers smaller than 1.0, it is faster to store the BLOBs in separate files.

In every case, the pager cache size was adjusted to keep the amount of cache memory at about 2MB. For example, a 2000 page cache was used for 1024 byte pages and a 31 page cache was used for 65536 byte pages. The BLOB values were read in a random order.

Database Page SizeBLOB size
10k20k50k100k200k500k1m
10241.5351.0200.6080.4560.3300.2470.233
20482.0041.4370.8700.6360.4830.3720.340
40962.2611.8861.1730.8900.7010.5260.487
81922.2401.8661.3341.0350.8300.6250.720
163842.4391.7571.2921.0230.8290.8200.598
327681.8781.8431.2960.9810.9760.6750.613
655361.2561.2551.3390.9830.7690.6870.609

We deduce the following rules of thumb from the matrix above:

Of course, your mileage may vary depending on hardware, filesystem, and operating system. Double-check these figures on target hardware before committing to a particular design.