简体   繁体   English

从大型数据库读取时SQLite3内存错误(> 1GB)

[英]SQLite3 memory error when reading from a large database (> 1GB)

I have a small Python 2.7 script that uses LIKE statements to extract pieces of information from textual data stored in a SQLite database. 我有一个小的Python 2.7脚本,它使用LIKE语句从存储在SQLite数据库中的文本数据中提取信息。

sql = "SELECT user_id, loc,\
        FROM entity\
        WHERE loc LIKE '%\"place\":%'\
        AND loc LIKE '%\"geo\":%'\
    AND loc LIKE '%\"coordinates\":%'" 
cin.execute(sql)
entities = cin.fetchall()

cin is a cursor to a SQLite database (table entity with >10^6 rows) (~1.5GB) which was established using cin是使用的SQLite数据库(表entity > 10 ^ 6行)(~1.5GB)的游标

import sqlite3
try:
    dbin = sqlite3.connect(database=args['dbi'].name)
    dbin.row_factory = sqlite3.Row
    cin = dbin.cursor()
except sqlite3.Error, e:
    errorLogger.error('... %e' % e)
    sys.exit()

The script ran fine with database sizes of 10^2 MB, but now I get a 脚本运行正常,数据库大小为10 ^ 2 MB,但现在我得到了一个

Traceback (most recent call last):
  File "C:\Users\...\migrate.py", line 247, in <module>
    entities = cin.fetchall()
MemoryError

after some seconds. 几秒钟后。 I am running a W7 64bit machine with 8GB RAM . 我正在运行带有8GB RAM的W7 64位机器 When the script is running, by looking at the resources monitor of W7, I can tell that successively all free memory is used and python.exe consumes as much as 1.9GB just before the program crashes. 当脚本运行时,通过查看W7的资源监视器,我可以告诉连续使用所有可用内存,python.exe在程序崩溃之前消耗的内存量高达1.9GB。 Still, there is about 3GB standby memory available (but don't ask me what's the difference between standby and free memory). 尽管如此,仍有大约3GB的待机内存可用(但不要问我待机和可用内存之间有什么区别)。

What can I do about this besides pre-filtering my query, eg by only looking at, let's say, 10'000 rows per query? 除了预先过滤我的查询之外,我还可以做些什么,例如,只查看,比方说,每个查询10 000行?

Calling fetchall requires allocating memory for all result records. 调用fetchall需要为所有结果记录分配内存。

You should instead read the result records from cin one by one. 您应该逐个从cin读取结果记录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM