我有一個事件表格,具有以下列user_id
,points
,created
。我需要的是獲得給定月份的所有積分,給定的user_id
總積分和總排名。我需要做的是在這導致了以下嘗試一個查詢:PostgreSQL查詢性能和變化
select
e.user_id,
sum(e.points) as total_points,
(select sum(points) from event where user_id = 1 and extract(month from created) = 1) as month_points,
(select rank from (select user_id as id, rank() over (order by sum(points) desc) from event group by user_id) as rank where id = 1)
from
event pte
where
e.user_id = 1
group by
e.user_id
;
我想什麼要問的是:
- 此查詢可能會很慢?
- 它可以做得更好/以另一種方式?
- 是
rank
功能足夠可靠還是被誤用?
1)這將是非常快的,因爲它包含一個錯誤(在第二個子查詢)。 2)是:更正錯誤3)取決於查詢的意圖 – wildplasser
@wildplasser,我已經更新了這個問題。 – Opal