如何提升sqlite中blob数据的查询性能

本文探讨了SQLite中存储大型BLOB数据时,内部存储(直接存于数据库)与外部存储(独立文件)的性能差异。测试结果显示,当BLOB数据超过100kB时,使用外部文件存储通常性能更优。而页大小设置为8192或16384字节时,对于大型BLOB的I/O操作表现最佳。不过,实际性能还取决于硬件、文件系统和操作系统。建议在目标硬件上验证这些结论。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

关于sqlite的blob数据存储,由于暂时没有时间深入到代码内部去进行详细分析。

看到sqlite官网上有类似的测试数据和对比。


结论是:正常情况下,如果blob的数据量超过100kB,那么最好使用分开单独的文件来存储blob数据性能会好一点。如果非要讲Blob数据存储在一个数据库文件中,那么增加Page Size可以在一般情况下提升性能。当然文章了说了,具体还是与硬件、文件系统、操作系统有关系。

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 to 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:

  • A database page size of 8192 or 16384 gives the best performance for large BLOB I/O.

  • For BLOBs smaller than 100KB, reads are faster when the BLOBs are stored directly in the database file. For BLOBs larger than 100KB, reads from a separate file are faster.

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.






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值