- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我是第一次使用 ZODB python 数据库 模块。教程 ( http://www.zodb.org/en/latest/tutorial.html ) 让我对 ZODB 数据库行为的某个方面感到困惑:如何避免不小心创建一个只有一个非常大的条目的数据库?我将逐步解释我的应用程序、我当前的数据库方法以及困惑的根源。
我要保存的数据库完全由 Item 对象组成,定义如下(稍微简化):
class Item(Persistent):
def __init__(self, name, *args, **kwargs):
super().__init__(*args, **kwargs)
# 1. Persistent variables
# ------------------------
self.__name = name
self.__myList = PersistentList() # <- list can hold other Item-objects
self.__myVar01 = None
self.__myVar02 = None
self.__myVar03 = None
# 2. Non-persistent variables
# ----------------------------
self._v_myVar01 = None
self._v_myVar02 = None
self._v_myVar03 = None
直观表示如下:
应用程序在启动时构造一个这样的 Item 对象。在应用程序期间,此项目对象将创建“子项”(它们本身也是项目对象)。这个过程持续了一段时间,使得以下对象结构在内存中:
这个构造很容易包含 20.000 个Item-对象。所以我想将它们保存在数据库中。
为了将这种 Item-objects 结构保存在数据库中,我遵循了教程中的以下指南:
Storing objects
To store an object in the ZODB we simply attach it to any other object that already lives in the database. Hence, the root object functions as a boot-strapping point. The root object is meant to serve as a namespace for top-level objects in your database.
[ Quoted from ZODB Tutorial http://www.zodb.org/en/latest/tutorial.html ]
以下函数创建一个新数据库(从顶级项目开始)并将其保存到硬盘:
from ZODB.FileStorage import FileStorage
from ZODB import DB
from persistent import Persistent
import transaction
# Call this function to save the database
# to the harddrive and close it.
# ----------------------------------------
def save_database_and_close():
transaction.commit()
conn.close()
db.close()
# Call this function to create a new
# database, starting from a root-item
# ------------------------------------
def create_database(root_item):
storage = FileStorage("C:/mytest/mydb.db")
db = DB(storage)
conn = db.open()
root = conn.root()
root.myRootItem = root_item
transaction.commit()
但是 - 在继续阅读本教程时 - 我觉得我目前的方法不是很好:
(Please note that at this point, the tutorial has covered an example of making Account-objects to be stored in a ZODB database)
We could store Account-objects directly on the root object:
import account
# Probably a bad idea:
root.account1 = account.Account()But if you’re going to store many objects, you’ll want to use a collection object 3:
import account, BTrees.OOBTree
root.accounts = BTrees.OOBTree.BTree()
root.accounts['account-1'] = Account()Footnote 3:
The root object is a fairy simple persistent object that’s stored in a single database record. If you stored many objects in it, its database record would become very large, causing updates to be inefficient and causing memory to be used ineffeciently.
脚注暗示我只是在创建一个包含一个大条目的数据库——这当然是您能想象到的最低效的数据库。
好吧,我很确定下面的方法是非常糟糕的(并且被上面的警告所谴责):
但是这个警告(不要将所有内容都存储在根目录中)是否也适用于我的情况?像这样:
换句话说,我的方法会创建一个只有一个大条目的数据库吗(非常低效)?或者它会创建一个很好的数据库,每个 Item-object 有一个条目吗?
注意:
我不确定它是否相关,但我将在此处列出我的系统规范:
最佳答案
不,这不是低效的。 ZODB 为每个 Persistent
实例创建单独的条目。这些条目稍后会在您访问它们时按需加载。
来自您链接到的同一教程:
Subclassing
Persistent
provides a number of features:[...]
- Data will be saved in its own database record.
这对您的应用程序来说是完全透明的。您的第一笔交易会很大,但后续交易只会在您更改单个项目时写出对它们的更改。
关于python - ZODB python : how to avoid creating a database with only one big entry?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50445870/
我使用 ZODB 创建了一个数据库,然后将其复制粘贴到另一台 PC。我想知道为什么每次我登录这个数据库(复制的)我都会得到这个错误: no handlers could be found for lo
我将 ZODB 用于我的 python 对象持久性。我使用 web2py 作为框架。 我使用的 ZODB 数据库仅用于读取。我已经编写了在我的模型中打开数据库的代码。 connection.root(
我目前正在从事一个童话般的大型项目(活跃成员大约有数百 K)并且非常倾向于 Plone 解决方案。 我问了一些相关的问题,比如 here和 here . 从非常有经验的 Plonistas(以及活跃的
多线程 Web 应用程序中 ZODB.DB 调用中 pool_size 的合理默认值是多少? 保留实际的默认值7会给我一些连接警告,即使我是唯一一个在数据库交互处理程序中导航的人。是否可以设置太高的数
我正在开发一个应用程序,用于填写一些巨大的表格。一个表单可以属于多个项目。此外,该表单有两个可以多次填写的部分,例如目标和事件,因此一个表单可以定义多个目标和事件。 我有一个类来代表项目,另一个类代表
我是第一次使用 ZODB。只是尝试使用 FileStorage 提交数据。但是当我第二次执行相同的脚本时,我无法提交新对象。这是我的脚本 from ZODB import FileStorage,DB
我有一个站点,每当我在服务器运行时尝试打开数据库时,它都会显示一条错误消息,指出数据库已被不同的进程打开。 问题是我有一些脚本可以使用 cron 安排来检查数据库,甚至可以在服务器运行时使用 pshe
我有一个 ZODB 安装,我必须在其中组织数百万个不同类型的对象。我有一个通用容器类 Table,其中包含 BTrees 以按属性或这些属性的组合对对象进行索引。数据一致性非常重要,因此我想强制执行,
我确实在我的 ZODB 上运行并行写入请求。我的 ZODB 中确实有多个 BTree 实例。一旦服务器访问了这样的 BTree 中的相同对象,我就得到了 IOBucket 类的 ConflictErr
今天我在使用 ZODB 的 Python 应用程序上发现了一个错误。试图找出我的应用程序卡住的原因,我认为 ZODB 是原因。 将日志记录设置为调试,似乎在提交时,ZODB 会发现 2 个连接,然后开
我想使用 ZODB 并尽可能少地缓存。为此,我创建 ZODB 数据库实例并像这样打开它: db = DB('/home/me/example.db', cache_size=1, cache_size
我有一个服务器和一个客户端。 客户端发送请求。该请求有一个与之关联的特定键,例如a-1, a-2, b-1, b-4. 如果同时传入两个对同一个键的请求,则会出现冲突错误,因为正在修改相同的数据结构。
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
目前 ZMI 管理“打包数据库”的功能有点粗糙。 1) 是否有可能为 Web UI 提供某种进度指示器?例如。一个告诉你还剩多少分钟/小时,至少给出某种估计 2) ZODB 打包如何影响站点的响应性?
有没有办法只单独记录冲突错误? 是否有一个插件可以帮助解决这个问题? 我想将此作为微调 ZEO 集群的一部分。 最佳答案 为此创建了一个简单的 Plone 产品。 https://github.com
如何设置在提交后 ZODB 对象更改时触发的 Hook ?我可以为 IObjectWillBeMovedEvent 和 IObjectWillBeAddedEvent 设置处理程序,但这些处理程序会在
为了持久化,我使用 ZODB 来存储内存中存在的一些数据。如果内存中数据的服务每次崩溃,重新启动将从 ZODB 加载数据,而不是查询 MySQL 数据库中的数百或数千行。 似乎每次我将 500K 数据
我有一个上传表单,它接受 zip 文件,并有一个方法可以解压缩它并从中获取每个文件。从它的 md5 哈希中创建一个唯一的 id 并将它们存储在字典中; dict[uid] = imagebinary
按照给出的建议here ,我使用 ZODB 存储了我的数据,该数据由以下代码创建: # structure of the data [around 3.5 GB on disk] bTree_cont
我可以在文档中看到,如果我要更改类中的某些字段,那么我需要编写一个迁移脚本。但是我到底需要在那个脚本中写什么?还有像 dgango 中那样的自动迁移工具吗? 最佳答案 在您的脚本中,您将修改存储在 Z
我是一名优秀的程序员,十分优秀!