[英]make mysql query more efficient
so at the minute my initial query looks like this: 因此,此刻我的初始查询如下所示:
SELECT
`surveyEntries`.`ID` AS EntryID,
`surveyEntries`.`created` AS EntryDate,
`hw_services`.`name` AS Provider,
`surveyQuestions`.`ID` AS QuestionID,
`surveyQuestions`.`label` AS Question,
`survey_meta`.`answer` AS Answer,
`surveyQuestions`.`parentID` AS ParentQuestion
FROM `survey` , `surveyQuestions` , `surveyEntries` , `survey_meta` , `hw_services`
WHERE `survey`.`ID` =2
AND `survey`.`ID` = `surveyQuestions`.`surveyID`
AND `survey`.`ID` = `surveyEntries`.`surveyID`
AND `surveyEntries`.`ID` = `survey_meta`.`entryID`
AND `hw_services`.`ID` = `surveyEntries`.`hw_serviceID`
AND `surveyQuestions`.`ID` = `survey_meta`.`questionID`
Now i have read around and this is a very weak lengthy query that could be optimised. 现在我已经阅读了,这是一个非常弱的冗长查询,可以对其进行优化。
Does any one know how i could do this? 有谁知道我该怎么做? my attempt failed to provide the same result which is below:
我的尝试未能提供以下相同的结果:
SELECT
`surveyEntries`.`ID` AS EntryID,
`surveyEntries`.`created` AS EntryDate,
`hw_services`.`name` AS Provider,
`surveyQuestions`.`ID` AS QuestionID,
`surveyQuestions`.`label` AS Question,
`survey_meta`.`answer` AS Answer,
`surveyQuestions`.`parentID` AS ParentQuestion
FROM `survey`
JOIN `surveyQuestions`
ON `survey`.`ID` = `surveyQuestions`.`surveyID`
JOIN `surveyEntries`
ON `survey`.`ID` = `surveyEntries`.`surveyID`
JOIN `survey_meta`
ON `survey`.`ID` = `survey_meta`.`entryID`
JOIN `hw_services`
ON `survey`.`ID` = `hw_services`.`ID`
WHERE `survey`.`ID` =2
EDIT:: 编辑::
Okay so after looking at your comments i can see why, silly mistake. 好吧,看完您的评论后,我可以明白为什么,这是愚蠢的错误。
A few of the joins are actually joining two tables together instead of joining them to the FROM table (survey). 一些联接实际上是将两个表联接在一起,而不是将它们联接到FROM表(调查)。
This is my new attempt which has failed. 这是我失败的新尝试。
SELECT
`surveyEntries`.`ID` AS EntryID,
`surveyEntries`.`created` AS EntryDate,
`hw_services`.`name` AS Provider,
`surveyQuestions`.`ID` AS QuestionID,
`surveyQuestions`.`label` AS Question,
`survey_meta`.`answer` AS Answer,
`surveyQuestions`.`parentID` AS ParentQuestion
FROM `survey`
JOIN `surveyQuestions`
ON `survey`.`ID` = `surveyQuestions`.`surveyID`
JOIN `surveyEntries`
ON `survey`.`ID` = `surveyEntries`.`surveyID`
JOIN `survey_meta`
ON `surveyEntries`.`surveyID` = `survey_meta`.`entryID`
JOIN `hw_services`
ON `hw_services`.`ID` = `surveyEntries `.`hw_serviceID`
JOIN `surveyQuestions`
ON `surveyQuestions`.`ID` = `survey_meta`.`questionID`
WHERE `survey`.`ID` =2
The error i get is : Not unique table/alias: 'surveyQuestions' 我得到的错误是:不是唯一的表/别名:'surveyQuestions'
Try this: 尝试这个:
SELECT
`surveyEntries`.`ID` AS EntryID,
`surveyEntries`.`created` AS EntryDate,
`hw_services`.`name` AS Provider,
`surveyQuestions`.`ID` AS QuestionID,
`surveyQuestions`.`label` AS Question,
`survey_meta`.`answer` AS Answer,
`surveyQuestions`.`parentID` AS ParentQuestion
FROM `survey`
JOIN `surveyQuestions`
ON `survey`.`ID` = `surveyQuestions`.`surveyID`
JOIN `surveyEntries`
ON `survey`.`ID` = `surveyEntries`.`surveyID`
JOIN `survey_meta`
ON (`surveyEntries`.`ID` = `survey_meta`.`entryID` AND `surveyQuestions`.`ID` = `survey_meta`.`questionID`)
JOIN `hw_services`
ON `surveyEntries`.`hw_serviceID` = `hw_services`.`ID`
WHERE `survey`.`ID` =2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.