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解决改动非常大)
问题2: 10个表1亿5千万条,检索时间大概是20秒,如果优化提交加锁速度呢?满足5秒检索
通过explian 看,也是使用的索引检索
问题3:第一检索非常慢,第二次检索就能稍微快些(数据已加载到缓存中),换个检索区间又非常慢
使用场景,大部分时间是第一次检索,如何提高第一次检索速度?
问题4:windows 上postgres 如何清除缓存,不用每次重新启动电脑。
首先看索引有没有加,然后可以考虑用视图
贴下你的explain输出看看
问题1: 如果code=AAA在第五个表order_20221205 中,而且满足条件条数也大于5条,
是否第六个表以后不再检索,如果数据库本身五优化所有表都查询一遍,
如何能让第六个表以后不查询呢?(最好通过sql解决,如果java解决改动非常大)
> **这个不能这样武断,因为在计算中,无法确定要不要查第六张表。**
问题2: 10个表1亿5千万条,检索时间大概是20秒,如果优化提交加锁速度呢?满足5秒检索
通过explian 看,也是使用的索引检索
> **你这个SQL有一个可以优化的地方,应该是可以提速速度,就是每个表取前5条,最终联合排序再取前5条,这样可以介绍联合临时表读写时间,进而提升效率** ** 你可以尝试一下,不会私信我。**
问题3:第一检索非常慢,第二次检索就能稍微快些(数据已加载到缓存中),换个检索区间又非常慢
使用场景,大部分时间是第一次检索,如何提高第一次检索速度?
> **这个问题除非你每个区的数据都缓存到内存中,不然冷数据第一次都会从磁盘读取,进而第一次查询慢的原因再此**
如果你的union all很多的话,可以做成视图,相当于把所有的表数据合成一张表来显示。
但数据实际上还是分表存的,只是展示出来的结果让你看上去是一张表。相当于把union all这一层给提前做好了。这样查询的时候就只要控制某个字段是时间,比如date>'2019-03-05’这样子
如有帮助,望采纳
可以尝试将查询条件中的时间进行索引,这样可以加快查询的速度。例如在查询条件中加入:
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_view
WHERE code = 'AAAA'
AND timed > '2022-12-01 00:00:00'
AND timed < '2022-12-01 00:00:00'
ORDER BY timed DESC
在数据库中创建 order_view 视图,该视图组合了所有 order_20221201~order_202212011 表,可以减少查询时的表
改进的地方: