Ich habe SQL Server 2008 zu diesen drei Variationen der Aquarius verwendet. Bei meinen Tests wird die AdventureWorks-Datenbank mithilfe von ProductInventory im Produktionsschema abgefragt. Die drei Abfragen sind:
declare @max int
Select @max = MAX(Quantity) FROM [AdventureWorks].[Production].[ProductInventory]
SELECT TOP 1000 [ProductID]
,[LocationID]
,[Shelf]
,[Bin]
,[Quantity]
,[rowguid]
,[ModifiedDate]
FROM [AdventureWorks].[Production].[ProductInventory]
WHERE Quantity = @max
SELECT TOP 1000 [ProductID]
,[LocationID]
,[Shelf]
,[Bin]
,[Quantity]
,[rowguid]
,[ModifiedDate]
FROM [AdventureWorks].[Production].[ProductInventory]
WHERE Quantity = (Select MAX(Quantity) FROM [AdventureWorks].[Production].[ProductInventory])
SELECT TOP 1000 AW1.[ProductID]
,AW1.[LocationID]
,AW1.[Shelf]
,AW1.[Bin]
,AW1.[Quantity]
,AW1.[rowguid]
,AW1.[ModifiedDate]
FROM [AdventureWorks].[Production].[ProductInventory] AW1
LEFT JOIN [AdventureWorks].[Production].[ProductInventory] AW2 ON AW1.Quantity < AW2.Quantity
WHERE AW2.ProductID IS NULL;
die „Show geschätzten Abfrageplan“ Symbol verwenden ich die exection Ereignisse für drei Fälle vergleichen. Die Ergebnisse sind:
- Deklarieren einer Variablen und Füllen der Variablen ist 5% schneller als eine Unterauswahl in der Where-Klausel.
- Die Verbindung ist 98% langsamer als die subselect
- Die Verbindung ist 99% langsamer als die Variable
Mein Vorschlag ist, eine Variable zu deklarieren und sie füllen.Verwenden Sie die Variable in der WHERE-Klausel
Warum hat MySQL dieses Problem mit IN? Wo finde ich Unterlagen dazu? – BMario
@BMario - Siehe http://stackoverflow.com/questions/3417074/why-would-an-in-condition-be-slower-than-in-sql –