如何获取特定日期的最新文件? [英] How to get latest document from specific date?

查看:64
本文介绍了如何获取特定日期的最新文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从一个大型数据库(每月1.5亿个文档)中的 Couchbase N1QL 获取指定日期(变量)的最新文档.日期中的最新文档可以是任何时间(之前的第二个月或上个月).注意,日期在Unix Epoch中.这是文档的结构:

I'm trying to get the latest document from specified date (variable) in Couchbase and N1QL on a very large database (150 millions documents each month). The latest document from a date could be any time (previous second or last month). Note, the date is in Unix Epoch. Here is a structure of the documents:

{
  "type" : "person",
  "id":"001",
  "name":"John Doe",
  "date":"1491199810.435"
}

我有日期索引:

CREATE INDEX `date` ON `mytable`(`date`) WITH { "defer_build"=true }

通常在我处理过的Postgres中,我通常会这样做:

Usually in Postgres that I dealt with, I usually would do:

SELECT * WHERE date <="1491199813.000" ORDER BY date DESC LIMIT 1;

在Postgres中具有日期索引的飞行时间以毫秒为单位.

Having index on date in Postgres just flies with response in milliseconds.

但是, Couchbase 的ORDER BY ... LIMIT非常慢.在相同的硬件和相同的数据上花费几分钟.

However, Couchbase ORDER BY ... LIMIT is very slow. It takes minutes on the same hardware and same data.

当然,有一种有效的方法可以使用Couchbase N1QL从指定日期获取单个最新文档,但是我找不到它.

Surely there an efficient way of getting single latest document from specified date with Couchbase N1QL, but I can't find it yet.

您有什么主意吗?

推荐答案

创建负函数索引并按如下所示在5.0.0之前版本中更改查询

Create negative functional index and change the query like below In pre 5.0.0

CREATE INDEX `forex_dt` ON `forex`(-TONUMBER(`date`));
SELECT * FROM `forex` WHERE -TONUMBER(`date`) >= -1491199813.000 ORDER BY -TONUMBER(`date`) LIMIT 1;

在5.0.0版中

CREATE INDEX `forex_dt` ON `forex`(`date` DESC);
SELECT * FROM `forex` WHERE `date` <= "1491199813.000" ORDER BY `date` DESC LIMIT 1;

这篇关于如何获取特定日期的最新文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆