我的问题是我使用此查询:
@questions = Question.includes(:answer).joins('LEFT OUTER JOIN answers ON answers.question_id = questions.id AND answers.user_id= 1').select('answers.*, questions.*')
然后它产生这两个mysql查询。问题是第二个查询为每个用户提取答案。
1.Question Load (2.7ms) SELECT answers.*, questions.* FROM "questions" LEFT OUTER JOIN answers ON answers.question_id = questions.id AND answers.user_id= 1
2.Answer Load (0.8ms) SELECT "answers".* FROM "answers" WHERE "answers"."question_id" IN (2, 3, 4, 5, 6, 7, 8, 1)