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

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日

悬赏问题

  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?