gpt4 book ai didi

python - ZODB python : how to avoid creating a database with only one big entry?

转载 作者:搜寻专家 更新时间:2023-10-30 19:47:22 26 4
gpt4 key购买 nike

我是第一次使用 ZODB python 数据库 模块。教程 ( http://www.zodb.org/en/latest/tutorial.html ) 让我对 ZODB 数据库行为的某个方面感到困惑:如何避免不小心创建一个只有一个非常大的条目的数据库?我将逐步解释我的应用程序、我当前的数据库方法以及困惑的根源。


1。 Item-对象

我要保存的数据库完全由 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

直观表示如下:

enter image description here


应用程序在启动时构造一个这样的 Item 对象。在应用程序期间,此项目对象将创建“子项”(它们本身也是项目对象)。这个过程持续了一段时间,使得以下对象结构在内存中:

enter image description here


这个构造很容易包含 20.000 个Item-对象。所以我想将它们保存在数据库中。


2。我如何将此结构保存到 ZODB 数据库

为了将这种 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()


3。将所有内容存储在根上的问题

但是 - 在继续阅读本教程时 - 我觉得我目前的方法不是很好:

(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.

脚注暗示我只是在创建一个包含一个大条目的数据库——这当然是您能想象到的最低效的数据库。


4。我困惑的是什么

好吧,我很确定下面的方法是非常糟糕的(并且被上面的警告所谴责):

enter image description here


但是这个警告(不要将所有内容都存储在根目录中)是否也适用于我的情况?像这样:

enter image description here

换句话说,我的方法会创建一个只有一个大条目的数据库吗(非常低效)?或者它会创建一个很好的数据库,每个 Item-object 有一个条目吗?


注意:
我不确定它是否相关,但我将在此处列出我的系统规范:

  • Windows 10,64 位
  • python 3.6.3
  • ZODB 5.4.0(截至目前的最新版本 - 2018 年 5 月 21 日)

最佳答案

不,这不是低效的。 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/

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