加快创建视图

时间:2015-11-03 11:44:22

标签: mysql

任何人都可以建议加快我创造的观点吗? 它从数据库中获取正确的数据,但编译速度很慢,因为它做了很多选择,但我不确定如何解决这个问题。

以下是查询:

CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`%` SQL SECURITY DEFINER VIEW `ResponseMonthly_View`
AS SELECT 
(SELECT SurveyMonth
    FROM surveyResponse_View AS v
    WHERE v.SurveyMonth = main.SurveyMonth GROUP BY v.SurveyMonth) AS SurveyMonth,
(SELECT Count(*)
    FROM surveyResponse_View AS v
    WHERE v.SurveyMonth = main.SurveyMonth GROUP BY v.SurveyMonth) AS Responses,
(SELECT COUNT(*)
    FROM surveyResponse_View AS v
    WHERE v.funded = 1 AND v.SurveyMonth = main.SurveyMonth GROUP BY v.SurveyMonth) AS FundedResponses,
(SELECT COUNT(*)
    FROM surveyResponse_View AS t
    WHERE t.funded = 0 AND t.SurveyMonth = main.SurveyMonth GROUP BY t.SurveyMonth) AS UnfundedResponses,

(SELECT ROUND(AVG(NoOfUL))
    FROM surveyResponse_View AS m
    WHERE m.SurveyMonth = main.SurveyMonth AND NoOFUl < 200 AND TutorSession < 200 AND IndependentSession < 200 AND DedicatedSession < 200  GROUP BY m.SurveyMonth) AS AllNoOfUl,

(SELECT ROUND(AVG(TutorSession))
    FROM surveyResponse_View AS m
    WHERE m.SurveyMonth = main.SurveyMonth AND NoOFUl < 200 AND TutorSession < 200 AND IndependentSession < 200 AND DedicatedSession < 200 GROUP BY m.SurveyMonth) AS AllTutorSession,

(SELECT ROUND(AVG(IndependentSession))
    FROM surveyResponse_View AS m
    WHERE m.SurveyMonth = main.SurveyMonth AND NoOFUl < 200 AND TutorSession < 200 AND IndependentSession < 200 AND DedicatedSession < 200 GROUP BY m.SurveyMonth) AS AllIndependentSession,

(SELECT ROUND(AVG(DedicatedSession))
    FROM surveyResponse_View AS m
    WHERE m.SurveyMonth = main.SurveyMonth AND NoOFUl < 200 AND TutorSession < 200 AND IndependentSession < 200 AND DedicatedSession < 200 GROUP BY m.SurveyMonth) AS AllDedicatedSession,

(SELECT ROUND(AVG(TutorSession) + AVG(IndependentSession) + AVG(DedicatedSession) / Count(*))
    FROM surveyResponse_View AS m
    WHERE m.SurveyMonth = main.SurveyMonth AND NoOFUl < 200 AND TutorSession < 200 AND IndependentSession < 200 AND DedicatedSession < 200 GROUP BY m.SurveyMonth) AS AverageForSum,

(SELECT ROUND(AVG(NoOfUL))
    FROM surveyResponse_View AS m
    WHERE m.SurveyMonth = main.SurveyMonth AND funded = 1 AND NoOFUl < 200 AND TutorSession < 200 AND IndependentSession < 200 AND DedicatedSession < 200  GROUP BY m.SurveyMonth) AS FundedNoOfUl,

(SELECT ROUND(AVG(TutorSession))
    FROM surveyResponse_View AS m
    WHERE m.SurveyMonth = main.SurveyMonth AND funded = 1 AND NoOFUl < 200 AND TutorSession < 200 AND IndependentSession < 200 AND DedicatedSession < 200 GROUP BY m.SurveyMonth) AS FundedTutorSession,

(SELECT ROUND(AVG(IndependentSession))
    FROM surveyResponse_View AS m
    WHERE m.SurveyMonth = main.SurveyMonth AND funded = 1 AND NoOFUl < 200 AND TutorSession < 200 AND IndependentSession < 200 AND DedicatedSession < 200 GROUP BY m.SurveyMonth) AS FundedIndependentSession,

(SELECT ROUND(AVG(DedicatedSession))
    FROM surveyResponse_View AS m
    WHERE m.SurveyMonth = main.SurveyMonth AND funded = 1 AND NoOFUl < 200 AND TutorSession < 200 AND IndependentSession < 200 AND DedicatedSession < 200 GROUP BY m.SurveyMonth) AS FundedDedicatedSession,

(SELECT ROUND(AVG(TutorSession) + AVG(IndependentSession) + AVG(DedicatedSession) / Count(*))
    FROM surveyResponse_View AS m
    WHERE m.SurveyMonth = main.SurveyMonth AND funded = 1 AND NoOFUl < 200 AND TutorSession < 200 AND IndependentSession < 200 AND DedicatedSession < 200 GROUP BY m.SurveyMonth) AS FundedAverageForSum,

(SELECT ROUND(AVG(NoOfUL))
    FROM surveyResponse_View AS m
    WHERE m.SurveyMonth = main.SurveyMonth AND funded = 0 AND NoOFUl < 200 AND TutorSession < 200 AND IndependentSession < 200 AND DedicatedSession < 200  GROUP BY m.SurveyMonth) AS UnfundedNoOfUl,

(SELECT ROUND(AVG(TutorSession))
    FROM surveyResponse_View AS m
    WHERE m.SurveyMonth = main.SurveyMonth AND funded = 0 AND NoOFUl < 200 AND TutorSession < 200 AND IndependentSession < 200 AND DedicatedSession < 200 GROUP BY m.SurveyMonth) AS UnfundedTutorSession,

(SELECT ROUND(AVG(IndependentSession))
    FROM surveyResponse_View AS m
    WHERE m.SurveyMonth = main.SurveyMonth AND funded = 0 AND NoOFUl < 200 AND TutorSession < 200 AND IndependentSession < 200 AND DedicatedSession < 200 GROUP BY m.SurveyMonth) AS UnfundedIndependentSession,

(SELECT ROUND(AVG(DedicatedSession))
    FROM surveyResponse_View AS m
    WHERE m.SurveyMonth = main.SurveyMonth AND funded = 0 AND NoOFUl < 200 AND TutorSession < 200 AND IndependentSession < 200 AND DedicatedSession < 200 GROUP BY m.SurveyMonth) AS UnfundedDedicatedSession,

(SELECT ROUND(AVG(TutorSession) + AVG(IndependentSession) + AVG(DedicatedSession) / Count(*))
    FROM surveyResponse_View AS m
    WHERE m.SurveyMonth = main.SurveyMonth AND funded = 0 AND NoOFUl < 200 AND TutorSession < 200 AND IndependentSession < 200 AND DedicatedSession < 200 GROUP BY m.SurveyMonth) AS UnfundedAverageForSum



FROM surveyResponse_View AS main WHERE NoOFUl < 200 AND TutorSession < 200 AND IndependentSession < 200 AND DedicatedSession < 200 GROUP BY SurveyMonth;

任何帮助将不胜感激,谢谢!

0 个答案:

没有答案