Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.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查询的效率?_Mysql - Fatal编程技术网

如何提高MySQL查询的效率?

如何提高MySQL查询的效率?,mysql,Mysql,以下查询将在0.12秒内执行 SET @num :=0, @current_shop_id := NULL, @current_product_id := NULL; SELECT * FROM ( SELECT fav1.user_id, @num := IF(@current_shop_id=shops.shop_id, IF(@current_product_id=products.product_id,@num,@num+1),0) AS row_number, @current_s

以下查询将在0.12秒内执行

SET @num :=0, @current_shop_id := NULL, @current_product_id := NULL;

SELECT * FROM (

SELECT fav1.user_id, @num := IF(@current_shop_id=shops.shop_id, IF(@current_product_id=products.product_id,@num,@num+1),0) AS row_number, @current_shop_id := shops.shop_id AS shop_dummy, @current_product_id := products.product_id AS product_dummy

    FROM favorites fav1 INNER JOIN
    products ON
    fav1.product_id=products.product_id AND 
    fav1.current=1 AND
    fav1.closeted=1 AND 
    fav1.user_id=30  INNER JOIN
    shops ON
    shops.shop_id = products.shop_id 

    GROUP BY fav1.product_id
    ORDER BY shops.shop ASC

    ) AS rowed_results WHERE rowed_results.row_number>=0 AND rowed_results.row_number<(0+5)
但是,将它们按如下方式组合会产生一个在11秒内执行的查询

SET @num :=0, @current_shop_id := NULL, @current_product_id := NULL;

SELECT * FROM (

SELECT fav1.user_id, @num := IF(@current_shop_id=shops.shop_id, IF(@current_product_id=products.product_id,@num,@num+1),0) AS row_number, @current_shop_id := shops.shop_id AS shop_dummy, @current_product_id := products.product_id AS product_dummy

    FROM

    #limit results to products favorited by the user whose closet is being shown
    favorites fav1 INNER JOIN

    products ON
    fav1.product_id=products.product_id AND 
    fav1.current=1 AND
    fav1.closeted=1 AND 
    fav1.user_id=30  INNER JOIN

    shops ON
    shops.shop_id = products.shop_id 

    LEFT JOIN

    # this LEFT JOIN associates favorites_count table (adds up the scores for all the products in the favorites table)
    (
    SELECT fav5.product_id AS product_id, SUM(CASE 
    WHEN fav5.current = 1 AND fav5.closeted = 1 THEN 1
    WHEN fav5.current = 1 AND fav5.closeted = 0 THEN -1
    ELSE 0
    END) AS favorites_count
    FROM favorites fav5
    GROUP BY fav5.product_id 

    ) AS fav6 ON products.product_id=fav6.product_id

    GROUP BY fav1.product_id
    ORDER BY shops.shop ASC

    ) AS rowed_results WHERE rowed_results.row_number>=0 AND rowed_results.row_number<(0+5)
显示第二个查询的警告是

+----+-------------+-------+------+---------------+------+---------+------+-------+----------+---------------------------------+
| id | select_type | table | type | possible_keys | key  | key_len | ref  | rows  | filtered | Extra                           |
+----+-------------+-------+------+---------------+------+---------+------+-------+----------+---------------------------------+
|  1 | SIMPLE      | fav5  | ALL  | NULL          | NULL | NULL    | NULL | 16377 |   100.00 | Using temporary; Using filesort |
+----+-------------+-------+------+---------------+------+---------+------+-------+----------+---------------------------------+
1 row in set, 1 warning (0.00 sec)
+-------+------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Level | Code | Message                                                                                                                                                                                                                                                                                                                                                                            |
+-------+------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Note  | 1003 | select `my_database`.`fav5`.`product_id` AS `product_id`,sum((case when ((`my_database`.`fav5`.`current` = 1) and (`my_database`.`fav5`.`closeted` = 1)) then 1 when ((`my_database`.`fav5`.`current` = 1) and (`my_database`.`fav5`.`closeted` = 0)) then -(1) else 0 end)) AS `favorites_count` from `my_database`.`favorites` `fav5` group by `my_database`.`fav5`.`product_id` |
+-------+------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
最终查询的解释扩展为

+----+-------------+------------+--------+------------------------------------------------+-----------+---------+------------------------------+-------+----------+----------------------------------------------+
| id | select_type | table      | type   | possible_keys                                  | key       | key_len | ref                          | rows  | filtered | Extra                                        |
+----+-------------+------------+--------+------------------------------------------------+-----------+---------+------------------------------+-------+----------+----------------------------------------------+
|  1 | PRIMARY     | <derived2> | ALL    | NULL                                           | NULL      | NULL    | NULL                         |  8846 |   100.00 | Using where                                  |
|  2 | DERIVED     | fav1       | ref    | user_id,user_id_2,product_id,closeted          | user_id_2 | 4       |                              |  9624 |   100.00 | Using where; Using temporary; Using filesort |
|  2 | DERIVED     | products   | eq_ref | PRIMARY,shop_id,shop_id_2,product_id,shop_id_3 | PRIMARY   | 4       | my_database.fav1.product_id  |     1 |   100.00 |                                              |
|  2 | DERIVED     | shops      | eq_ref | PRIMARY                                        | PRIMARY   | 4       | my_database.products.shop_id |     1 |   100.00 |                                              |
|  2 | DERIVED     | <derived3> | ALL    | NULL                                           | NULL      | NULL    | NULL                         | 15764 |   100.00 |                                              |
|  3 | DERIVED     | fav5       | ALL    | NULL                                           | NULL      | NULL    | NULL                         | 16377 |   100.00 | Using temporary; Using filesort              |
+----+-------------+------------+--------+------------------------------------------------+-----------+---------+------------------------------+-------+----------+----------------------------------------------+
6 rows in set, 1 warning (11.50 sec)
+-------+------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Note  | 1003 | select `rowed_results`.`user_id` AS `user_id`,`rowed_results`.`row_number` AS `row_number`,`rowed_results`.`shop_dummy` AS `shop_dummy`,`rowed_results`.`product_dummy` AS `product_dummy` from (select `my_database`.`fav1`.`user_id` AS `user_id`,(@num:=if(((@current_shop_id) = `my_database`.`shops`.`shop_id`),if(((@current_product_id) = `my_database`.`products`.`product_id`),(@num),((@num) + 1)),0)) AS `row_number`,(@current_shop_id:=`my_database`.`shops`.`shop_id`) AS `shop_dummy`,(@current_product_id:=`my_database`.`products`.`product_id`) AS `product_dummy` from `my_database`.`favorites` `fav1` join `my_database`.`products` join `my_database`.`shops` left join (select `my_database`.`fav5`.`product_id` AS `product_id`,sum((case when ((`my_database`.`fav5`.`current` = 1) and (`my_database`.`fav5`.`closeted` = 1)) then 1 when ((`my_database`.`fav5`.`current` = 1) and (`my_database`.`fav5`.`closeted` = 0)) then -(1) else 0 end)) AS `favorites_count` from `my_database`.`favorites` `fav5` group by `my_database`.`fav5`.`product_id`) `fav6` on(((`my_database`.`products`.`product_id` = `my_database`.`fav1`.`product_id`) and (`fav6`.`product_id` = `my_database`.`fav1`.`product_id`))) where ((`my_database`.`fav1`.`user_id` = 30) and (`my_database`.`products`.`product_id` = `my_database`.`fav1`.`product_id`) and (`my_database`.`shops`.`shop_id` = `my_database`.`products`.`shop_id`) and (`my_database`.`fav1`.`current` = 1) and (`my_database`.`fav1`.`closeted` = 1)) group by `my_database`.`fav1`.`product_id` order by `my_database`.`shops`.`shop`) `rowed_results` where ((`rowed_results`.`row_number` >= 0) and (`rowed_results`.`row_number` < 5)) |
+-------+------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
+----+-------------+------------+--------+------------------------------------------------+-----------+---------+------------------------------+-------+----------+----------------------------------------------+
|id |选择|类型|类型|可能的|键|键|列|参考|行|过滤|额外|
+----+-------------+------------+--------+------------------------------------------------+-----------+---------+------------------------------+-------+----------+----------------------------------------------+
|1 | PRIMARY | | ALL | NULL | NULL | NULL | NULL | 8846 | 100.00 |使用where|
|2 |衍生| fav1 |参考|用户id,用户id | 2,产品id,封闭|用户id | 2 | 4 | 9624 | 100.00 |使用where ;;使用临时设备;使用文件排序|
|2 |衍生|产品| eq | ref |主要,车间| id,车间| id | 2,产品| id,车间| id | 3 |主要| 4 |我的|数据库.fav1.product | id | 1 | 100.00 ||
|2 |派生|店铺| eq | U ref | PRIMARY | PRIMARY | 4 |我的|数据库.产品.店铺| id | 1 | 100.00 ||
|2 |派生| |所有|空|空|空|空| 15764 | 100.00 ||
|3 |派生| fav5 | ALL | NULL | NULL | NULL | NULL | 16377 | 100.00 |使用临时变量;使用文件排序|
+----+-------------+------------+--------+------------------------------------------------+-----------+---------+------------------------------+-------+----------+----------------------------------------------+
设置6行,1条警告(11.50秒)
显示最终查询的警告是

+----+-------------+------------+--------+------------------------------------------------+-----------+---------+------------------------------+-------+----------+----------------------------------------------+
| id | select_type | table      | type   | possible_keys                                  | key       | key_len | ref                          | rows  | filtered | Extra                                        |
+----+-------------+------------+--------+------------------------------------------------+-----------+---------+------------------------------+-------+----------+----------------------------------------------+
|  1 | PRIMARY     | <derived2> | ALL    | NULL                                           | NULL      | NULL    | NULL                         |  8846 |   100.00 | Using where                                  |
|  2 | DERIVED     | fav1       | ref    | user_id,user_id_2,product_id,closeted          | user_id_2 | 4       |                              |  9624 |   100.00 | Using where; Using temporary; Using filesort |
|  2 | DERIVED     | products   | eq_ref | PRIMARY,shop_id,shop_id_2,product_id,shop_id_3 | PRIMARY   | 4       | my_database.fav1.product_id  |     1 |   100.00 |                                              |
|  2 | DERIVED     | shops      | eq_ref | PRIMARY                                        | PRIMARY   | 4       | my_database.products.shop_id |     1 |   100.00 |                                              |
|  2 | DERIVED     | <derived3> | ALL    | NULL                                           | NULL      | NULL    | NULL                         | 15764 |   100.00 |                                              |
|  3 | DERIVED     | fav5       | ALL    | NULL                                           | NULL      | NULL    | NULL                         | 16377 |   100.00 | Using temporary; Using filesort              |
+----+-------------+------------+--------+------------------------------------------------+-----------+---------+------------------------------+-------+----------+----------------------------------------------+
6 rows in set, 1 warning (11.50 sec)
+-------+------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Note  | 1003 | select `rowed_results`.`user_id` AS `user_id`,`rowed_results`.`row_number` AS `row_number`,`rowed_results`.`shop_dummy` AS `shop_dummy`,`rowed_results`.`product_dummy` AS `product_dummy` from (select `my_database`.`fav1`.`user_id` AS `user_id`,(@num:=if(((@current_shop_id) = `my_database`.`shops`.`shop_id`),if(((@current_product_id) = `my_database`.`products`.`product_id`),(@num),((@num) + 1)),0)) AS `row_number`,(@current_shop_id:=`my_database`.`shops`.`shop_id`) AS `shop_dummy`,(@current_product_id:=`my_database`.`products`.`product_id`) AS `product_dummy` from `my_database`.`favorites` `fav1` join `my_database`.`products` join `my_database`.`shops` left join (select `my_database`.`fav5`.`product_id` AS `product_id`,sum((case when ((`my_database`.`fav5`.`current` = 1) and (`my_database`.`fav5`.`closeted` = 1)) then 1 when ((`my_database`.`fav5`.`current` = 1) and (`my_database`.`fav5`.`closeted` = 0)) then -(1) else 0 end)) AS `favorites_count` from `my_database`.`favorites` `fav5` group by `my_database`.`fav5`.`product_id`) `fav6` on(((`my_database`.`products`.`product_id` = `my_database`.`fav1`.`product_id`) and (`fav6`.`product_id` = `my_database`.`fav1`.`product_id`))) where ((`my_database`.`fav1`.`user_id` = 30) and (`my_database`.`products`.`product_id` = `my_database`.`fav1`.`product_id`) and (`my_database`.`shops`.`shop_id` = `my_database`.`products`.`shop_id`) and (`my_database`.`fav1`.`current` = 1) and (`my_database`.`fav1`.`closeted` = 1)) group by `my_database`.`fav1`.`product_id` order by `my_database`.`shops`.`shop`) `rowed_results` where ((`rowed_results`.`row_number` >= 0) and (`rowed_results`.`row_number` < 5)) |
+-------+------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
+-------+------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|注| 1003 |选择`排成行的结果'。`用户id`作为`用户id',`排成行的结果'。`排成行的结果'。`商店虚拟'作为`商店虚拟',`排成行的结果'。`产品虚拟'作为`产品虚拟'。'(((@current\u-product\u-id)=`my\u-database`.`products`.`products\u-id`),(@num),(@num)+1)),0)作为`row\u-number`,(@current\u-shop\u-id:=`my\u-database`.`shops`.`shop\u-id`)作为`shop\u-dummy`,(@current\u-product\u-id:=`my\u-database`.`products\u-id`作为`我的'数据库'中的'产品'虚拟'。`收藏夹'`fav1`加入`我的'数据库'。`产品'加入`我的'数据库'。`商店'左加入(选择`我的'数据库'。`fav5`.`产品的id`作为`产品的id`,求和(`我的'数据库'`fav5`.`current`=1)和(`我的'我的'数据库'`fav5`.`Closed`=1)的情况下,然后是(`我的'数据库'`fav5`.`current`=1)和(`my_database`.`fav5`.`Closed`=0))然后-(1)否则0结束)作为`my_database`.`favorites``fav5`按`my_database`.`fav5`.`product_id`.`fav6`.`product_id`=`my_database`.`fav1`.`product_id`.`fav6`.`product_id`=`my_database`.`fav1`.`((`my_database`.`fav1`.`user_id`=30)和(`my_database`.`products`.`product_id`=`my_database`.`fav1`.`product_id`=`30)和(`my_database`.`fav1`.`current`=1)和(`my_database`.`fav1`.`Closed`=1))按“我的”数据库分组。`fav1`.`product\U id`按“我的”数据库分组。`SHOPES`.`SHOPE`)`rowed\U结果`where(`rowed\U结果`.`row\U编号`>=0)和(`rowed\U结果`.`row\U编号`<5))|
+-------+------+-------------------------------------------------------------------------------------------------------------------------------------------------
+-------+------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Note  | 1003 | select `rowed_results`.`user_id` AS `user_id`,`rowed_results`.`row_number` AS `row_number`,`rowed_results`.`shop_dummy` AS `shop_dummy`,`rowed_results`.`product_dummy` AS `product_dummy` from (select `my_database`.`fav1`.`user_id` AS `user_id`,(@num:=if(((@current_shop_id) = `my_database`.`shops`.`shop_id`),if(((@current_product_id) = `my_database`.`products`.`product_id`),(@num),((@num) + 1)),0)) AS `row_number`,(@current_shop_id:=`my_database`.`shops`.`shop_id`) AS `shop_dummy`,(@current_product_id:=`my_database`.`products`.`product_id`) AS `product_dummy` from `my_database`.`favorites` `fav1` join `my_database`.`products` join `my_database`.`shops` left join (select `my_database`.`fav5`.`product_id` AS `product_id`,sum((case when ((`my_database`.`fav5`.`current` = 1) and (`my_database`.`fav5`.`closeted` = 1)) then 1 when ((`my_database`.`fav5`.`current` = 1) and (`my_database`.`fav5`.`closeted` = 0)) then -(1) else 0 end)) AS `favorites_count` from `my_database`.`favorites` `fav5` group by `my_database`.`fav5`.`product_id`) `fav6` on(((`my_database`.`products`.`product_id` = `my_database`.`fav1`.`product_id`) and (`fav6`.`product_id` = `my_database`.`fav1`.`product_id`))) where ((`my_database`.`fav1`.`user_id` = 30) and (`my_database`.`products`.`product_id` = `my_database`.`fav1`.`product_id`) and (`my_database`.`shops`.`shop_id` = `my_database`.`products`.`shop_id`) and (`my_database`.`fav1`.`current` = 1) and (`my_database`.`fav1`.`closeted` = 1)) group by `my_database`.`fav1`.`product_id` order by `my_database`.`shops`.`shop`) `rowed_results` where ((`rowed_results`.`row_number` >= 0) and (`rowed_results`.`row_number` < 5)) |
+-------+------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
CREATE TEMPORARY TABLE fav6 (product_id INT, favorites_count INT, PRIMARY KEY (product_id));

INSERT INTO fav6
    SELECT fav5.product_id AS product_id, SUM(CASE 
        WHEN fav5.current = 1 AND fav5.closeted = 1 THEN 1
        WHEN fav5.current = 1 AND fav5.closeted = 0 THEN -1
        ELSE 0
        END) AS favorites_count
    FROM favorites fav5
    GROUP BY fav5.product_id;

SET @num :=0, @current_shop_id := NULL, @current_product_id := NULL;

SELECT * FROM (

SELECT fav1.user_id, @num := IF(@current_shop_id=shops.shop_id, IF(@current_product_id=products.product_id,@num,@num+1),0) AS row_number, @current_shop_id := shops.shop_id AS shop_dummy, @current_product_id := products.product_id AS product_dummy

    FROM

    #limit results to products favorited by the user whose closet is being shown
    favorites fav1 INNER JOIN

    products ON
    fav1.product_id=products.product_id AND 
    fav1.current=1 AND
    fav1.closeted=1 AND 
    fav1.user_id=30  INNER JOIN

    shops ON
    shops.shop_id = products.shop_id 

    LEFT JOIN

    fav6 ON products.product_id=fav6.product_id

    GROUP BY fav1.product_id
    ORDER BY shops.shop ASC

    ) AS rowed_results WHERE rowed_results.row_number>=0 AND rowed_results.row_number<(0+5)