1

我必须从两张表中获取记录,一张是超表,另一张是普通表。

超级表主键(一个 UUID,不是时间戳列)在第二个普通表中用作外键。

超级表与普通表具有一对多的关系。

如果我在加入此表后选择记录,我会在这里获得超表的所有好处吗?

我正在使用 postgresql 数据库作为时间尺度。

下面是相同的创建表查询。demography_person 是超表,emotions_person 是普通表

CREATE TABLE public.demography_person
(
  start_timestamp timestamp with time zone NOT NULL,
  end_timestamp timestamp with time zone,
  demography_person_id character varying NOT NULL,
  device_id bigint,
  age_actual numeric,
  age_band integer,
  gender integer,
  dwell_time_in_millis bigint,
  customer_id bigint NOT NULL
);

SELECT create_hypertable('demography_person', 'start_timestamp');

CREATE TABLE public.emotions_person
(
  emotion_start_timestamp timestamp with time zone NOT NULL,
  demography_person_id character varying NOT NULL,
  count integer,
  emotion integer,
  emotion_percentage numeric
);

选择 sql 查询就像:-

SELECT * FROM crosstab
             (
               $$
                   SELECT * FROM  ( select  to_char(dur,'HH24') as duration , dur as time_for_sorting from 
                generate_series(
            timestamp '2019-04-01 00:00:00',
            timestamp '2020-03-09 23:59:59' ,
            interval  '1 hour'
                ) as dur   ) d
                   LEFT JOIN (  
                   select to_char(
                                    start_timestamp ,
                                   'HH24'
                                   )
                   as duration,
                   emotion,count(*) as count from demography_person dp INNER JOIN (
            select  distinct ON (demography_person_id)  demography_person_id, emotion_start_timestamp,count,emotion,emotion_percentage,
            (CASE emotion when 4 THEN 1 when 6  THEN 2 when 1 THEN 3  WHEN 3 THEN 4 WHEN 2 THEN 5  when 7 THEN 6  when 5 THEN 7  ELSE 8 END )  
             as emotion_key_for_sorting from emotions_person  where    demography_person_id in (select demography_person_id from demography_person where start_timestamp >= '2019-04-01 00:00:00'
            AND start_timestamp <= '2020-03-09 23:59:59' AND device_id IN ( 2052,2692,1797,2695,1928,2697,2698,1931,2574,2575,2706,1942,1944,2713,1821,2719,2720,2721,2722,2723,2596,2725,2217,2603,1852,2750,1726,1727,2754,2757,1990,2759,2760,2376,2761,2762,2257,2777,2394,2651,2652,1761,2658,1762,2659,2788,2022,2791,2666,1770,2026,2028,2797,2675,1780,2549 ))   
               order by demography_person_id asc,emotion_percentage desc, emotion_key_for_sorting asc 
                   ) ep ON
                   ep.demography_person_id = dp.demography_person_id
                   WHERE start_timestamp >= '2019-04-01 00:00:00'
AND start_timestamp <= '2020-03-09 23:59:59' AND device_id IN ( 2052,2692,1797,2695,1928,2697,2698,1931,2574,2575,2706,1942,1944,2713,1821,2719,2720,2721,2722,2723,2596,2725,2217,2603,1852,2750,1726,1727,2754,2757,1990,2759,2760,2376,2761,2762,2257,2777,2394,2651,2652,1761,2658,1762,2659,2788,2022,2791,2666,1770,2026,2028,2797,2675,1780,2549 ) AND gender IN ( 1,2 )
                   group by 1,2 ORDER  BY 1,2 ASC
                   ) t USING (duration) GROUP  BY 1,2,3,4 ORDER  BY time_for_sorting;           
               $$ ,
               $$
                 select emotion from (
                                                          values ('1'), ('2'), ('3'),('4'), ('5'), ('6'),('7'), ('8')
                                                    ) t(emotion)
              $$ 
         ) AS ct 
              (
                   duration text,
                   time_for_sorting  timestamp,
                  ANGER bigInt,
                  DISGUSTING bigInt,
                  FEAR bigInt,
                  HAPPY bigInt,
                  NEUTRAL bigInt,
                  SAD bigInt,
                  SURPRISE bigInt,
                  NO_DETECTION bigInt
             ); 
4

1 回答 1

1

如果我有一个查询,我在 timescaledb 中加入一个带有普通(非超)表的超表,我会从超表中获益吗

我不完全理解这个问题,并看到了 2 种解释:

  1. 我会从使用 TimescaleDB 和 hypertable 来改进这个查询中受益吗?
  2. 我可以加入超表和普通表以及如何使上述查询执行得更好?

如果您只需要对大型数据集执行复杂的查询,如果您提供索引,PostgreSQL 可以做得很好。TimescaleDB 为Timeseries 工作流提供好处,特别是当工作流包括数据按顺序摄取、时间相关查询、时间序列运算符和/或使用 TimescaleDB 特定功能(如连续聚合压缩)时,即不仅仅是查询。TimescaleDB 专为大量时间序列数据而设计。我希望它能澄清第一个问题。

在 TimescaleDB 中,连接存储时间序列数据的超表和包含时间序列数据元数据的普通表是很常见的。TimescaleDB 实现约束排除以提高查询性能。但是,由于不常见的查询表达式或过于复杂的查询,它可能在某些情况下不适用。

问题中的查询非常复杂。所以我建议在查询上使用ANALYZE来查看查询规划器是否遗漏了一些优化。

我看到查询生成数据,我怀疑它可以做很多事情来产生好的查询计划。所以这是我获得良好性能的最大担忧。如果您能解释在查询中生成数据的动机,那就太好了。

我看到的另一个问题是demography_person_id in (select demography_person_id from demography_person ...where 条件下的嵌套查询。外部查询是内部联接的一部分,与嵌套查询中的表具有相同的表。我希望它可以在没有使用内部连接的嵌套子查询的情况下重写。

我怀疑 TimescaleDB 或 PostgreSQL 能否有效地执行查询。该查询需要手动人工重写。

于 2020-03-26T13:07:46.487 回答