3

I have created this good but slow working SQL-Statement to search the highest price from a pricelist linked to an item from the itemlist. Known is the items itemnr:

SELECT i.ItemNr, i.ItemId, x.maxprice
  FROM itemlist i,
       pricelist p,
       (SELECT MAX (p2.price) AS maxprice, p2.ItemId
          FROM pricelist p2
         GROUP BY p2.ItemId) x
 WHERE i.ItemNr = 4711
   AND i.ItemId = p.ItemId
   AND i.ItemId = x.ItemId
   AND p.price = x.maxprice

The itemlist holds about 100000 items and the pricelist about 1000000 prices. This statement is realy slow. I am afraid that the db-server searches the whole pricelist for every item I search.

I know the itemnr. But can I somehow search the correponding itemid and "send" this itemid to the subquery? So the subquery can find the highest price of this item quickly??? Or is there an other solution for my problem?

Help please.

4

1 回答 1

1

我已将不需要的 JOIN 删除回pricelist,因为您没有从中选择任何列:

select i.ItemNr, i.ItemId, x.maxprice
from itemlist i
inner join (
    select MAX(price) as maxprice, ItemId
    from pricelist
    group by ItemId
    ) x on i.ItemId = x.ItemId
where i.ItemNr = 4711

一旦您提供了一些有关我可以提出建议的信息,一些索引调整可能会有所帮助。

于 2012-11-01T14:26:19.700 回答