SQL Server对多个select执行速度太慢

时间:2012-09-26 07:09:38

标签: sql-server

SELECT 
   CountryCode, CountryName, CountryEName, FirstShow, iseTicketFirstShow 
FROM 
   Tristar.dbo.COMCountry 
WHERE 
  ContinentName = 'Continent Name' AND iseTicket = 1 
  and CountryCode in 
    (select Distinct CountryCode 
     from eTicketSubAirport 
     where AirportCode in 
         (select DISTINCT eTDestinationAirport 
          from eTicketMain 
          where eTChecked=1 and eTDepartAirport='TPE')
    ) 
ORDER BY 
   iseTicketFirstShow DESC, FirstShow,CountryEName

上面的代码用了3秒......这是不可接受的。 2内部选择本身执行速度非常快。

如果我带走了一个内部选择,例如....

SELECT 
   CountryCode, CountryName, CountryEName, FirstShow, iseTicketFirstShow 
FROM 
   Tristar.dbo.COMCountry 
WHERE 
   ContinentName = 'continent name' AND iseTicket = 1 
   and CountryCode in 
     (select Distinct CountryCode 
      from eTicketSubAirport) 
ORDER BY 
   iseTicketFirstShow DESC, FirstShow,CountryEName

这也执行得非常快。

哈希匹配太过79%的处理。 [eTicketSubAirport]

我无法取出选择的任何部分,因为它们都是必要的....

1 个答案:

答案 0 :(得分:2)

尝试使用连接,它看起来像这样:

SELECT DISTINCT
   Country.CountryCode, 
   Country.CountryName, 
   Country.CountryEName, 
   Country.FirstShow, 
   Country.iseTicketFirstShow 
FROM 
   Tristar.dbo.COMCountry AS Country
   INNER JOIN eTicketSubAirport ON Country.CountryCode = eTicketSubAirport.CountryCode
   INNER JOIN eTicketMain       ON eTicketSubAirport.AirportCode = eTicketMain.eTDestinationAirport
WHERE 
  Country.ContinentName = 'Continent Name' 
  AND Country.iseTicket = 1 
  AND eTicketMain.eTChecked = 1 
  AND eTicketMain.eTDepartAirport = 'TPE'
ORDER BY 
   Country.iseTicketFirstShow DESC, 
   Country.FirstShow,
   Country.CountryEName