sqlite3中的索引和RAM

sqlite3中的索引和RAM,sqlite,Sqlite,我需要写147m行。如果我在没有索引的情况下配置数据库,所有这些都可以正常工作: conn = sqlite3.connect('db.db') cur = conn.cursor() cur.execute('''CREATE TABLE IF NOT EXISTS NodesRefStop (stop_id text, stop_lat text, stop_lon text, stop_id_ref text, stop_lat_ref text, stop

我需要写147m行。如果我在没有索引的情况下配置数据库,所有这些都可以正常工作:

conn = sqlite3.connect('db.db')
cur = conn.cursor()
cur.execute('''CREATE TABLE IF NOT EXISTS NodesRefStop (stop_id text, stop_lat text, stop_lon text,
                stop_id_ref text, stop_lat_ref text, stop_lon_ref text, stop_type_ref text, distance REAL )''')
cur.execute('PRAGMA synchronous = 0')
conn.commit()
但如果我添加索引,sqlite会吃掉我所有的RAM:

conn = sqlite3.connect('db.db')
cur = conn.cursor()
cur.execute('''CREATE TABLE IF NOT EXISTS NodesRefStop (stop_id text, stop_lat text, stop_lon text,
                stop_id_ref text, stop_lat_ref text, stop_lon_ref text, stop_type_ref text, distance REAL )''')
cur.execute("CREATE INDEX dist_index ON NodesRefStop (distance);")
cur.execute("CREATE INDEX stop_id_index ON NodesRefStop (stop_id);")
cur.execute('PRAGMA synchronous = 0')
conn.commit()

我该怎么办?

我从来没有遇到过这个问题,但我很好奇,我搜索了一下,显然这是一个已知的问题

下面是一篇关于sqlite优化的有趣文章:

“尝试仅对静态数据或很少更改的数据使用索引。在实时数据或临时数据上构建索引可能代价高昂。”。。。这在每个数据库中都是正确的,不仅仅是使用sqlite


如果您在数据不经常更改的开始和之后插入所有这些行,那么您可以尝试仅在末尾添加索引。是否在单个事务中插入所有行?也许你可以分块做。

请在答案正文中包含链接中的相关信息。到第三方网站的链接可能会随着页面移动或网站关闭而中断。@ColonelThirtyTwo谢谢,你说得对。我刚刚给了用户一些相关信息的链接和一些一般性的建议,希望能对用户有所帮助,从这些文章中提取一条信息太难了。他接受了答案,所以我想这对他来说已经足够了。