我正在创建一个显示饼图的网络应用。为了在单个HTTP请求中从 PostgreSQL 9.3 数据库获取图表的所有数据,我将多个SELECT
语句与UNION ALL
组合在一起 - 这是一部分:
SELECT 'spf' as type, COUNT(*)
FROM (SELECT cai.id
FROM common_activityinstance cai
JOIN common_activityinstance_settings cais ON cai.id = cais.activityinstance_id
JOIN common_activitysetting cas ON cas.id = cais.id
JOIN quizzes_quiz q ON q.id = cai.activity_id
WHERE cai.end_time::date = '2015-09-12'
AND q.name != 'Exit Ticket Quiz'
AND cai.activity_type = 'QZ'
AND (cas.key = 'disable_student_nav' AND cas.value = 'True'
OR cas.key = 'pacing' AND cas.value = 'student')
GROUP BY cai.id
HAVING COUNT(cai.id) = 2) sub
UNION ALL
SELECT 'spn' as type, COUNT(*)
FROM common_activityinstance cai
JOIN common_activityinstance_settings cais ON cai.id = cais.activityinstance_id
JOIN common_activitysetting cas ON cas.id = cais.id
WHERE cai.end_time::date = '2015-09-12'
AND cai.activity_type = 'QZ'
AND cas.key = 'disable_student_nav'
AND cas.value = 'False'
UNION ALL
SELECT 'tp' as type, COUNT(*)
FROM (SELECT cai.id
FROM common_activityinstance cai
JOIN common_activityinstance_settings cais ON cai.id = cais.activityinstance_id
JOIN common_activitysetting cas ON cas.id = cais.id
WHERE cai.end_time::date = '2015-09-12'
AND cai.activity_type = 'QZ'
AND cas.key = 'pacing' AND cas.value = 'teacher') sub;
这会产生一个很好的小响应,用于发送回客户端:
type | count
------+---------
spf | 100153
spn | 96402
tp | 84211
我想知道我的查询是否可以提高效率。每个SELECT语句主要使用相同的JOIN操作。有没有办法不为每个新SELECT重复JOIN?
我实际上更喜欢有3列的单行。
或者,总的来说,是否存在一些与我正在做的完全不同但更好的方法?
答案 0 :(得分:2)
您可以在CTE的单个主查询中捆绑大部分费用,并多次重复使用该结果。
这将返回以每个type
(as requested in the comment)命名的单行,其中包含三列:
WITH cte AS (
SELECT cai.id, cai.activity_id, cas.key, cas.value
FROM common_activityinstance cai
JOIN common_activityinstance_settings s ON s.activityinstance_id = cai.id
JOIN common_activitysetting cas ON cas.id = s.id
WHERE cai.end_time::date = '2015-09-12' -- problem?
AND cai.activity_type = 'QZ'
AND (cas.key = 'disable_student_nav' AND cas.value IN ('True', 'False') OR
cas.key = 'pacing' AND cas.value IN ('student', 'teacher'))
)
SELECT *
FROM (
SELECT count(*) AS spf
FROM (
SELECT c.id
FROM cte c
JOIN quizzes_quiz q ON q.id = c.activity_id
WHERE q.name <> 'Exit Ticket Quiz'
AND (c.key, c.value) IN (('disable_student_nav', 'True')
, ('pacing', 'student'))
GROUP BY 1
HAVING count(*) = 2
) sub
) spf
, (
SELECT count(key = 'disable_student_nav' AND value = 'False' OR NULL) AS spn
, count(key = 'pacing' AND value = 'teacher' OR NULL) AS tp
FROM cte
) spn_tp;
应该为Postgres 9.3工作。在Postgres 9.4中,您可以使用新的聚合FILTER
子句:
count(*) FILTER (WHERE key = 'disable_student_nav' AND value = 'False') AS spn
, count(*) FILTER (WHERE key = 'pacing' AND value = 'teacher') AS tp
两种语法变体的详细信息:
标记为problem?
的条件可能是一个很大的性能问题,具体取决于cai.end_time
的数据类型。首先,它不是sargable。如果它是timestamptz
类型,则表达式难以索引,因为结果取决于会话的当前时区设置 - 在不同时区执行时也会导致不同的结果。 / p>
比较
您只需命名应该定义日期的时区。以我在维也纳的时区为例:
WHERE cai.end_time >= '2015-09-12 0:0'::timestamp AT TIME ZONE 'Europe/Vienna'
AND cai.end_time < '2015-09-13 0:0'::timestamp AT TIME ZONE 'Europe/Vienna'
您也可以提供简单的timestamptz
值。你甚至可以只是:
WHERE cai.end_time >= '2015-09-12'::date
AND cai.end_time < '2015-09-12'::date + 1
但第一个版本不依赖于当前时区设置 上面链接中的详细说明。
现在,查询可以使用您的索引,如果您的表中有很多不同的日期,那么查询速度会快得多。
答案 1 :(得分:1)
这只是一个完全不同的方法的草图:为您需要的所有条件构造一个布尔“超立方体” 在你的“交叉制表”中。选择或聚合子集的逻辑可以在以后完成(例如抑制exit_tickets,业务逻辑对我来说不清楚)
SELECT DISTINCT not_exit, disabled, pacing
, COUNT(*) AS the_count
FROM (SELECT DISTINCT cai.id
, EXISTS (SELECT *
FROM quizzes_quiz q
WHERE q.id = cai.activity_id AND q.name != 'Exit Ticket Quiz'
) AS not_exit
, EXISTS ( SELECT *
FROM common_activityinstance_settings cais
JOIN common_activitysetting cas ON cas.id = cais.id
WHERE cai.id = cais.activityinstance_id
AND cas.key = 'disable_student_nav' AND cas.value = 'True'
) AS disabled
, EXISTS ( SELECT *
FROM common_activityinstance_settings cais
JOIN common_activitysetting cas ON cas.id = cais.id
WHERE cai.id = cais.activityinstance_id
AND cas.key = 'pacing' AND cas.value = 'student')
) AS pacing
FROM common_activityinstance cai
WHERE cai.end_time::date = '2015-09-12' AND cai.activity_type = 'QZ'
) my_cube
GROUP BY 1,2,3
ORDER BY 1,2,3
;
最后说明:此方法基于我的假设,基础数据模型实际上是一个EAV模型,每个学生最多可以出现一次属性。
答案 2 :(得分:0)
这是部分答案。第二个可以组合成一个查询:
SELECT (case when key = 'disable_student_nav' then 'spn'
when key = 'pacing' then 'tp'
end) as type, COUNT(*)
FROM common_activityinstance cai JOIN
common_activityinstance_settings cais
ON cai.id = cais.activityinstance_id JOIN
common_activitysetting cas
ON cas.id = cais.id
WHERE cai.end_time::date = '2015-09-12' AND cai.activity_type = 'QZ' AND
(key, value) in (('disable_student_nav', 'False'), ('pacing', 'teacher'))
GROUP BY type
我想知道是否有办法将第一组置于类似的逻辑中。例如,如果QZ
条件可以应用于所有三个组,那么添加第一组将很容易。
答案 3 :(得分:0)
您可以将case
与where
子句中的条件一起用于每种类型。但是,第一个查询的having
条件不会被此满足。
select type, count(*) as count
from
(
SELECT cai.id,
case when q.name!= 'Exit Ticket Quiz' and key = 'disable_student_nav'
AND value = 'True' OR key = 'pacing' AND value = 'student' then 'spf'
when key = 'disable_student_nav' AND value = 'False' then 'spn'
when key = 'pacing' AND value = 'teacher' then 'tp'
end as type
FROM common_activityinstance cai
JOIN common_activityinstance_settings cais ON cai.id = cais.activityinstance_id
JOIN common_activitysetting cas ON cas.id = cais.id
JOIN quizzes_quiz q ON q.id = cai.activity_id
WHERE cai.end_time::date = '2015-09-12'
AND q.name != 'Exit Ticket Quiz'
AND cai.activity_type = 'QZ'
) t
group by type
答案 4 :(得分:-1)
没有办法让查询更有效率,没有。你可以设置一个视图或其他什么,但它总是要经历三次。但是你可以通过在PHP或PL / SQL或其他任何方面进行一些后处理来解决问题。从更简单的查询开始,如下所示:
SELECT COUNT(*),cai.id,q.name,key,value 来自common_activityinstance cai JOIN common_activityinstance_settings cais ON cai.id = cais.activityinstance_id JOIN common_activitysetting cas ON cas.id = cais.id 在哪里cai.end_time :: date =&#39; 2015-09-12&#39; GROUP BY cai.id,q.name,key,value
...我从你的解释中不清楚这是否会导致合理数量的输出行。但假设确实如此,请编写一些代码将它们按到您想要的形状中。