Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 选择takes';s越长,添加的联接越多_Mysql_Join - Fatal编程技术网

Mysql 选择takes';s越长,添加的联接越多

Mysql 选择takes';s越长,添加的联接越多,mysql,join,Mysql,Join,我有一个问题 其中,8个连接中的每一个单独使用0.0007秒,但当我将它们组合在一起时,查询所需的时间呈指数增长,最终以3秒的查询结束 基本上,我有一种灵活的方式来请求具有附加字段的场地。前三个字段看起来不错,但当我达到7时,我注意到这需要很长时间。 我随机化了附加字段的顺序,但结果表明,添加哪个字段并不重要 无其他字段[0.52毫秒] SELECT `v`.`venue_id`, `v`.`venue_nm`, GROUP_CONCAT(DISTINCT vtr.tag_id SEPAR

我有一个问题 其中,8个连接中的每一个单独使用0.0007秒,但当我将它们组合在一起时,查询所需的时间呈指数增长,最终以3秒的查询结束

基本上,我有一种灵活的方式来请求具有附加字段的场地。前三个字段看起来不错,但当我达到7时,我注意到这需要很长时间。 我随机化了附加字段的顺序,但结果表明,添加哪个字段并不重要

无其他字段[0.52毫秒]

SELECT 
  `v`.`venue_id`, `v`.`venue_nm`, GROUP_CONCAT(DISTINCT vtr.tag_id SEPARATOR ",") tags 
FROM 
   `listing_venue` AS `v` 
INNER JOIN 
   `listing_venue_tag_rel` AS `vtr` ON vtr.venue_id = v.venue_id WHERE  (v.venue_id = 79) AND (v.published = 1) 
GROUP BY 
   `venue_id`
1个附加字段[0.72毫秒]

SELECT 
   `v`.`venue_id`, `v`.`venue_nm`, GROUP_CONCAT(DISTINCT vtr.tag_id SEPARATOR ",") tags, `addressvalue`.`value` AS `address`
FROM 
   `listing_venue` AS `v` 
INNER JOIN 
   `listing_venue_tag_rel` AS `vtr` ON vtr.venue_id = v.venue_id 
INNER JOIN 
   `listing_field` AS `addressfield` 
LEFT JOIN
   `listing_venue_field_rel` AS `addressvalue` ON addressvalue.venue_id = v.venue_id AND addressvalue.field_id = addressfield.field_id 
WHERE 
   (v.venue_id = 79) AND (v.published = 1) AND (addressfield.field_nm = 'address') 
GROUP BY 
   `venue_id`
2个附加字段[1.42毫秒]

SELECT `v`.`venue_id`, `v`.`venue_nm`, GROUP_CONCAT(DISTINCT vtr.tag_id SEPARATOR ",") tags, `addressvalue`.`value` AS `address`, `address_zhvalue`.`value` AS `address_zh` FROM `listing_venue` AS `v` INNER JOIN `listing_venue_tag_rel` AS `vtr` ON vtr.venue_id = v.venue_id INNER JOIN `listing_field` AS `addressfield` LEFT JOIN `listing_venue_field_rel` AS `addressvalue` ON addressvalue.venue_id = v.venue_id AND addressvalue.field_id = addressfield.field_id INNER JOIN `listing_field` AS `address_zhfield` LEFT JOIN `listing_venue_field_rel` AS `address_zhvalue` ON address_zhvalue.venue_id = v.venue_id AND address_zhvalue.field_id = address_zhfield.field_id WHERE (v.venue_id = 79) AND (v.published = 1) AND (addressfield.field_nm = 'address') AND (address_zhfield.field_nm = 'address_zh') GROUP BY `venue_id`
3->1.13毫秒

SELECT `v`.`venue_id`, `v`.`venue_nm`, GROUP_CONCAT(DISTINCT vtr.tag_id SEPARATOR ",") tags, `addressvalue`.`value` AS `address`, `address_zhvalue`.`value` AS `address_zh`, `address_nearvalue`.`value` AS `address_near` FROM `listing_venue` AS `v` INNER JOIN `listing_venue_tag_rel` AS `vtr` ON vtr.venue_id = v.venue_id INNER JOIN `listing_field` AS `addressfield` LEFT JOIN `listing_venue_field_rel` AS `addressvalue` ON addressvalue.venue_id = v.venue_id AND addressvalue.field_id = addressfield.field_id INNER JOIN `listing_field` AS `address_zhfield` LEFT JOIN `listing_venue_field_rel` AS `address_zhvalue` ON address_zhvalue.venue_id = v.venue_id AND address_zhvalue.field_id = address_zhfield.field_id INNER JOIN `listing_field` AS `address_nearfield` LEFT JOIN `listing_venue_field_rel` AS `address_nearvalue` ON address_nearvalue.venue_id = v.venue_id AND address_nearvalue.field_id = address_nearfield.field_id WHERE (v.venue_id = 79) AND (v.published = 1) AND (addressfield.field_nm = 'address') AND (address_zhfield.field_nm = 'address_zh') AND (address_nearfield.field_nm = 'address_near') GROUP BY `venue_id`
4->1.94毫秒

SELECT `v`.`venue_id`, `v`.`venue_nm`, GROUP_CONCAT(DISTINCT vtr.tag_id SEPARATOR ",") tags, `addressvalue`.`value` AS `address`, `address_zhvalue`.`value` AS `address_zh`, `address_nearvalue`.`value` AS `address_near`, `phone_numbervalue`.`value` AS `phone_number` FROM `listing_venue` AS `v` INNER JOIN `listing_venue_tag_rel` AS `vtr` ON vtr.venue_id = v.venue_id INNER JOIN `listing_field` AS `addressfield` LEFT JOIN `listing_venue_field_rel` AS `addressvalue` ON addressvalue.venue_id = v.venue_id AND addressvalue.field_id = addressfield.field_id INNER JOIN `listing_field` AS `address_zhfield` LEFT JOIN `listing_venue_field_rel` AS `address_zhvalue` ON address_zhvalue.venue_id = v.venue_id AND address_zhvalue.field_id = address_zhfield.field_id INNER JOIN `listing_field` AS `address_nearfield` LEFT JOIN `listing_venue_field_rel` AS `address_nearvalue` ON address_nearvalue.venue_id = v.venue_id AND address_nearvalue.field_id = address_nearfield.field_id INNER JOIN `listing_field` AS `phone_numberfield` LEFT JOIN `listing_venue_field_rel` AS `phone_numbervalue` ON phone_numbervalue.venue_id = v.venue_id AND phone_numbervalue.field_id = phone_numberfield.field_id WHERE (v.venue_id = 79) AND (v.published = 1) AND (addressfield.field_nm = 'address') AND (address_zhfield.field_nm = 'address_zh') AND (address_nearfield.field_nm = 'address_near') AND (phone_numberfield.field_nm = 'phone_number') GROUP BY `venue_id`
5->9.7毫秒

SELECT `v`.`venue_id`, `v`.`venue_nm`, GROUP_CONCAT(DISTINCT vtr.tag_id SEPARATOR ",") tags, `addressvalue`.`value` AS `address`, `address_zhvalue`.`value` AS `address_zh`, `address_nearvalue`.`value` AS `address_near`, `phone_numbervalue`.`value` AS `phone_number`, `websitevalue`.`value` AS `website` FROM `listing_venue` AS `v` INNER JOIN `listing_venue_tag_rel` AS `vtr` ON vtr.venue_id = v.venue_id INNER JOIN `listing_field` AS `addressfield` LEFT JOIN `listing_venue_field_rel` AS `addressvalue` ON addressvalue.venue_id = v.venue_id AND addressvalue.field_id = addressfield.field_id INNER JOIN `listing_field` AS `address_zhfield` LEFT JOIN `listing_venue_field_rel` AS `address_zhvalue` ON address_zhvalue.venue_id = v.venue_id AND address_zhvalue.field_id = address_zhfield.field_id INNER JOIN `listing_field` AS `address_nearfield` LEFT JOIN `listing_venue_field_rel` AS `address_nearvalue` ON address_nearvalue.venue_id = v.venue_id AND address_nearvalue.field_id = address_nearfield.field_id INNER JOIN `listing_field` AS `phone_numberfield` LEFT JOIN `listing_venue_field_rel` AS `phone_numbervalue` ON phone_numbervalue.venue_id = v.venue_id AND phone_numbervalue.field_id = phone_numberfield.field_id INNER JOIN `listing_field` AS `websitefield` LEFT JOIN `listing_venue_field_rel` AS `websitevalue` ON websitevalue.venue_id = v.venue_id AND websitevalue.field_id = websitefield.field_id WHERE (v.venue_id = 79) AND (v.published = 1) AND (addressfield.field_nm = 'address') AND (address_zhfield.field_nm = 'address_zh') AND (address_nearfield.field_nm = 'address_near') AND (phone_numberfield.field_nm = 'phone_number') AND (websitefield.field_nm = 'website') GROUP BY `venue_id`
6->230.52毫秒

SELECT `v`.`venue_id`, `v`.`venue_nm`, GROUP_CONCAT(DISTINCT vtr.tag_id SEPARATOR ",") tags, `addressvalue`.`value` AS `address`, `address_zhvalue`.`value` AS `address_zh`, `address_nearvalue`.`value` AS `address_near`, `phone_numbervalue`.`value` AS `phone_number`, `websitevalue`.`value` AS `website`, `price_maxvalue`.`value` AS `price_max` FROM `listing_venue` AS `v` INNER JOIN `listing_venue_tag_rel` AS `vtr` ON vtr.venue_id = v.venue_id INNER JOIN `listing_field` AS `addressfield` LEFT JOIN `listing_venue_field_rel` AS `addressvalue` ON addressvalue.venue_id = v.venue_id AND addressvalue.field_id = addressfield.field_id INNER JOIN `listing_field` AS `address_zhfield` LEFT JOIN `listing_venue_field_rel` AS `address_zhvalue` ON address_zhvalue.venue_id = v.venue_id AND address_zhvalue.field_id = address_zhfield.field_id INNER JOIN `listing_field` AS `address_nearfield` LEFT JOIN `listing_venue_field_rel` AS `address_nearvalue` ON address_nearvalue.venue_id = v.venue_id AND address_nearvalue.field_id = address_nearfield.field_id INNER JOIN `listing_field` AS `phone_numberfield` LEFT JOIN `listing_venue_field_rel` AS `phone_numbervalue` ON phone_numbervalue.venue_id = v.venue_id AND phone_numbervalue.field_id = phone_numberfield.field_id INNER JOIN `listing_field` AS `websitefield` LEFT JOIN `listing_venue_field_rel` AS `websitevalue` ON websitevalue.venue_id = v.venue_id AND websitevalue.field_id = websitefield.field_id INNER JOIN `listing_field` AS `price_maxfield` LEFT JOIN `listing_venue_field_rel` AS `price_maxvalue` ON price_maxvalue.venue_id = v.venue_id AND price_maxvalue.field_id = price_maxfield.field_id WHERE (v.venue_id = 79) AND (v.published = 1) AND (addressfield.field_nm = 'address') AND (address_zhfield.field_nm = 'address_zh') AND (address_nearfield.field_nm = 'address_near') AND (phone_numberfield.field_nm = 'phone_number') AND (websitefield.field_nm = 'website') AND (price_maxfield.field_nm = 'price_max') GROUP BY `venue_id`
7->3375.29毫秒

SELECT 
    `v`.`venue_id`, `v`.`venue_nm`, GROUP_CONCAT(DISTINCT vtr.tag_id SEPARATOR ",") tags, `addressvalue`.`value` AS `address`, `address_zhvalue`.`value` AS `address_zh`, `address_nearvalue`.`value` AS `address_near`, `phone_numbervalue`.`value` AS `phone_number`, `websitevalue`.`value` AS `website`, `price_maxvalue`.`value` AS `price_max`, `price_minvalue`.`value` AS `price_min` 
FROM 
    `listing_venue` AS `v` 
INNER JOIN 
    `listing_venue_tag_rel` AS `vtr` 
    ON 
        vtr.venue_id = v.venue_id 
INNER JOIN
    `listing_field` AS `addressfield` 
LEFT JOIN 
    `listing_venue_field_rel` AS `addressvalue` 
    ON 
        addressvalue.venue_id = v.venue_id AND addressvalue.field_id = addressfield.field_id 
INNER JOIN 
    `listing_field` AS `address_zhfield` 
LEFT JOIN 
    `listing_venue_field_rel` AS `address_zhvalue` 
    ON 
        address_zhvalue.venue_id = v.venue_id AND address_zhvalue.field_id = address_zhfield.field_id 
INNER JOIN 
    `listing_field` AS `address_nearfield` 
LEFT JOIN 
    `listing_venue_field_rel` AS `address_nearvalue` 
    ON 
    address_nearvalue.venue_id = v.venue_id AND address_nearvalue.field_id = address_nearfield.field_id 
INNER JOIN 
    `listing_field` AS `phone_numberfield` 
LEFT JOIN 
    `listing_venue_field_rel` AS `phone_numbervalue` 
    ON 
        phone_numbervalue.venue_id = v.venue_id AND phone_numbervalue.field_id = phone_numberfield.field_id 
INNER JOIN 
    `listing_field` AS `websitefield` 
LEFT JOIN 
    `listing_venue_field_rel` AS `websitevalue` 
    ON 
        websitevalue.venue_id = v.venue_id AND websitevalue.field_id = websitefield.field_id 
INNER JOIN 
    `listing_field` AS `price_maxfield` 
LEFT JOIN 
    `listing_venue_field_rel` AS `price_maxvalue` 
    ON 
        price_maxvalue.venue_id = v.venue_id AND price_maxvalue.field_id = price_maxfield.field_id 
INNER JOIN 
    `listing_field` AS `price_minfield` 
LEFT JOIN 
    `listing_venue_field_rel` AS `price_minvalue`
    ON 
        price_minvalue.venue_id = v.venue_id AND price_minvalue.field_id = price_minfield.field_id 
WHERE 
    (v.venue_id = 79) 
AND (v.published = 1) 
AND (addressfield.field_nm = 'address') 
AND (address_zhfield.field_nm = 'address_zh') 
AND (address_nearfield.field_nm = 'address_near') 
AND (phone_numberfield.field_nm = 'phone_number') 
AND (websitefield.field_nm = 'website') 
AND (price_maxfield.field_nm = 'price_max') 
AND (price_minfield.field_nm = 'price_min') GROUP BY `venue_id`
显然,我可以将查询拆分为7个,但我不明白为什么会出现这种奇怪的时间增长

另外,我不确定我使用的联接是否正确。场地可能是空的,这就是为什么我要加入场地的原因

以下是表格结构

listing_field:
field_id | field_nm

listing_venue_field_rel
venue_id | field_id| value

listing_venue
venue_id | venue_nm
说明

id  select_type     table               type    possible_keys                                                                                       key                 key_len     ref     rows    Extra
1   SIMPLE          v                   const   PRIMARY,item_id_UNIQUE                                                                              PRIMARY             4           const   1    
1   SIMPLE          addressfield        ALL     NULL                                                                                                NULL                NULL        NULL    7       Using where
1   SIMPLE          addressvalue        ref     field_value_venue,fk_mnc_listing_venue_field_rel_venue_id,fk_mnc_listing_venue_field_rel_field_id   field_value_venue   4           const   6       Using index
1   SIMPLE          address_zhfield     ALL     NULL                                                                                                NULL                NULL        NULL    7       Using where; Using join buffer
1   SIMPLE          address_zhvalue     ref     field_value_venue,fk_mnc_listing_venue_field_rel_venue_id,fk_mnc_listing_venue_field_rel_field_id   field_value_venue   4           const   6       Using index
1   SIMPLE          address_nearfield   ALL     NULL                                                                                                NULL                NULL        NULL    7       Using where; Using join buffer
1   SIMPLE          address_nearvalue   ref     field_value_venue,fk_mnc_listing_venue_field_rel_venue_id,fk_mnc_listing_venue_field_rel_field_id   field_value_venue   4           const   6       Using index
1   SIMPLE          phone_numberfield   ALL     NULL                                                                                                NULL                NULL        NULL    7       Using where; Using join buffer
1   SIMPLE          phone_numbervalue   ref     field_value_venue,fk_mnc_listing_venue_field_rel_venue_id,fk_mnc_listing_venue_field_rel_field_id   field_value_venue   4           const   6       Using index
1   SIMPLE          websitefield        ALL                                                                                                         NULL                NULL        NULL    7       Using where; Using join buffer
1   SIMPLE          websitevalue        ref     field_value_venue,fk_mnc_listing_venue_field_rel_venue_id,fk_mnc_listing_venue_field_rel_field_id   field_value_venue   4           const   6       Using index
1   SIMPLE          price_maxfield      ALL                                                                                                         NULL                NULL        NULL    7       Using where; Using join buffer
1   SIMPLE          price_maxvalue      ref     field_value_venue,fk_mnc_listing_venue_field_rel_venue_id,fk_mnc_listing_venue_field_rel_field_id   field_value_venue   4           const   6       Using index
1   SIMPLE          price_minfield      ALL                                                                                                         NULL                NULL        NULL    7       Using where; Using join buffer
1   SIMPLE          price_minvalue      ref     field_value_venue,fk_mnc_listing_venue_field_rel_venue_id,fk_mnc_listing_venue_field_rel_field_id   field_value_venue   4           const   6       Using index
1   SIMPLE          vtr                 ref     item_tag,fk_venue_id                                                                                fk_venue_id         4           const   11      Using index
编辑I“索引唯一”字段,将速度降低回7毫秒

id  select_type table               type    possible_keys           key                 key_len     ref     rows    Extra
1   SIMPLE      v                   const   PRIMARY,item_id_UNIQUE  PRIMARY             4           const   1    
1   SIMPLE      addressfield        const   field_nm,field_nm_2     field_nm            137         const   1   Using index
1   SIMPLE      address_zhfield     const   field_nm,field_nm_2     field_nm            137         const   1   Using index
1   SIMPLE      address_nearfield   const   field_nm,field_nm_2     field_nm            137         const   1   Using index
1   SIMPLE      phone_numberfield   const   field_nm,field_nm_2     field_nm            137         const   1   Using index
1   SIMPLE      websitefield        const   field_nm,field_nm_2     field_nm            137         const   1   Using index
1   SIMPLE      price_maxfield      const   field_nm,field_nm_2     field_nm            137         const   1   Using index
1   SIMPLE      addressvalue        ref     field_value_venue,...   field_value_venue   4           const   7   Using index
1   SIMPLE      address_zhvalue     ref     field_value_venue,...   field_value_venue   4           const   7   Using index
1   SIMPLE      address_nearvalue   ref     field_value_venue,...   field_value_venue   4           const   7   Using index
1   SIMPLE      phone_numbervalue   ref     field_value_venue,...   field_value_venue   4           const   7   Using index
1   SIMPLE      websitevalue        ref     field_value_venue,...   field_value_venue   4           const   7   Using index
1   SIMPLE      price_maxvalue      ref     field_value_venue,...   field_value_venue   4           const   7   Using index
1   SIMPLE      vtr                 ref     item_tag,fk_venue_id    fk_venue_id         4           const   17  Using index

不过,我确信查询可以再优化一些

我认为您可能在这里进行交叉连接

内部联接
列表\字段
地址\字段
左联接


您没有ON子句,因此其他所有内容都是笛卡尔积,这可能会导致指数级的速度减慢。

可能会正确设置查询格式。他们看起来很可怕…+1@astander!Moak,你能把你的查询格式化成几行吗?@astander@lagrandmile我重新格式化了前两个查询,之后只添加了一个额外的查询,但列表字段与场馆表无关,那么我如何才能设置ON呢?事实上,所有内部连接都缺少连接条件,因此表中的每个记录都将连接到所有其他记录,您肯定会看到速度变慢。使用EXPLAIN Select查看发生了什么(将结果张贴在此处)。下面是连接语法(使用连接条件,而不是实际启用)OK。看看解释,如果你取“rows”列,然后将这些值相乘,我认为可能会有250000000000行(1*7*6*7*6*7*6*7*6*7*6*7*7*6*11)需要分析。请看这里,并注意连接类型“ALL”的含义(这很糟糕)。如何将结果减少到更少?如果不了解您要实现的目标,也没有关于数据库的更多信息,我真的不能说,对不起。