Mysql 执行时间相同。@jela在内部联接中使用p1.shop\u id=86或shops.shop\u id=86是否有任何更改?此外,请发布SHOW WARNINGS的输出,以便优化的查询可用。这通过使用外部查询消除内部查询返回的不相关结果来解决问题。我暂时不

Mysql 执行时间相同。@jela在内部联接中使用p1.shop\u id=86或shops.shop\u id=86是否有任何更改?此外,请发布SHOW WARNINGS的输出,以便优化的查询可用。这通过使用外部查询消除内部查询返回的不相关结果来解决问题。我暂时不,mysql,Mysql,执行时间相同。@jela在内部联接中使用p1.shop\u id=86或shops.shop\u id=86是否有任何更改?此外,请发布SHOW WARNINGS的输出,以便优化的查询可用。这通过使用外部查询消除内部查询返回的不相关结果来解决问题。我暂时不提这个问题,希望有人能建议重新构造只返回所需结果的内部查询,并比我的292秒版本执行得更高效。我更改了内部连接条件并删除了WHERE子句。查询在283秒内执行。我想知道如何强制慢速查询正确使用主键。我用您建议的对连接条件的更改更新了原始问题。我


执行时间相同。@jela在内部联接中使用p1.shop\u id=86或shops.shop\u id=86是否有任何更改?此外,请发布SHOW WARNINGS的输出,以便优化的查询可用。这通过使用外部查询消除内部查询返回的不相关结果来解决问题。我暂时不提这个问题,希望有人能建议重新构造只返回所需结果的内部查询,并比我的292秒版本执行得更高效。我更改了内部连接条件并删除了WHERE子句。查询在283秒内执行。我想知道如何强制慢速查询正确使用主键。我用您建议的对连接条件的更改更新了原始问题。我还观察到,查询执行时间似乎取决于products.shop_id等于指定shops.shop_id的products表中的行数。products表中约有34K行products.shop_id=86,执行时间为292秒。对于products.shop_id=50,大约有28K行,执行时间为210秒。对于products.shop_id=175,大约有2K行,执行时间为2.8秒。我不确定如何修改查询以纠正这种行为。由于同一查询的速度存在巨大差异,我的第一个猜测是您的排序缓冲区太小。MySQL的性能在不够大的时候急剧下降。我已经修补了一段时间了。这个查询比我的查询快得多:原始查询为0.13秒,而我的原始查询为3.4秒(在比我以前使用的数据集稍大的数据集上)。我对sxFav.Product\u ID=sex.Product\u ID子句中的
加入sex感到困惑。添加此子句似乎会检索非预期的结果,因为查询的目的是仅检索
sex.sex=0
的结果,但该子句还将添加
sex.sex=1
的行,只要与
sxFav.Product\u ID=sex.Product\u ID
匹配。我验证了这增加了11个原始查询未返回的额外行。从您的查询中删除sxFav.Product_ID=sex.Product_ID
会使其检索到与原始查询相同数量的结果(没有包含
sex.sex=1
的结果)。我还对sxFav.Product_ID=p.Product_ID上的
连接产品p感到困惑,因为在创建sxFav时已经发生了此连接,所以我似乎可以从sxFav表中的产品表中选择相关列。删除此子句似乎不会更改执行时间。“我可能误解了这两个条款在做什么。”杰拉,继续在聊天室里说吗?我将继续。我已经在@jela创建了聊天室,这里有一个链接,用于我在聊天室中向您提到的复杂sqlvariables问题。。。
SET @num :=0, @current_shop_id := NULL, @current_product_id := NULL;

#this query limits the results of the query within it by row number (so that only 250 products get displayed per store)

SELECT * FROM (

#this query adds row numbers to the query within it

SELECT *, @num := IF( @current_shop_id = shop_id, IF(@current_product_id=product_id,@num,@num+1), 0) AS row_number, @current_shop_id := shop_id AS shop_dummy, @current_product_id := product_id AS product_dummy FROM (

SELECT shop, shops.shop_id AS
shop_id, p1.product_id AS
product_id 
    FROM products p1 LEFT JOIN #this LEFT JOIN gets the favorites count for each product
  (
  SELECT fav3.product_id AS product_id, SUM(CASE 
    WHEN fav3.current = 1 AND fav3.closeted = 1 THEN 1
    WHEN fav3.current = 1 AND fav3.closeted = 0 THEN -1
    ELSE 0
    END) AS favorites_count
    FROM favorites fav3
GROUP BY fav3.product_id 

  ) AS fav4 ON p1.product_id=fav4.product_id
    INNER JOIN sex ON sex.product_id=p1.product_id AND
    sex.sex=0 AND
    sex.date >= SUBDATE(NOW(),INTERVAL 1 DAY) 
    INNER JOIN shops ON shops.shop_id = p1.shop_id
    ORDER BY shop, sex.DATE, product_id
    ) AS testtable

) AS rowed_results WHERE
rowed_results.row_number>=0 AND
rowed_results.row_number<(7)
SET @num :=0, @current_shop_id := NULL, @current_product_id := NULL;

#this query limits the results of the query within it by row number (so that only 250 products get displayed per store)

SELECT * FROM (

#this query adds row numbers to the query within it

SELECT *, @num := IF( @current_shop_id = shop_id, IF(@current_product_id=product_id,@num,@num+1), 0) AS row_number, @current_shop_id := shop_id AS shop_dummy, @current_product_id := product_id AS product_dummy FROM (

SELECT shop, shops.shop_id AS
shop_id, p1.product_id AS
product_id 
    FROM products p1 LEFT JOIN #this LEFT JOIN gets the favorites count for each product
  (
  SELECT fav3.product_id AS product_id, SUM(CASE 
    WHEN fav3.current = 1 AND fav3.closeted = 1 THEN 1
    WHEN fav3.current = 1 AND fav3.closeted = 0 THEN -1
    ELSE 0
    END) AS favorites_count
    FROM favorites fav3
GROUP BY fav3.product_id 

  ) AS fav4 ON p1.product_id=fav4.product_id
    INNER JOIN sex ON sex.product_id=p1.product_id AND
    sex.sex=0 AND
    sex.date >= SUBDATE(NOW(),INTERVAL 1 DAY)
    INNER JOIN shops ON shops.shop_id = p1.shop_id AND
    shops.shop_id=86
    ORDER BY shop, sex.DATE, product_id
    ) AS testtable

) AS rowed_results WHERE
rowed_results.row_number>=0 AND
rowed_results.row_number<(7)
id  select_type table   type    possible_keys   key key_len ref rows    filtered    Extra
1   PRIMARY <derived2>  ALL NULL    NULL    NULL    NULL    1203    100.00  Using where
2   DERIVED <derived3>  ALL NULL    NULL    NULL    NULL    1203    100.00  
3   DERIVED sex ALL product_id_2,product_id NULL    NULL    NULL    526846  75.00   Using where; Using temporary; Using filesort
3   DERIVED p1  eq_ref  PRIMARY,shop_id,shop_id_2,product_id,shop_id_3  PRIMARY 4   mydatabase.sex.product_id   1   100.00  
3   DERIVED <derived4>  ALL NULL    NULL    NULL    NULL    14752   100.00  
3   DERIVED shops   eq_ref  PRIMARY PRIMARY 4   mydatabase.p1.shop_id   1   100.00  
4   DERIVED fav3    ALL NULL    NULL    NULL    NULL    15356   100.00  Using temporary; Using filesort
-----+
| Note | 1003 | select `rowed_results`.`shop` AS `shop`,`rowed_results`.`shop_id` AS `shop_id`,`rowed_results`.`product_id` AS `product_id`,`rowed_results`.`row_number` AS `row_number`,`rowed_results`.`shop_dummy` AS `shop_dummy`,`rowed_results`.`product_dummy` AS `product_dummy` from (select `testtable`.`shop` AS `shop`,`testtable`.`shop_id` AS `shop_id`,`testtable`.`product_id` AS `product_id`,(@num:=if(((@current_shop_id) = `testtable`.`shop_id`),if(((@current_product_id) = `testtable`.`product_id`),(@num),((@num) + 1)),0)) AS `row_number`,(@current_shop_id:=`testtable`.`shop_id`) AS `shop_dummy`,(@current_product_id:=`testtable`.`product_id`) AS `product_dummy` from (select `mydatabase`.`shops`.`shop` AS `shop`,`mydatabase`.`shops`.`shop_id` AS `shop_id`,`mydatabase`.`p1`.`product_id` AS `product_id` from `mydatabase`.`products` `p1` left join (select `mydatabase`.`fav3`.`product_id` AS `product_id`,sum((case when ((`mydatabase`.`fav3`.`current` = 1) and (`mydatabase`.`fav3`.`closeted` = 1)) then 1 when ((`mydatabase`.`fav3`.`current` = 1) and (`mydatabase`.`fav3`.`closeted` = 0)) then -(1) else 0 end)) AS `favorites_count` from `mydatabase`.`favorites` `fav3` group by `mydatabase`.`fav3`.`product_id`) `fav4` on(((`mydatabase`.`p1`.`product_id` = `mydatabase`.`sex`.`product_id`) and (`fav4`.`product_id` = `mydatabase`.`sex`.`product_id`))) join `mydatabase`.`sex` join `mydatabase`.`shops` where ((`mydatabase`.`sex`.`sex` = 0) and (`mydatabase`.`p1`.`product_id` = `mydatabase`.`sex`.`product_id`) and (`mydatabase`.`shops`.`shop_id` = `mydatabase`.`p1`.`shop_id`) and (`mydatabase`.`sex`.`date` >= (now() - interval 1 day))) order by `mydatabase`.`shops`.`shop`,`mydatabase`.`sex`.`date`,`mydatabase`.`p1`.`product_id`) `testtable`) `rowed_results` where ((`rowed_results`.`row_number` >= 0) and (`rowed_results`.`row_number` < 7)) |
+------
id  select_type table   type    possible_keys   key key_len ref rows    filtered    Extra
1   PRIMARY <derived2>  ALL NULL    NULL    NULL    NULL    36  100.00  Using where
2   DERIVED <derived3>  ALL NULL    NULL    NULL    NULL    36  100.00  
3   DERIVED shops   const   PRIMARY PRIMARY 4       1   100.00  Using temporary; Using filesort
3   DERIVED p1  ref PRIMARY,shop_id,shop_id_2,product_id,shop_id_3  shop_id 4       11799   100.00  
3   DERIVED <derived4>  ALL NULL    NULL    NULL    NULL    14752   100.00  
3   DERIVED sex eq_ref  product_id_2,product_id product_id_2    5   mydatabase.p1.product_id    1   100.00  Using where
4   DERIVED fav3    ALL NULL    NULL    NULL    NULL    15356   100.00  Using temporary; Using filesort
----+ 
| Note | 1003 | select `rowed_results`.`shop` AS `shop`,`rowed_results`.`shop_id` AS `shop_id`,`rowed_results`.`product_id` AS `product_id`,`rowed_results`.`row_number` AS `row_number`,`rowed_results`.`shop_dummy` AS `shop_dummy`,`rowed_results`.`product_dummy` AS `product_dummy` from (select `testtable`.`shop` AS `shop`,`testtable`.`shop_id` AS `shop_id`,`testtable`.`product_id` AS `product_id`,(@num:=if(((@current_shop_id) = `testtable`.`shop_id`),if(((@current_product_id) = `testtable`.`product_id`),(@num),((@num) + 1)),0)) AS `row_number`,(@current_shop_id:=`testtable`.`shop_id`) AS `shop_dummy`,(@current_product_id:=`testtable`.`product_id`) AS `product_dummy` from (select 'shop.nordstrom.com' AS `shop`,'86' AS `shop_id`,`mydatabase`.`p1`.`product_id` AS `product_id` from `mydatabase`.`products` `p1` left join (select `mydatabase`.`fav3`.`product_id` AS `product_id`,sum((case when ((`mydatabase`.`fav3`.`current` = 1) and (`mydatabase`.`fav3`.`closeted` = 1)) then 1 when ((`mydatabase`.`fav3`.`current` = 1) and (`mydatabase`.`fav3`.`closeted` = 0)) then -(1) else 0 end)) AS `favorites_count` from `mydatabase`.`favorites` `fav3` group by `mydatabase`.`fav3`.`product_id`) `fav4` on(((`fav4`.`product_id` = `mydatabase`.`p1`.`product_id`) and (`mydatabase`.`sex`.`product_id` = `mydatabase`.`p1`.`product_id`))) join `mydatabase`.`sex` join `mydatabase`.`shops` where ((`mydatabase`.`sex`.`sex` = 0) and (`mydatabase`.`sex`.`product_id` = `mydatabase`.`p1`.`product_id`) and (`mydatabase`.`p1`.`shop_id` = 86) and (`mydatabase`.`sex`.`date` >= (now() - interval 1 day))) order by 'shop.nordstrom.com',`mydatabase`.`sex`.`date`,`mydatabase`.`p1`.`product_id`) `testtable`) `rowed_results` where ((`rowed_results`.`row_number` >= 0) and (`rowed_results`.`row_number` < 7)) | 
+-----
Action  Keyname Type    Unique  Packed  Column  Cardinality Collation   Null    Comment
 Edit    Drop   PRIMARY BTREE   Yes No  shop_id 163 A
SET @num :=0, @current_shop_id := NULL, @current_product_id := NULL;

#this query limits the results of the query within it by row number (so that only 250 products get displayed per store)

SELECT * FROM (

#this query adds row numbers to the query within it

SELECT *, @num := IF( @current_shop_id = shop_id, IF(@current_product_id=product_id,@num,@num+1), 0) AS row_number, @current_shop_id := shop_id AS shop_dummy, @current_product_id := product_id AS product_dummy FROM (

SELECT shop, shops.shop_id AS
shop_id, p1.product_id AS
product_id 
    FROM products p1 LEFT JOIN #this LEFT JOIN gets the favorites count for each product
  (
  SELECT fav3.product_id AS product_id, SUM(CASE 
    WHEN fav3.current = 1 AND fav3.closeted = 1 THEN 1
    WHEN fav3.current = 1 AND fav3.closeted = 0 THEN -1
    ELSE 0
    END) AS favorites_count
    FROM favorites fav3
GROUP BY fav3.product_id 

  ) AS fav4 ON p1.product_id=fav4.product_id
    INNER JOIN sex ON sex.product_id=p1.product_id AND sex.sex=0
    INNER JOIN shops ON shops.shop_id = p1.shop_id
    WHERE sex.date >= SUBDATE(NOW(),INTERVAL 1 DAY) 


    ORDER BY shop, sex.DATE, product_id
    ) AS testtable

) AS rowed_results WHERE
rowed_results.row_number>=0 AND
rowed_results.row_number<(7) AND
rowed_results.shop_dummy=86;
SELECT 
      PreQuery.*,
      @num := IF( @current_shop_id = PreQuery.shop_id, @num +1, 1 ) AS RowPerShop, 
      @current_shop_id := PreQuery.shop_id AS shop_dummy 
   from 
      ( SELECT 
              sxFav.product_id, 
              sxFav.shop_id, 
              sxFav.Favorites_Count
           from 
              ( SELECT 
                      sex.product_id,
                      MAX( p.shop_id ) shop_id,
                      SUM( CASE WHEN F.current = 1 AND F.closeted = 1 THEN 1 
                                WHEN F.current = 1 AND F.closeted = 0 THEN -1 
                                ELSE 0 END ) AS favorites_count 
                   from 
                      sex
                         JOIN products p
                            ON sex.Product_ID = p.Product_ID
                         LEFT JOIN Favorites F 
                            ON sex.product_id = F.product_ID 
                   where 
                          sex.date >= subdate( now(), interval 1 day) 
                      and sex.sex = 0 
                   group by 
                      sex.product_id ) sxFav 

              JOIN sex 
                 ON sxFav.Product_ID = sex.Product_ID

              JOIN products p
                 ON sxFav.Product_ID = p.Product_ID
      order by 
         sxFav.shop_id, 
         sex.date, 
         sxFav.product_id ) PreQuery,

     ( select @num :=0, 
              @current_shop_id := 0 ) as SQLVars 
select * from ( entire query above ) where RowPerShop between 1 and 7
RowPerShop between RowsPerPage*PageYouAreShowing and RowsPerPage*(PageYouAreShowing +1)
....
INNER JOIN shops ON shops.shop_id = p1.shop_id AND shops.shop_id=86
....
...
INNER JOIN shops ON shops.shop_id = p1.shop_id
AND sex.date >= SUBDATE(NOW(),INTERVAL 1 DAY)
...
3   DERIVED p1  eq_ref  PRIMARY,shop_id,shop_id_2,product_id,shop_id_3  PRIMARY 4   mydatabase.sex.product_id   1   100.00  

3   DERIVED p1  ref PRIMARY,shop_id,shop_id_2,product_id,shop_id_3  shop_id 4       11799   100.00