英文:
Cost of creating a datastore.Key: Storing a key in struct versus an id and fetching from the datastore
问题
考虑以下两种选择。
A)将键存储在结构体中。
type Point struct {
Place *datastore.Key
Lat float64
Lon float64
}
然后使用键进行获取:
place := new(Place)
if err := datastore.Get(c, k, point.Place); err != nil {
return err
}
B)存储ID
type Point struct {
Place int64
Lat float64
Lon float64
}
然后在创建键之后进行获取。
k := datastore.NewKey(c, "Place", "", point.Place, nil)
place := new(Place)
if err := datastore.Get(c, k, place); err != nil {
return err
}
存储键而不是ID需要更多的空间。为了了解权衡情况,了解创建键需要多少资源将会很有帮助。换句话说,创建键是否真的很便宜,还是最好创建一次并存储起来?
对于单个键,可能并不重要,但假设我获取了一系列的点,对于每个点,我想要检索地点(即循环遍历点以构建键的数组,并获取它们)。
编辑:我在这里不考虑分配ID或键,只是使用它们(即所有的点和地点已经在数据存储中,问题只是存储ID还是整个键)。
诚挚的,
Alexander Yngling
英文:
Consider the following two alternatives.
A) Storing the key in the struct.
type Point struct {
Place *datastore.Key
Lat float64
Lon float64
}
Then fetching using the key:
place := new(Place)
if err := datastore.Get(c, k, point.Place); err != nil {
return err
}
B) Storing the id
type Point struct {
Place int64
Lat float64
Lon float64
}
Then fetching after creating the key.
k := datastore.NewKey(c, "Place", "", point.Place, nil)
place := new(Place)
if err := datastore.Get(c, k, place); err != nil {
return err
}
Storing the key instead of the id takes a bit more space. In order to see the tradeoff, it would be great to get a feeling for how much resources it takes to create a key. In other words, is it really cheap to create a key, or is it better to create it once and store it?
With a single key, it probably doesn't matter much, but let's say that I fetch a list of points, and for each point I want to retrieve the place (i.e. loop through the points to build an array of keys, and fetch them).
Edit: I am not thinking about allocating IDs or keys here, only using them (i.e. all points and places are already in the datastore, the question is just whether to store the id or the entire key).
Ex animo,
Alexander Yngling
答案1
得分: 2
键基本上只是一组属性的包装:种类、ID/名称、父键、命名空间。
因此,从种类和ID创建一个键是没有成本的,因为这是一个本地操作(不需要Datastore)。
另一方面,键分配是有成本的,因为这会创建一个具有唯一ID的键,并且需要在底层查询Datastore。
英文:
Key is basically just a wrapper around a set of properties: kind, id/name, parent key(s), namespace.
So creating a Key from a kind and ID costs nothing, as this is a local operation (does not require Datastore).
OTOH, Key allocation does cost as this creates a Key with unique ID and this needs to Query a Datastore under the hood.
答案2
得分: 2
我认为Peter给出了一个相当好的答案,但这里涉及到两个问题:
-
你似乎在问是否从ID生成密钥并存储ID以节省存储成本,还是存储实际密钥更便宜。从ID生成密钥是一个相当简单的哈希过程,几乎可以忽略不计 - 你可能有更重要的事情要担心。我无法说它的成本有多高,但你可以进行一个相当简单的实验,循环几十万次并估算成本。它可能比存储更便宜,但我怀疑它不会非常显著。
-
难点在于生成唯一的ID,你在问题中没有包含这部分。对于这个问题,可能更容易获取一个保证唯一的数据存储键。
英文:
I think Peter gave a pretty good answer, but there's two things involved here:
-
You seem to be asking whether it's cheaper to generate a key from an ID and store the ID to save storage cost, or store the actual key. Generating a key from an ID is a pretty simple hash, close to neglible - you probably have many more important things to worry about. I can't say how much it costs, but you can do a pretty simple experiement and loop a few hundred thousand times and estimate the cost. It probably is cheaper than storage, but I doubt it'd be very significant.
-
The difficult part is generating the unique ID, which you didn't include in your question. For that, it's probably easier to just get a datastore key which is guaranteed to be unique.
答案3
得分: 0
如前所述,创建一个键几乎不需要任何费用;因此,您可以选择存储ID,从而节省一些存储成本。存储键的好处是它与实际指向的ID类型匹配,并且可以防止代码中可能出现的错误。例如,如果您存储ID,那么在代码中可能会检索到该ID,然后为错误的实体创建一个键,如果您存储键,您就不需要担心这个错误。而且,考虑到数据存储的成本,存储ID和键之间的差异可能是可以忽略的,您花费在思考这个问题上的时间更有价值。
英文:
As mentioned already creating a key costs nearly nothing; therefore, you would choose to store the ID which would save you some storage cost. The benefits to storing the Key is that it is typeded to what the id is actually pointing at and would prevent possible errors in your code. For example if you store the ID it is possible for you to retrieve that ID and then create a key fro the wrong entity in your code, if you store the key you don't need to worry about that mistake. And at the cost of the datastore the difference in storing a ID and a Key is probably negligible, the time you spent thinking about it is more valuable.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论