Fla*_*tor 1 python database zodb
我正在编写一个软件来检索网页,将有关它们的一些关键信息提取到一个对象中,然后将其写入 ZODB 数据库。我最终将大约 350,000 个这些对象写入我的数据库。
在我的代码运行一段时间后,每当我向数据库添加新对象时,它就会开始发布此消息...
UserWarning: The <class 'persistent.mapping.PersistentMapping'>
object you're saving is large. (26362014 bytes.)
Perhaps you're storing media which should be stored in blobs.
Perhaps you're using a non-scalable data structure, such as a
PersistentMapping or PersistentList.
Perhaps you're storing data in objects that aren't persistent at
all. In cases like that, the data is stored in the record of the
containing persistent object.
In any case, storing records this big is probably a bad idea.
Run Code Online (Sandbox Code Playgroud)
所以我的问题首先是错误消息所指的 26MB 是用于添加的单个对象还是整个数据库。这些对象中的每一个都应该很小,但消息会显示在添加的每个新对象上。
26MB 是为整个PersistentMapping对象生成的“pickle”的大小。正如消息所说,PersistentMapping不可扩展:如果您再向其添加一个键值对,并提交事务,它将再次写出 26MB(加上您添加的单个新对的大小)。每次更改PersistentMapping实例并提交时,整个对象都会存储到磁盘(包括您之前添加的所有对象)。通过一系列的添加和提交,这会产生与您添加的项目数量成二次方的数据库总大小,并且还会受到二次方时间行为的影响(您添加的每个新项目都比添加的最后一个项目花费的时间更长,因为每次提交都会写出所有以前添加的项目也是如此,而不仅仅是添加的最后一个项目)。
查看文档以了解BTreeZODB 支持的各种风格。这些是可扩展的、持久的键值映射,并且几乎可以肯定您应该用于此任务。
请注意,BTree为了提高效率,ZODB 实现了多种风格。最通用的是OOBTree,它允许键和值的通用对象。最具体的是IIBTree,它只允许 32 位整数作为键和值。这是一个教程:
http://pythonhosted.org/BTrees