优化mysql联合查询

时间:2019-01-22 20:08:03

标签: mysql query-optimization

我编写了以下查询,以从两个不同的表集中检索数据集。

`SELECT `chapter_id`, `chapter_title`, `actual_content_type`, `area_name`, `status`
FROM ((
      SELECT `css`.`chapter_id`, `css`.`chapter_title`, `a`.`name` AS 
      `area_name`, `css`.`content_status` AS `status`, `css`.`actual_content_type`
      FROM `dm_content_search_summary` `css`
      INNER JOIN `curriculum` `cu` ON css.curriculum_id=cu.id
      INNER JOIN `year` `y` ON css.year_id=y.id
      INNER JOIN `area` `a` ON y.area_id=a.id
      INNER JOIN `country` `cou` ON a.country_id=cou.id
    ) 
      UNION ALL (
       SELECT `cpx`.`category_id` AS `chapter_id`, `cat`.`name` AS 
            `chapter_title`, `a`.`name` AS `area_name`, `pa`.`status` AS 
            `status`, 'PROVE_IT' AS actual_content_type
       FROM `qbnk_published_assessment` `pa`
       INNER JOIN `qbnk_assessment` `ass` ON pa.assessment_id=ass.id
       INNER JOIN `qbnk_assessment_question_xref` `aq` ON 
                ass.id=aq.assessment_id
       INNER JOIN `qbnk_question` `q` ON aq.question_id=q.id
       INNER JOIN `qbnk_answer` `an` ON q.id=an.question_id
       INNER JOIN `qbnk_category_published_assessment_xref` `cpx` ON 
            pa.id=cpx.published_assessment_id
       INNER JOIN `category` `cat` ON cpx.category_id=cat.id
       INNER JOIN `course` `co` ON cat.course_id=co.id
       INNER JOIN `curriculum` `cu` ON co.curriculum_id=cu.id
       INNER JOIN `year` `y` ON cu.year_id = y.id
       INNER JOIN `area` `a` ON y.area_id=a.id
       INNER JOIN `country` `cou` ON a.country_id=cou.id
         GROUP BY `pa`.`assessment_id`
  )
) `0` ORDER BY `chapter_title``

表之间的关系 国家/地区为1:很多,地区/年份为1:很多,地区课程为1:很多,课程课程为1:很多,课程:类别为1:很多,dm_content_search_summary:课程很多:1

qbnk_published_assessment:qbnk_assessment为1:1,qbnk_assessment:qbnk_question有许多:很多,qbnk_question:qbnk_answer是1:很多,qbnk_category_published_assesssment_xref:qbnk_published_asses>

目前,检索数据需要超过17秒的时间。有什么办法可以改善这个查询? 说明命令的结果如下。

enter image description here

====================================更新=========== ======================

有些过滤器需要应用于此查询。这就是为什么我加入一组表的原因。过滤字段为cu.subject_id,cu.year_id,a.id,a.country_id和css.actual_content_type。 而且我必须在ass.name,q.description,an.label,qbnk_category_published_assessment_xref,css.chapter_content和css.content_title字段中应用条件搜索字词。本节的用户界面如下。

enter image description here

在应用搜索词和查询过滤器之后,如下所示。

SELECT `chapter_id`, `chapter_title`, `actual_content_type`, `area_name`, 
`status`
 FROM ((
   SELECT `css`.`chapter_id`, `css`.`chapter_title`, `a`.`name` AS 
   `area_name`, `css`.`content_status` AS `status`, 
   `css`.`actual_content_type`
    FROM `country` `cou`
    INNER JOIN `area` `a` ON cou.id=a.country_id AND a.country_id=4
    INNER JOIN `year` `y` ON a.id=y.area_id AND a.id=19
    INNER JOIN `curriculum` `cu` ON y.id=cu.year_id
    INNER JOIN `dm_content_search_summary` `css` ON cu.id=css.curriculum_id 
    AND cu.year_id=41 AND cu.subject_id=2
    WHERE (((((`css`.`content_title` LIKE '%Summary%') OR 
    (`css`.`chapter_content` LIKE '%Summary%')) AND ((`css`.`content_title` 
    LIKE '%and text%') OR (`css`.`chapter_content` LIKE '%and text%'))) OR 
    (`css`.`content_title` LIKE '%or text%')) OR (`css`.`chapter_content` 
    LIKE '%or text%')) AND ((css.actual_content_type='READ_IT' AND 
    css.chapter_content NOT LIKE '%not text%' AND css.content_title NOT LIKE 
   '%not text%') OR (css.actual_content_type<>'READ_IT' AND 
   css.content_title NOT LIKE '%not text%'))) UNION (
   SELECT `cpx`.`category_id` AS `chapter_id`, `cat`.`name` AS 
   `chapter_title`, `a`.`name` AS `area_name`, `pa`.`status` AS `status`, 
   'PROVE_IT' AS actual_content_type
   FROM `qbnk_published_assessment` `pa`
   INNER JOIN `qbnk_assessment` `ass` ON pa.assessment_id=ass.id
   INNER JOIN `qbnk_assessment_question_xref` `aq` ON 
     ass.id=aq.assessment_id
   INNER JOIN `qbnk_question` `q` ON aq.question_id=q.id
   INNER JOIN `qbnk_answer` `an` ON q.id=an.question_id
   INNER JOIN `qbnk_category_published_assessment_xref` `cpx` ON 
    pa.id=cpx.published_assessment_id
   INNER JOIN `category` `cat` ON cpx.category_id=cat.id
   INNER JOIN `course` `co` ON cat.course_id=co.id
   INNER JOIN `curriculum` `cu` ON co.curriculum_id=cu.id AND cu.year_id=41 
    AND cu.subject_id=2
   INNER JOIN `year` `y` ON cu.year_id = y.id
   INNER JOIN `area` `a` ON y.area_id=a.id AND a.id=19
   INNER JOIN `country` `cou` ON a.country_id=cou.id AND a.country_id=4
   WHERE ((((((((
     (`ass`.`name` LIKE '%Summary%') 
     OR (`q`.`description` LIKE '%Summary%')) 
     OR (`an`.`label` LIKE '%Summary%')) 
     AND (`ass`.`name` LIKE '%and text%') 
     AND (`q`.`description` LIKE '%and text%') 
     AND (`an`.`label` LIKE '%and text%')) 
     OR (`ass`.`name` LIKE '%or text%')) 
     OR (`q`.`description` LIKE '%or text%'))
     OR (`an`.`label` LIKE '%or text%')) 
     OR (`ass`.`name` NOT LIKE '%not text%')) 
     OR (`q`.`description` NOT LIKE '%not text%')) 
    OR (`an`.`label` NOT LIKE '%not text%')
 GROUP BY `pa`.`assessment_id`)) `0`
 ORDER BY `chapter_title`

谢谢

0 个答案:

没有答案