postgresql - 数百万条记录的 Postgresql 数据库变慢(选择)
问题描述
尝试获取数百万条记录的数据时,Postgresql 数据库变慢。我尝试使用Materialized view,但是性能非常快,但它没有提供实时数据。
我也在使用聚合前。sum、count、group by 子句等...
SELECT offer_id as off_id,
COUNT(distinct ip) as hosts,
COUNT(distinct click_id) as clicks
FROM offer_affiliate_stats
WHERE
created_dt >= '2019-06-01'
AND
created_dt <= '2019-06-30'
GROUP BY off_id;
我试过Materialized view。
索引应用于 id、created_dt、click_id
我的输出应该是这样的:
off_id | 79
hosts | 4
clicks | 4
offer_name | "Testing Javelin"
offer_id |
total_conversions |
total_income |
optimised_count |
optimised_income |
approved_income |
approved_conversions |
declined_income |
declined_conversions |
total_payout |
实际上,如果不使用distinct关键字,它可以完美地工作,但是当我使用distinct时需要很长时间。
解决方案
你应该配置你的数据库吗?
您会看到以下链接:https ://www.postgresql.org/docs/current/runtime-config-resource.html
特别是work_mem
默认值是 4MB。您可以增加到 100MB。
您更改代码如下:
SELECT offer_id as off_id,
COUNT(ip) as hosts,
COUNT(click_id) as clicks
FROM
(select distinct offer_id,
ip ,
click_id
from offer_affiliate_stats
WHERE created_dt >= '2019-06-01'
AND created_dt <= '2019-06-30' ) as t
GROUP BY off_id;
推荐阅读
- authentication - Azure AD 应用授予不记名令牌,其范围来自同一租户下的不同 AD 应用
- api - 使用 ASP.NET Core 定义 API 调用程序类
- workflow - 露天工作流程详细信息页面 - 如何隐藏参数?
- csv - 使用 apache nifi 从 csv 中删除“”
- c# - 拆分 html 以分别显示 CrystalQuartz 图形
- android - 将 longClickListener 添加到 ViewGroup 并将单击侦听器添加到 viewgroups 子项-如何使两者都起作用?
- node.js - SequelizeDatabaseError:SQLITE_ERROR:没有这样的表:用户
- python-3.x - BLE 设备在一段时间后断开连接
- node.js - 安装Vue后无法创建Vue项目,怎么办?
- java - 无法让edittext在滚动视图中工作