Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/69.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.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 虽然Sql查询性能相同,但它们是不同的_Mysql_Sql - Fatal编程技术网

Mysql 虽然Sql查询性能相同,但它们是不同的

Mysql 虽然Sql查询性能相同,但它们是不同的,mysql,sql,Mysql,Sql,共有2个表格,其结构如下: mysql> desc product; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | id | int(11) | NO | PRI | NULL |

共有2个表格,其结构如下:

mysql> desc product;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | NO   | PRI | NULL    |       |
| brand | varchar(20) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.02 sec)


mysql> desc sales;
+-------------+-------------+------+-----+---------+-------+
| Field       | Type        | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+-------+
| id          | int(11)     | YES  |     | NULL    |       |
| yearofsales | varchar(10) | YES  |     | NULL    |       |
| price       | int(11)     | YES  |     | NULL    |       |
+-------------+-------------+------+-----+---------+-------+
3 rows in set (0.01 sec)
这里的
id
是外键

查询如下:

一,

二,


问题是:为什么第二个查询比第一个查询花费更少的时间?我也以不同的顺序执行了多次

您可以检查两个查询的执行计划以及两个表上的索引,以了解为什么一个查询比另一个查询占用更多的时间。此外,您无法运行一个简单的测试并信任结果,有许多因素可能会影响查询的执行,例如服务器在执行一个查询时忙于其他事情,因此运行速度较慢。您必须多次运行这两个查询,然后比较平均值

但是,强烈建议使用显式联接而不是隐式联接:

SELECT brand, SUM(price), yearofsales
FROM product p
INNER JOIN sales s ON p.id = s.id
GROUP BY s.id, yearofsales;

您使用的是MySQL还是MS SQL Server?考虑到他们的语句是以
MySQL>
开头的,我假设是MySQL:)如果你能看一看执行计划,它会给你一个关于时间在哪里被用完的线索。能够看到表数据的索引和大小也会提供线索。请为这些查询显示EXPLAIN的输出。在不知道所使用的查询计划的情况下,任何答案都只是一个有根据的猜测。
mysql> select brand,tmp.yearofsales,tmp.sum 
       from product p 
       join (
           select id,yearofsales,sum(price) as sum
           from sales
           group by yearofsales,id
       ) tmp on p.id=tmp.id ;
+-------+-------------+-----------+
| brand | yearofsales | sum       |
+-------+-------------+-----------+
| Nike  | 2012        | 917504000 |
| FF    | 2011        | 328990720 |
| FF    | 2012        | 723517440 |
| FF    | 2010        | 328990720 |
+-------+-------------+-----------+
4 rows in set (1.59 sec)
SELECT brand, SUM(price), yearofsales
FROM product p
INNER JOIN sales s ON p.id = s.id
GROUP BY s.id, yearofsales;