[英]Optimized querying in PostgreSQL
假設您有一個名為tracker的表,其中包含以下記錄。
issue_id | ingest_date | verb,status
10 2015-01-24 00:00:00 1,1
10 2015-01-25 00:00:00 2,2
10 2015-01-26 00:00:00 2,3
10 2015-01-27 00:00:00 3,4
11 2015-01-10 00:00:00 1,3
11 2015-01-11 00:00:00 2,4
我需要以下結果
10 2015-01-26 00:00:00 2,3
11 2015-01-11 00:00:00 2,4
我正在嘗試這個查詢
select *
from etl_change_fact
where ingest_date = (select max(ingest_date)
from etl_change_fact);
但是,這只會給我
10 2015-01-26 00:00:00 2,3
該記錄。
但是,我希望所有唯一記錄(change_id)與
(a)max(ingest_date)AND
(b)動詞列優先級為(2-第一優先,1-第二優先,3-最后優先)
因此,我需要以下結果
10 2015-01-26 00:00:00 2,3
11 2015-01-11 00:00:00 2,4
請幫助我有效地查詢它。
PS:我不為ingest_date編制索引,因為我將在Distributed Computing設置中將其設置為“ distribution key”。 我是數據倉庫和查詢的新手。
因此,請以最佳方式幫助我達到TB大小的數據庫。
這是一個典型的“最大的每組”問題。 如果您在此處搜索此標簽,則將獲得大量解決方案-包括MySQL。
對於Postgres,最快的方法是使用distinct on
(這是SQL語言的Postgres專有擴展)
select distinct on (issue_id) issue_id, ingest_date, verb, status
from etl_change_fact
order by issue_id,
case verb
when 2 then 1
when 1 then 2
else 3
end, ingest_date desc;
您可以增強原始查詢以使用相關的子查詢來實現相同的目的:
select f1.*
from etl_change_fact f1
where f1.ingest_date = (select max(f2.ingest_date)
from etl_change_fact f2
where f1.issue_id = f2.issue_id);
編輯
對於過時且不受支持的Postgres版本,您可以使用以下方法來擺脫困境:
select f1.*
from etl_change_fact f1
where f1.ingest_date = (select f2.ingest_date
from etl_change_fact f2
where f1.issue_id = f2.issue_id
order by case verb
when 2 then 1
when 1 then 2
else 3
end, ingest_date desc
limit 1);
SQLFiddle示例: http ://sqlfiddle.com/#!15/3bb05/1
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.