这两个查询中的哪一个更有效或运行最佳?

时间:2017-05-12 23:21:05

标签: sql

查询1

SELECT m.Name, p.Name, p.Price
    FROM Manufacturers m INNER JOIN Products p
        ON m.Code = p.Manufacturer
WHERE p.Price IN (SELECT MAX(pp.Price) FROM Products pp GROUP BY pp.Manufacturer);

查询2

  select m.Name as ManufName, p.Name as ProductName, p.Price as ProductPrice
    from Manufacturers m inner join Products p
      on m.Code = p.Manufacturer
        where p.Price =   (select max(sub_p.Price)
                                  from Products sub_p
                                    where m.Code = sub_p.Manufacturer);

哪一个应该花费更少的时间?

1 个答案:

答案 0 :(得分:1)

这取决于。表格,索引等的大小尝试它们并进行比较。

但是,对数据库一无所知,我会猜测查询1应该表现得更好。查询2有correlated sub query,这将使优化器使用嵌套循环。