dongzhilian0188 2012-08-23 08:12
浏览 27
已采纳

创建数据存储区的成本。密钥:将密钥存储在结构中,而不存储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

  • 写回答

3条回答 默认 最新

  • douruye5092 2012-08-23 08:34
    关注

    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条)

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算