pujitan978 2022-12-11 11:24 采纳率: 28.6%
浏览 84
已结题

union all 大数据检索慢

postgres 表的数据量非常大,按天分表(1千五百万行)。检索范围可能几十个表,检索很慢。

order表结构:
产品 代码 名字 分组 生产时间
product code(单独索引) name group timed(单独索引)

mybatis查询:


select product, code, name, group, timed
FROM
( SELECT  product, code, name, group, timed FROM order_20221201 where code = 'AAAA' and timed > '2022-12-01  00:00:00'  and timed <  '2022-12-01  00:00:00' order by timed desc
  UNION ALL SELECT  product, code, name, group, timed FROM order_20221202 where code = 'AAAA' and timed > '2022-12-01  00:00:00'  and timed <  '2022-12-01  00:00:00' order by timed desc
  UNION ALL SELECT  product, code, name, group, timed FROM order_20221203 where code = 'AAAA' and timed > '2022-12-01  00:00:00'  and timed <  '2022-12-01  00:00:00' order by timed desc
  UNION ALL SELECT  product, code, name, group, timed FROM order_20221204 where code = 'AAAA' and timed > '2022-12-01  00:00:00'  and timed <  '2022-12-01  00:00:00' order by timed desc
  UNION ALL SELECT  product, code, name, group, timed FROM order_20221205 where code = 'AAAA' and timed > '2022-12-01  00:00:00'  and timed <  '2022-12-01  00:00:00' order by timed desc
  UNION ALL SELECT  product, code, name, group, timed FROM order_20221206 where code = 'AAAA' and timed > '2022-12-01  00:00:00'  and timed <  '2022-12-01  00:00:00' order by timed desc
  UNION ALL SELECT  product, code, name, group, timed FROM order_20221207 where code = 'AAAA' and timed > '2022-12-01  00:00:00'  and timed <  '2022-12-01  00:00:00' order by timed desc
  UNION ALL SELECT  product, code, name, group, timed FROM order_20221208 where code = 'AAAA' and timed > '2022-12-01  00:00:00'  and timed <  '2022-12-01  00:00:00' order by timed desc
  UNION ALL SELECT  product, code, name, group, timed FROM order_20221209 where code = 'AAAA' and timed > '2022-12-01  00:00:00'  and timed <  '2022-12-01  00:00:00' order by timed desc
  UNION ALL SELECT  product, code, name, group, timed FROM order_202212010 where code = 'AAAA' and timed > '2022-12-01  00:00:00'  and timed <  '2022-12-01  00:00:00' order by timed desc
  UNION ALL SELECT  product, code, name, group, timed FROM order_202212011 where code = 'AAAA' and timed > '2022-12-01  00:00:00'  and timed <  '2022-12-01  00:00:00' order by timed des

 ) AS PP
OFFSET 0
LIMIT 5
问题1: 如果code=AAA在第五个表order_20221205 中,而且满足条件条数也大于5条,
        是否第六个表以后不再检索,如果数据库本身五优化所有表都查询一遍,
        如何能让第六个表以后不查询呢?(最好通过sql解决,如果java解决改动非常大)

问题210个表1亿5千万条,检索时间大概是20秒,如果优化提交加锁速度呢?满足5秒检索
        通过explian 看,也是使用的索引检索

问题3:第一检索非常慢,第二次检索就能稍微快些(数据已加载到缓存中),换个检索区间又非常慢
       使用场景,大部分时间是第一次检索,如何提高第一次检索速度?

问题4:windows 上postgres 如何清除缓存,不用每次重新启动电脑。
  • 写回答

6条回答 默认 最新

  • 日拱一两卒 上海惠普机器学习算法工程师 2022-12-11 11:49
    关注
    获得4.40元问题酬金

    首先看索引有没有加,然后可以考虑用视图

    评论

报告相同问题?

问题事件

  • 系统已结题 12月19日
  • 创建了问题 12月11日