如何获取date_part查询以命中索引?

时间:2019-03-29 18:22:41

标签: postgresql indexing aggregate postgresql-performance

我尚未能够使该查询命中索引而不是执行完整扫描-我还有另一个查询,它对几乎相同的表使用date_part('day',datelocal)(该表只有一点点)较少的数据,但结构相同),并且将命中我在datelocal列上创建的索引(这是没有时区的时间戳)。查询(此查询在表上执行并行seq扫描并执行内存快速排序):

SELECT
    date_part('hour', datelocal) AS hour,
    SUM(CASE WHEN gender LIKE 'male' THEN views ELSE 0 END) AS male,
    SUM(CASE WHEN gender LIKE 'female' THEN views ELSE 0 END) AS female
FROM reportimpression
WHERE datelocal >= '2-1-2019' AND datelocal < '2-28-2019'
GROUP BY date_part('hour', datelocal)
ORDER BY date_part('hour', datelocal)

这是另一个命中我的datelocal索引的人:

SELECT
    date_part('day', datelocal) AS day,
    SUM(CASE WHEN gender LIKE 'male' THEN views ELSE 0 END) AS male,
    SUM(CASE WHEN gender LIKE 'female' THEN views ELSE 0 END) AS female
FROM reportimpressionday
WHERE datelocal >= '2-1-2019' AND datelocal < '2-28-2019'
GROUP BY date_trunc('day', datelocal), date_part('day', datelocal)
ORDER BY date_trunc('day', datelocal)

为此而烦恼!关于如何加快第一个或至少使其达到索引的任何想法?我尝试在datelocal字段上创建索引,在datelocal,性别和视图上创建复合索引,并在date_part('hour',datelocal)上创建表达式索引,但是这些都没有用。

模式:

-- Table Definition ----------------------------------------------

CREATE TABLE reportimpression (
    datelocal timestamp without time zone,
    devicename text,
    network text,
    sitecode text,
    advertisername text,
    mediafilename text,
    gender text,
    agegroup text,
    views integer,
    impressions integer,
    dwelltime numeric
);

-- Indices -------------------------------------------------------

CREATE INDEX reportimpression_datelocal_index ON reportimpression(datelocal timestamp_ops);
CREATE INDEX reportimpression_viewership_index ON reportimpression(datelocal timestamp_ops,views int4_ops,impressions int4_ops,gender text_ops,agegroup text_ops);
CREATE INDEX reportimpression_test_index ON reportimpression(datelocal timestamp_ops,(date_part('hour'::text, datelocal)) float8_ops);
-- Table Definition ----------------------------------------------

CREATE TABLE reportimpressionday (
    datelocal timestamp without time zone,
    devicename text,
    network text,
    sitecode text,
    advertisername text,
    mediafilename text,
    gender text,
    agegroup text,
    views integer,
    impressions integer,
    dwelltime numeric
);

-- Indices -------------------------------------------------------

CREATE INDEX reportimpressionday_datelocal_index ON reportimpressionday(datelocal timestamp_ops);
CREATE INDEX reportimpressionday_detail_index ON reportimpressionday(datelocal timestamp_ops,views int4_ops,impressions int4_ops,gender text_ops,agegroup text_ops);

说明(分析,缓冲区)输出:

Finalize GroupAggregate  (cost=999842.42..999859.67 rows=3137 width=24) (actual time=43754.700..43754.714 rows=24 loops=1)
  Group Key: (date_part('hour'::text, datelocal))
  Buffers: shared hit=123912 read=823290
  I/O Timings: read=81228.280
  ->  Sort  (cost=999842.42..999843.99 rows=3137 width=24) (actual time=43754.695..43754.698 rows=48 loops=1)
        Sort Key: (date_part('hour'::text, datelocal))
        Sort Method: quicksort  Memory: 28kB
        Buffers: shared hit=123912 read=823290
        I/O Timings: read=81228.280
        ->  Gather  (cost=999481.30..999805.98 rows=3137 width=24) (actual time=43754.520..43777.558 rows=48 loops=1)
              Workers Planned: 1
              Workers Launched: 1
              Buffers: shared hit=123912 read=823290
              I/O Timings: read=81228.280
              ->  Partial HashAggregate  (cost=998481.30..998492.28 rows=3137 width=24) (actual time=43751.649..43751.672 rows=24 loops=2)
                    Group Key: date_part('hour'::text, datelocal)
                    Buffers: shared hit=123912 read=823290
                    I/O Timings: read=81228.280
                    ->  Parallel Seq Scan on reportimpression  (cost=0.00..991555.98 rows=2770129 width=17) (actual time=13.097..42974.126 rows=2338145 loops=2)
                          Filter: ((datelocal >= '2019-02-01 00:00:00'::timestamp without time zone) AND (datelocal < '2019-02-28 00:00:00'::timestamp without time zone))
                          Rows Removed by Filter: 6792750
                          Buffers: shared hit=123912 read=823290
                          I/O Timings: read=81228.280
Planning time: 0.185 ms
Execution time: 43777.701 ms

1 个答案:

答案 0 :(得分:1)

好吧,您的两个查询都在不同的表上(reportimpressionreportimpressionday),因此两个查询的比较实际上不是比较。你们都ANALYZE吗?各种列统计信息也可能起作用。索引或表膨胀可能会有所不同。所有行中是否有较大一部分符合2019年2月的条件?等等

在黑暗中拍摄一张照片,比较两个表的百分比:

SELECT tbl, round(share * 100 / total, 2) As percentage
FROM  (
   SELECT text 'reportimpression' AS tbl
        , count(*)::numeric AS total
        , count(*) FILTER (WHERE datelocal >= '2019-02-01' AND datelocal < '2019-03-01')::numeric AS share
   FROM  reportimpression

   UNION ALL
   SELECT 'reportimpressionday'
        , count(*)
        , count(*) FILTER (WHERE datelocal >= '2019-02-01' AND datelocal < '2019-03-01')
   FROM  reportimpressionday
  ) sub;

reportimpression的那个更大吗?那么它可能刚好超过预期索引可以帮助的数目。

通常,您在{datelocal}上的索引reportimpression_datelocal_index看起来很合适,并且reportimpression_viewership_index甚至允许在自动清理超过表的写负载的情况下仅进行索引扫描。 (尽管impressionsagegroup只是为此付出了沉重的代价,如果没有,它会更好地工作)。

答案

对于我的查询,您获得了 26.6 percent, and day is 26.4 percent 。对于这么大的百分比, 索引通常根本没有用。顺序扫描通常是最快的方法。如果基础表更大,则仅索引扫描 仍然有意义。 (或者您有 severe 严重的表膨胀和较少的膨胀索引,这使索引再次更具吸引力。)

您的第一个查询可能只是跨越临界点。尝试缩小时间范围,直到看到仅索引扫描。您不会看到(位图)索引扫描的合格行占总数的大约5%以上(取决于许多因素)。

查询

请尽可能考虑以下修改后的查询:

SELECT date_part('hour', datelocal)                AS hour
     , SUM(views) FILTER (WHERE gender = 'male')   AS male
     , SUM(views) FILTER (WHERE gender = 'female') AS female
FROM   reportimpression
WHERE  datelocal >= '2019-02-01'
AND    datelocal <  '2019-03-01' -- '2019-02-28'  -- ?
GROUP  BY 1
ORDER  BY 1;

SELECT date_trunc('day', datelocal)                AS day
     , SUM(views) FILTER (WHERE gender = 'male')   AS male
     , SUM(views) FILTER (WHERE gender = 'female') AS female
FROM   reportimpressionday
WHERE  datelocal >= '2019-02-01'
AND    datelocal <  '2019-03-01'
GROUP  BY 1
ORDER  BY 1;

要点

  • 使用'2-1-2019'之类的本地化的日期格式时,请使用明确的格式说明符浏览to_timestamp()。否则,这取决于语言环境设置,并且从具有不同设置的会话中调用时可能会(无提示)中断。而是使用所示的ISO日期/时间格式,而不依赖于区域设置。

  • 您似乎想包含2月的整个月。但是您的查询没有达到上限。一月中,二月可能有29天。 datelocal < '2-28-2019'也排除了2月28日的全部时间。请改用datelocal < '2019-03-01'

  • 如果可以的话,与在SELECT列表中进行相同的表达式进行分组和排序比较便宜。因此,也在那里使用date_trunc()。无需使用其他表达式。如果您需要结果中的日期部分,请将其应用于分组的表达式,例如:

    SELECT date_part('day', date_trunc('day', datelocal)) AS day
    ...
    GROUP  BY date_trunc('day', datelocal)
    ORDER  BY date_trunc('day', datelocal);
    

    嘈杂的代码更多,但速度更快(对于查询计划者而言,也可能更容易优化)。

  • 使用Postgres 9.4或更高版本中的 aggregate FILTER子句。更干净,速度更快。参见: