0

我有这个查询,加载时间太长(大约 40 秒)。

SELECT
  `auxiliary_table_2`.`entry_id`,
  `auxiliary_table_2`. `name`,
  COUNT(`auxiliary_table_2`.`entry_id`) AS `auxiliary_table_2_count`
FROM (SELECT
    `data_table`.*
      FROM `data_table`
    LEFT JOIN `exp_channels`
      ON `exp_channels`.`channel_id` = `data_table`.`channel_id`
    LEFT JOIN `exp_channel_titles`
      ON `exp_channel_titles`.`entry_id` = `data_table`.`entry_id`
      WHERE `exp_channels`.`channel_name` = 'main_table'
      AND `exp_channel_titles`.`status` = 'open'
      GROUP BY `data_table`.`entry_id`) AS `main_table`
  JOIN `mapping_table` AS `mapping_table_1`
    ON `mapping_table_1`.`rel_id` = `main_table`.`field_id_9`
  JOIN (SELECT
      `data_table`.`entry_id`,
      `data_table`.`field_id_8`       AS `name`
    FROM `data_table`
      LEFT JOIN `exp_channels`
        ON `exp_channels`.`channel_id` = `data_table`.`channel_id`
    WHERE `exp_channels`.`channel_name` = 'auxiliary_table_channel_name_1'
    GROUP BY `data_table`.`entry_id`) AS `auxiliary_table_1`
    ON `auxiliary_table_1`.`entry_id` = `mapping_table_1`.`rel_child_id`
  JOIN `mapping_table` AS `mapping_table_2`
    ON `mapping_table_2`.`rel_id` = `main_table`.`field_id_39`
  JOIN (SELECT
      `data_table`.`entry_id`,
      `data_table`.`field_id_38`      AS `name`
    FROM `data_table`
      LEFT JOIN `exp_channels`
        ON `exp_channels`.`channel_id` = `data_table`.`channel_id`
    WHERE `exp_channels`.`channel_name` = 'auxiliary_table_channel_name_2'
    GROUP BY `data_table`.`entry_id`) AS `auxiliary_table_2`
    ON `auxiliary_table_2`.`entry_id` = `mapping_table_2`.`rel_child_id`
  JOIN `mapping_table` AS `mapping_table_3`
    ON `mapping_table_3`.`rel_id` = `main_table`.`field_id_5`
  JOIN (SELECT
      `data_table`.`entry_id`,
      `data_table`.`field_id_1`       AS `name`
    FROM `data_table`
      LEFT JOIN `exp_channels`
        ON `exp_channels`.`channel_id` = `data_table`.`channel_id`
    WHERE `exp_channels`.`channel_name` = 'auxiliary_table_channel_name_3'
    GROUP BY `data_table`.`entry_id`) AS `auxiliary_table_3`
    ON `auxiliary_table_3`.`entry_id` = `mapping_table_3`.`rel_child_id`
  JOIN `mapping_table` AS `mapping_table_4`
    ON `mapping_table_4`.`rel_id` = `main_table`.`field_id_16`
  JOIN (SELECT
      `data_table`.`entry_id`,
      `data_table`.`field_id_18`      AS `name`
    FROM `data_table`
      LEFT JOIN `exp_channels`
        ON `exp_channels`.`channel_id` = `data_table`.`channel_id`
    WHERE `exp_channels`.`channel_name` = 'auxiliary_table_channel_name_4'
    GROUP BY `data_table`.`entry_id`) AS `auxiliary_table_4`
    ON `auxiliary_table_4`.`entry_id` = `mapping_table_4`.`rel_child_id`
  JOIN `mapping_table` AS `mapping_table_5`
    ON `mapping_table_5`.`rel_id` = `main_table`.`field_id_49`
  JOIN (SELECT
      `data_table`.`entry_id`,
      `data_table`.`field_id_48`      AS `name`
    FROM `data_table`
      LEFT JOIN `exp_channels`
        ON `exp_channels`.`channel_id` = `data_table`.`channel_id`
    WHERE `exp_channels`.`channel_name` = 'auxiliary_table_channel_name_5'
    GROUP BY `data_table`.`entry_id`) AS `auxiliary_table_5`
    ON `auxiliary_table_5`.`entry_id` = `mapping_table_5`.`rel_child_id`
GROUP BY `auxiliary_table_2`.`entry_id`

它的要点是data_table在通过映射表映射的辅助表中具有辅助数据。main_table只是一个过滤的数据表。

这里有什么可能导致它运行缓慢吗?

data_table只有少量的行(大约 200+)所以我认为这是一个查询问题。

这是解释结果

id  select_type     table           type    possible_keys           key             key_len     ref                                     rows        Extra 
1   PRIMARY     <derived4>          ALL     NULL                    NULL            NULL        NULL                                    3           Using temporary; Using filesort
1   PRIMARY     <derived6>          ALL     NULL                    NULL            NULL        NULL                                    5           Using join buffer
1   PRIMARY     <derived7>          ALL     NULL                    NULL            NULL        NULL                                    6           Using join buffer
1   PRIMARY     <derived3>          ALL     NULL                    NULL            NULL        NULL                                    21          Using join buffer
1   PRIMARY     <derived2>          ALL     NULL                    NULL            NULL        NULL                                    20          Using join buffer
1   PRIMARY     <derived5>          ALL     NULL                    NULL            NULL        NULL                                    143         Using join buffer
1   PRIMARY     auxiliary_table_1   eq_ref  PRIMARY,rel_child_id    PRIMARY         4           main_table.field_id_9                   1           Using where
1   PRIMARY     auxiliary_table_2   eq_ref  PRIMARY,rel_child_id    PRIMARY         4           main_table.field_id_39                  1           Using where
1   PRIMARY     auxiliary_table_3   eq_ref  PRIMARY,rel_child_id    PRIMARY         4           main_table.field_id_5                   1           Using where
1   PRIMARY     auxiliary_table_4   eq_ref  PRIMARY,rel_child_id    PRIMARY         4           main_table.field_id_16                  1           Using where
1   PRIMARY     auxiliary_table_5   eq_ref  PRIMARY,rel_child_id    PRIMARY         4           main_table.field_id_49                  1           Using where
7   DERIVED     exp_channels        ref     PRIMARY,channel_name    channel_name    122                                                 1           Using where; Using temporary; Using filesort
7   DERIVED     exp_channel_data    ref     channel_id              channel_id      4           devthree_ee.exp_channels.channel_id     17  
6   DERIVED     exp_channels        ref     PRIMARY,channel_name    channel_name    122                                                 1           Using where; Using temporary; Using filesort
6   DERIVED     exp_channel_data    ref     channel_id              channel_id      4           devthree_ee.exp_channels.channel_id     17  
5   DERIVED     exp_channels        ref     PRIMARY,channel_name    channel_name    122                                                 1           Using where; Using temporary; Using filesort
5   DERIVED     exp_channel_data    ref     channel_id              channel_id      4           devthree_ee.exp_channels.channel_id     17  
4   DERIVED     exp_channels        ref     PRIMARY,channel_name    channel_name    122                                                 1           Using where; Using temporary; Using filesort
4   DERIVED     exp_channel_data    ref     channel_id              channel_id      4           devthree_ee.exp_channels.channel_id     17  
3   DERIVED     exp_channels        ref     PRIMARY,channel_name    channel_name    122                                                 1           Using where; Using temporary; Using filesort
3   DERIVED     exp_channel_data    ref     channel_id              channel_id      4           devthree_ee.exp_channels.channel_id     17  
2   DERIVED     exp_channels        ref     PRIMARY,channel_name    channel_name    122                                                 1           Using where; Using temporary; Using filesort
2   DERIVED     exp_channel_data    ref     PRIMARY,channel_id      channel_id      4           devthree_ee.exp_channels.channel_id     17  
2   DERIVED     exp_channel_titles  eq_ref  PRIMARY,status          PRIMARY         4           devthree_ee.exp_channel_data.entry_id   1           Using where

MYSQL 版本:

我正在使用 MySQL 5.1.66-cll

表格:

数据表

#   Name        Type        Collation       Attributes      Null    Default Extra
1   entry_id    int(10)                     UNSIGNED        No      None        
2   site_id     int(4)                      UNSIGNED        No      1       
3   channel_id  int(4)                      UNSIGNED        No      None        
4   field_id_1  text        utf8_general_ci                 Yes     NULL        
5   field_ft_1  tinytext    utf8_general_ci                 Yes     NULL        
6   field_id_2  text        utf8_general_ci                 Yes     NULL        
7   field_ft_2  tinytext    utf8_general_ci                 Yes     NULL        
8   field_id_3  text        utf8_general_ci                 Yes     NULL        
9   field_ft_3  tinytext    utf8_general_ci                 Yes     NULL        
10  field_id_4  int(11)                                     Yes     0       
11  field_ft_4  tinytext    utf8_general_ci                 Yes     NULL        
12  field_id_5  int(10)                                     Yes     0       
13  field_ft_5  tinytext    utf8_general_ci                 Yes     NULL        
14  field_id_6  text        utf8_general_ci                 Yes     NULL
.....

映射表

#   Name                Type        Collation       Attributes      Null    Default     Extra   
1   rel_id              int(6)                      UNSIGNED        No      None        AUTO_INCREMENT
2   rel_parent_id       int(10)                                     No      0           
3   rel_child_id        int(10)                                     No      0           
4   rel_type            varchar(12) utf8_general_ci                 No      None        
5   rel_data            mediumtext  utf8_general_ci                 No      None        
6   reverse_rel_data    mediumtext  utf8_general_ci                 No      None        

PS 出于匿名目的,我已将表名替换为通用名称。

4

0 回答 0