我试图通过匹配他们的时间戳字段加上或减去一秒来加入两个表。
SELECT t1.id, t2.id
FROM ctc.table1 t1
INNER JOIN ctc.table2 t2
ON EXTRACT(EPOCH FROM t1.timestamp)
BETWEEN EXTRACT(EPOCH FROM t2.timestamp) - 1
AND EXTRACT(EPOCH FROM t2.timestamp) + 1
是否有更高效的方式来编写此查询?
答案 0 :(得分:0)
正如戴尔
的评论中已经指出的那样ON t1."timestamp" BETWEEN
t2."timestamp" - INTERVAL '1' SECOND AND
t2."timestamp" + INTERVAL '1' SECOND
Craig Ringer 提出的变体可以使用公共索引,而您的只能使用专门建立的索引,如
create index the_index_name on ctc.table1(extract(epoch from timestamp))