Postgresql 9.2索引字段上的时间戳排序不会在一天内对小时部分进行排序

时间:2014-07-10 23:25:06

标签: postgresql sorting timestamp postgresql-9.2 partition

我无法相信使用ORDER BY子句进行简单选择时所看到的内容。 这是我的查询和错误结果:

SELECT date_valid, id_variable FROM myTable
   WHERE id_stn='78224' AND date_valid BETWEEN '2014-07-03 09:00:00'
         AND '2014-07-03 21:00:00' AND id_variable IN (11012,12004)
   ORDER BY date_valid ;

     date_valid      | id_variable 
---------------------+-------------
 2014-07-03 09:00:00 |       11012
 2014-07-03 15:00:00 |       11012
 2014-07-03 21:00:00 |       11012
 2014-07-03 09:00:00 |       12004
 2014-07-03 15:00:00 |       12004
 2014-07-03 21:00:00 |       12004

如您所见,排序似乎是在 id_variable 而不是 date_valid 上完成的。为了获得预期的结果,我必须创建一个Postgresql无法优化的新字段或者给出超过1天的时间戳范围:

SELECT date_valid,id_variable FROM myTable
       WHERE id_stn='78224' AND date_valid BETWEEN '2014-07-03 09:00:00'
             AND '2014-07-03 21:00:00' AND id_variable IN (11012,12004)
       ORDER BY date_valid + '0 hours'::INTERVAL;

     date_valid      | id_variable
---------------------+-------------
 2014-07-03 09:00:00 |       11012
 2014-07-03 09:00:00 |       12004
 2014-07-03 15:00:00 |       11012
 2014-07-03 15:00:00 |       12004
 2014-07-03 21:00:00 |       11012
 2014-07-03 21:00:00 |       12004

这是一个部分表定义,它在每个月的date_valid上进行了分区:

    Column     |            Type
---------------+-----------------------------
 id_obs        | bigint                      
 date_valid    | timestamp without time zone
 id_variable   | integer
 id_stn        | character varying(50)
Indexes:
    "myTable_pkey" PRIMARY KEY, btree (id_obs)
    "myTable_ukey" UNIQUE CONSTRAINT, btree (date_valid, id_variable, lat, lon)
Check constraints:
    "myTable_date_valid_check" CHECK (date_valid >= '2014-07-01 00:00:00'::timestamp without time zone AND date_valid < '2014-08-01 00:00:00'::timestamp without time zone)
Triggers:
    myTable_before_update BEFORE UPDATE ON myTable_201407 FOR EACH ROW EXECUTE PROCEDURE obs_update()
Inherits: myTable_parent
Has OIDs: no

如果结果是在同一天,Postgresql似乎没有按小时排序的错误。它必须是一个优化器问题,因为如果我在另一个未编制索引的时间戳字段上排序,我会出现此问题。如果我在每个日期字符串之后指定:: TIMESTAMP,或者如果我将select包含在另一个on中,则结果是相同的(未排序):SELECT * FROM(SELECT ...)x ORDER BY DATE_VALID。我对其他结构相似的表有同样的问题。

这是Postgresql 9.2.8的EXPLAIN结果:

 Result  (cost=0.02..62864.86 rows=10 width=87)
   ->  Merge Append  (cost=0.02..62864.86 rows=10 width=87)
         Sort Key: myTable.date_valid
         ->  Sort  (cost=0.01..0.02 rows=1 width=220)
               Sort Key: myTable.date_valid
               ->  Seq Scan on myTable  (cost=0.00..0.00 rows=1 width=220)
                     Filter: ((date_valid >= '2014-07-03 09:00:00'::timestamp without time zone) AND (date_valid <= '2014-07-03 21:00:00'::timestamp without time zone) AND (id_variable = ANY ('{11012,12004}'::integer[])) AND ((id
_stn)::text = '78224'::text))
         ->  Index Scan using myTable_201407_ukey on myTable_201407 myTable  (cost=0.00..62864.71 rows=9 width=72)
               Index Cond: ((date_valid >= '2014-07-03 09:00:00'::timestamp without time zone) AND (date_valid <= '2014-07-03 21:00:00'::timestamp without time zone) AND (id_variable = ANY ('{11012,12004}'::integer[])))
               Filter: ((id_stn)::text = '78224'::text)

1 个答案:

答案 0 :(得分:1)

可能在9.2.1

中修复了此错误
  

修复涉及WHERE indexed_column IN(list_of_values)的查询的输出错误排序

http://www.postgresql.org/docs/9.2/static/release-9-2-1.html

9.2已经是9.2.8