我想知道爲什麼我的select語句在更新從一個表到另一個表的值時比我的update語句快幾個數量級,而不是從一個表中選擇值到另一個表。下面的例子說明,我可以運行我的select語句很快基於另一個表更新值到一個錶慢
select customers.name, countries.country
from customers
inner join countries on customers.countrycode = countries.countrycode
但是我寫的更新語句(我相當肯定的是同樣的事情)發生量級更長的時間來完成我的問題。
update customers
set customers.country = countries.country
from customers
inner join countries on customers.countrycode = countries.countrycode
有什麼建議?
UPDATE:
下面是SELECT語句
|--Hash Match(Inner Join, HASH:([countries].[countrycode])=([Testing].[dbo].[customers].[countrycode]), RESIDUAL:(@countries.[countrycode] as [countries].[countrycode]=[Testing].[dbo].[customers].[countrycode]))
|--Table Scan(OBJECT:(@countries AS [countries]))
|--Table Scan(OBJECT:([Testing].[dbo].[customers]))
下面的計劃是在UPDATE語句的計劃
|--Table Update(OBJECT:([Testing].[dbo].[Customers]), SET:([Testing].[dbo].[Customers].[Country] = @countries.[country] as [countries].[country]))
|--Top(ROWCOUNT est 0)
|--Stream Aggregate(GROUP BY:([Bmk1000]) DEFINE:([countries].[country]=ANY(@countries.[country] as [countries].[country])))
|--Nested Loops(Inner Join, WHERE:(@countries.[countrycode] as [countries].[countrycode]=[Testing].[dbo].[Customers].[countrycode]))
|--Table Scan(OBJECT:([Testing].[dbo].[Customers]))
|--Table Scan(OBJECT:(@countries AS [countries]))
請問您可以發佈兩個查詢的計劃嗎?運行'SET SHOWPLAN_TEXT ON GO',然後運行查詢。 – Quassnoi 2010-11-09 17:01:26