我有一些物联网设备将一些数据发送到Google Cloud Datastore。
数据存储区在数据存储区模式下设置为Cloud Firestore。
每行都有以下字段:
这些都属于ParticleEvent
类型。
我希望运行以下查询; select current_temperature, target_temperature from ParticleEvent where device_id = ‘abc123’ order by published_at desc
。
我尝试运行该查询时收到以下错误:
GQL查询错误:您的数据存储区没有此查询所需的复合索引(开发人员提供)。
所以我设置了一个index.yaml
文件,其中包含以下内容:
indexes:
- kind: ParticleEvent
properties:
- name: data
- name: device_id
- name: published_at
direction: desc
- kind: ParticleEvent
properties:
- name: current_temperature
- name: target_temperature
- name: device_id
- name: published_at
direction: desc
我使用gcloud
工具将此成功发送到数据存储区,我可以在索引选项卡中看到两个索引。
但是,当我尝试运行查询时,仍然会出现上述错误。
我需要添加/更改索引才能使此查询生效?
虽然在评论中我只是建议select *
(这是最好的方式,我认为)
有一种方法可以使您的查询工作。
- kind: ParticleEvent
properties:
- name: device_id
- name: published_at
direction: desc
- name: current_temperature
- name: target_temperature
原因是select
最后完成,因此你需要current_temperature
和target_temperature
的指数在较低的水平。
为什么我不建议这样做是因为,当您的数据增长时,您需要更多的索引组合,因为select
特定列。您的索引大小将呈指数级增长。
但是,如果您确定只使用一次并始终查询这样的数据,那么请随意索引它。
或者,如果您的计算机和Google云之间的连接带宽非常小,那么下载更多数据会导致延迟。