创建数据存储区的成本。密钥:将密钥存储在结构中,而不存储ID和从数据存储区中获取数据

创建数据存储区的成本。密钥:将密钥存储在结构中,而不存储ID和从数据存储区中获取数据

问题描述:

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

请考虑以下两种选择。 p>

A)将密钥存储在 p>

  type Point struct {
 Place * datastore.Key 
 Lat float64 
 Lon float64 
} 
  code>  pre> 
  
 

然后使用密钥获取: p>

  place:= new(Place)
if err:= datastore.Get(c,k,point.Place);  err!= nil {
 return err 
} 
  code>  pre> 
 
 

B)存储ID p>

 类型Point  struct {
放置int64 
 Lat float64 
 Lon float64 
} 
  code>  pre> 
 
 

然后在创建密钥后获取。 p> k:= datastore.NewKey(c,“ Place”,“”,point.Place,nil) place:= new(Place) if err:= datastore.Get(c,k,place) ; err!= nil { return err } code> pre>

存储密钥而不是ID会占用更多空间。 为了进行权衡,最好了解一下创建密钥需要多少资源。 换句话说,创建密钥真的便宜吗,还是一次创建并存储它更好? p>

使用单个密钥,可能没什么大不了,但是 假设我要获取一个点列表,然后要为每个点检索位置(即遍历这些点以构建键数组并获取它们)。 p>

编辑 :我不是在考虑在这里分配ID或密钥,而只是在使用它们(即,所有点和位置都已经在数据存储区中,问题是是否存储ID或整个密钥)。 p>

Ex animo, 亚历山大·恩林格 p> div>

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.

I think Peter gave a pretty good answer, but there's two things involved here:

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

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

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.