我有一个查询来计算一所大学的学生(按类型划分)有多少吃食物类别(例如蔬菜,肉类,鱼......)。
我数据库中的每种菜肴都有一个id dish_composition。我写了查询,但计算所有类别使用711秒(12分钟)。这是非常低效的。你能帮我缩短执行时间吗?
这是查询:
<?php
$query=(SELECT SUM(uomini) as uomini_carne, SUM(donne) as donne_carne FROM ( SELECT SUM(case when person.sex='M' then 1 end) as uomini, SUM(case when person.sex='F' then 1 end) as donne FROM `dish` JOIN dish_bought ON dish.id_dish=dish_bought.id_dish JOIN meal_receipt ON dish_bought.id_receipt=meal_receipt.id_receipt JOIN relation ON meal_receipt.id_stu=relation.id_stu JOIN person ON relation.id_pers=person.id_pers WHERE dish.dish_composition BETWEEN 51 AND 54 GROUP BY dish.dish_composition) carne)
UNION ALL
(SELECT SUM(uomini) as uomini_carne, SUM(donne) as donne_carne FROM ( SELECT SUM(case when person.sex='M' then 1 end) as uomini, SUM(case when person.sex='F' then 1 end) as donne FROM `dish` JOIN dish_bought ON dish.id_dish=dish_bought.id_dish JOIN meal_receipt ON dish_bought.id_receipt=meal_receipt.id_receipt JOIN relation ON meal_receipt.id_stu=relation.id_stu JOIN person ON relation.id_pers=person.id_pers WHERE dish.dish_composition BETWEEN 10 AND 34 GROUP BY dish.dish_composition) primo)
UNION ALL
(SELECT SUM(uomini) as uomini_carne, SUM(donne) as donne_carne FROM ( SELECT SUM(case when person.sex='M' then 1 end) as uomini, SUM(case when person.sex='F' then 1 end) as donne FROM `dish` JOIN dish_bought ON dish.id_dish=dish_bought.id_dish JOIN meal_receipt ON dish_bought.id_receipt=meal_receipt.id_receipt JOIN relation ON meal_receipt.id_stu=relation.id_stu JOIN person ON relation.id_pers=person.id_pers WHERE dish.dish_composition BETWEEN 60 AND 62 GROUP BY dish.dish_composition) pesce)
UNION ALL
(SELECT SUM(uomini) as uomini_carne, SUM(donne) as donne_carne FROM ( SELECT SUM(case when person.sex='M' then 1 end) as uomini, SUM(case when person.sex='F' then 1 end) as donne FROM `dish` JOIN dish_bought ON dish.id_dish=dish_bought.id_dish JOIN meal_receipt ON dish_bought.id_receipt=meal_receipt.id_receipt JOIN relation ON meal_receipt.id_stu=relation.id_stu JOIN person ON relation.id_pers=person.id_pers WHERE dish.dish_composition IN (71,72,81,82,93) GROUP BY dish.dish_composition) verdura)
UNION ALL
(SELECT SUM(uomini) as uomini_carne, SUM(donne) as donne_carne FROM ( SELECT SUM(case when person.sex='M' then 1 end) as uomini, SUM(case when person.sex='F' then 1 end) as donne FROM `dish` JOIN dish_bought ON dish.id_dish=dish_bought.id_dish JOIN meal_receipt ON dish_bought.id_receipt=meal_receipt.id_receipt JOIN relation ON meal_receipt.id_stu=relation.id_stu JOIN person ON relation.id_pers=person.id_pers WHERE dish.dish_composition BETWEEN 91 AND 92 GROUP BY dish.dish_composition) uova)
UNION ALL
(SELECT SUM(uomini) as uomini_carne, SUM(donne) as donne_carne FROM ( SELECT SUM(case when person.sex='M' then 1 end) as uomini, SUM(case when person.sex='F' then 1 end) as donne FROM `dish` JOIN dish_bought ON dish.id_dish=dish_bought.id_dish JOIN meal_receipt ON dish_bought.id_receipt=meal_receipt.id_receipt JOIN relation ON meal_receipt.id_stu=relation.id_stu JOIN person ON relation.id_pers=person.id_pers WHERE dish.dish_composition IN (101,101,212) GROUP BY dish.dish_composition) formaggi)
UNION ALL
(SELECT SUM(uomini) as uomini_carne, SUM(donne) as donne_carne FROM ( SELECT SUM(case when person.sex='M' then 1 end) as uomini, SUM(case when person.sex='F' then 1 end) as donne FROM `dish` JOIN dish_bought ON dish.id_dish=dish_bought.id_dish JOIN meal_receipt ON dish_bought.id_receipt=meal_receipt.id_receipt JOIN relation ON meal_receipt.id_stu=relation.id_stu JOIN person ON relation.id_pers=person.id_pers WHERE dish.dish_composition=83 GROUP BY dish.dish_composition) legumi)
UNION ALL
(SELECT SUM(uomini) as uomini_carne, SUM(donne) as donne_carne FROM ( SELECT SUM(case when person.sex='M' then 1 end) as uomini, SUM(case when person.sex='F' then 1 end) as donne FROM `dish` JOIN dish_bought ON dish.id_dish=dish_bought.id_dish JOIN meal_receipt ON dish_bought.id_receipt=meal_receipt.id_receipt JOIN relation ON meal_receipt.id_stu=relation.id_stu JOIN person ON relation.id_pers=person.id_pers WHERE dish.dish_composition=415 GROUP BY dish.dish_composition) frutta)";
?>
答案 0 :(得分:0)
应该是类似的......你必须完成案例陈述......
如果你不能使用IIF,那么用Case
替换 1)转换原始数据
2)总结原始数据
SELECT
SUM(iif(x.gender = 'F',1,0)) AS Female,
SUM(iif(x.gender = 'M',1,0)) AS Male,
x.DishName
FROM
(SELECT
person.sex as gender,
case
when dish.dish_composition BETWEEN 51 AND 54 then 'carne'
when .... then 'dish 2'
when .... then 'dish 3'
else 'dish 100'
end as DishName
FROM dish
JOIN dish_bought
ON dish.id_dish = dish_bought.id_dish
JOIN meal_receipt
ON dish_bought.id_receipt = meal_receipt.id_receipt
JOIN relation
ON meal_receipt.id_stu = relation.id_stu
JOIN person
ON relation.id_pers = person.id_pers
) x
group by x.gender, x.DishName