大型MySQL语句返回数千行冗余信息

时间:2014-11-05 22:10:00

标签: mysql

我广泛地知道,我的问题在于,我正在进行连接的方式导致大量信息一遍又一遍地重复。我不知道的是减少它:

发生的事情是,所有ON子句都是从表单动态生成的;下面的这个陈述应该返回大约1500行;它返回32000。

我不确定提供我的餐桌协会是否有帮助;我有一种感觉,我通常在某处忽略了这一点,但如果需要更多信息,请询问,我会提供!

非常感谢。

SELECT  `Appraisal`.`id` AS `Appraisal ID`,
                    `Appraisal`.`module_id` AS `Module ID`,
                    `Module`.`name` AS `Module Name`,
                    `Course`.`coursecode` AS `Course Code`,
                    `Appraisal`.`excerpt_id` AS `Excerpt ID`,
                    `Appraisal`.`user_id` AS `User ID`,
                    `Appraisal`.`attempt_id` AS `Attempt ID`,
                    `Excerpt`.`id` AS `Excerpt ID`,
                    `Appraisal`.`response` AS `Response (Y=1)`,
                    `Appraisal`.`correct` AS `Response Correct (c=1)`,
                    `Excerpt`.`type` AS `Excerpt Type`,
                    `Appraisal`.`confidence` AS `Confidence (n/100)`,
                    `Appraisal`.`time` AS `Response Time (ms)`,
                    `Institution`.`name` AS `Institution Name`
                     FROM `appraisals` AS `Appraisal`
                    JOIN `modules` AS `Module` ON (`Appraisal`.`module_id` = `Module`.`id`)
                    JOIN `users` AS `User` ON (`Appraisal`.`user_id` = `User`.`id`)
                    JOIN `users_courses` AS `UsersCourse` ON (`UsersCourse`.`user_id` = `User`.`id`)
                    JOIN `courses` AS `Course` ON (`UsersCourse`.`course_id` = `Course`.`id`)
                    JOIN `institutions` AS `Institution` ON (`Course`.`institution_id` = `Institution`.`id`)
                    JOIN `excerpts_modules` as `ExcerptsModule` ON (`Module`.`id` = `ExcerptsModule`.`module_id`)
                    JOIN `excerpts` as `Excerpt` ON (`ExcerptsModule`.`excerpt_id` = `Excerpt`.`id`)
                 WHERE `Institution`.`id` = '1' AND 
`Course`.`educator_id` = '2' AND 
`Course`.`id` IN ('1','2') AND 
`Module`.`id` = '1' AND 
`User`.`id` IN ('1','2','3','10','11','12','13','14','15','16','17','18','19','20','21','22','23','24','25','26','27','28')

此外,如果它提供了一些上下文,ON子句都是从HTML表单提供的,如下所示: enter image description here

更新

这是1 Appraisal个数据的屏幕上限;每个评估,我现在已经解决了,被复制了32次(虽然我确信32是一个欠上下文的数字): enter image description here

值得注意的是,Institution列(它已被切断),Response Time列和其他几个列永远不会改变;它是导致问题的Excerpt Type和其他东西(我认为)的组合。每次评估不能多于ExcerptExcerpt Type

次要更新 当前excerpts表附加了30个excerpts_module,因此至少发生的事情是每个Appraisal加入到每个Excerpt。但是,应该只有一个。我不知道如何制定这个约束并且仍然包含该单个摘录(即没有该联接,加入excerpts的下一行不能成功)。 :/对于那些一直在帮助的人,非常感谢,我肯定会这么说。

最终更新(已解决)

感谢下面的评论和我自己的一点推论,这证明可以通过三个步骤解决:

  1. 添加约束以防止某些连接表中出现重复项;
  2. 我的一个连接设计得很差,这是一个特定于上下文的问题;但
  3. SELECT DISTINCT做了(与我下面的评论相反)在我通过错误加入修复后证明是关键的
  4. 感谢StackOverflow(如果允许的话,将在24小时内回复此问题,或者如果我的一位有帮助的评论者希望我删除此更新并标记正确)。

0 个答案:

没有答案
相关问题