gpt4 book ai didi

SQLITE——一个包含 5,000,000 个 TINYINT 值的文件?

转载 作者:行者123 更新时间:2023-12-03 15:52:26 27 4
gpt4 key购买 nike

我想创建一个包含 5,000,000 个 tinyint 值(介于 0 - 256 之间)的 SQLITE 文件。根据我的计算,这个文件应该使用大约 5[MB]。

然而,我得到的文件大小接近 50[MB]。

这是我使用的代码:

import sqlite3
# Create and populate a database
db = sqlite3.connect('/home/mine/temp.db')
db.execute("CREATE TABLE IF NOT EXISTS test(id INT2)")
for i in range(0, 5000000):
db.execute("INSERT INTO test(id) VALUES({0})".format(0))
db.commit()
db.close()

有什么想法可以优化事物以减少空间占用吗?

最佳答案

数据库不仅仅包含 500 万个微型整数的空间。 SQLite 已发布 details of their file format .似乎说每行至少需要 9 个字节。

SQLite uses two distinct variants of the B-Tree structure:

  • The table B-Tree, which uses 64-bit integer values for keys. In a table B-Tree, an associated database record (section 2.3.2) is stored along with each entry. Table B-Tree structures are described in detail in section 2.3.4.
  • The index B-Tree, which uses database records as keys. Index B-Tree structures are described in detail in section 2.3.3.

关于SQLITE——一个包含 5,000,000 个 TINYINT 值的文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5453343/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com