我已經將EDM實體映射到數據庫(SQL Server 2005)視圖。 該實體是一個簡單的電影實體,其具有的ID,姓名和DateInserted其對應於其中具有以下定義一個視圖屬性:實體框架爲映射到視圖的實體生成的SQL
SELECT iMovieID,vchName,dtInsertDate
FROM dbo.t_Movie WITH(NOLOCK)
表t_Movie具有以下定義:
CREATE TABLE [DBO] [t_Movie](
[iMovieID] [INT] IDENTITY(1,1)NOT NULL,
[vchName] VARCHAR NOT NULL,
[dtInsertDate] [日期時間] NULL,
約束[PK_t_Movie] PRIMARY KEY CLUSTERED
( [iMovieID] ASC
)WITH( PAD_INDEX = OFF,STATISTICS_NORECOMPUTE = OFF,IGNORE_DUP_KEY = OFF,
ALLOW_ROW_LOCKS = ON,ALLOW_PAGE_LOCKS = ON)ON [PRIMARY]
)ON [PRIMARY]
GO
當我編寫一個簡單的LINQ實體詢問像這樣:
var q = from m in context.v_Movie where m.vchName.Contains("Ocean") select m;
foreach (var movie in q)
{
Console.WriteLine("{0}:{1}",movie.iMovieID, movie.vchName);
}
這裏是通過由分析器捕獲的實體框架生成的SQL:
SELECT
[Extent1] [iMovieID] AS [iMovieID],
[Extent1] [vchName] AS [vchName],
[Extent1] [dtInsertDate] AS [dtInsertDate]
FROM(SELECT
[v_Movie] [iMovieID] AS [iMovieID],
[v_Movie]。[VCH名] AS [vchName],
[v_Movie] [dtInsertDate] AS [dtInsertDate]
FROM [DBO]。[v_Movie] AS [v_Movie])AS [Extent1]
WHERE(CAST(CHARINDEX(N'Ocean 」,[Extent1] AS INT [vchName])))> 0
DBA擁有關注的是,內部的選擇:。
SELECT
[v_Movie] [iMovieID] AS [ iMovieID],
[v_Movie]。[vchName] AS [vchName],
[v_Movie] [dtInsertDate] AS [dtInsertDate]
FROM [DBO]。[v_Movie] AS [v_Movie])AS [Extent1]
會造成一段時間一些嚴重的性能問題表,因爲它選擇的所有增長將視圖中的行轉換爲臨時表([Extent1]),然後外部SELECT從此臨時表中進行選擇。
任何特別的原因EF需要做到這一點,是沒有任何理由下不能一直生成的SQL:
選擇
[v_Movie] [iMovieID] AS [iMovieID ],
[v_Movie] [vchName] AS [vchName],
[v_Movie] [dtInsertDate] AS [dtInsertDate]
FROM [DBO]。[v_Movie] AS [v_Movie]
WHERE(CAST(CHARINDEX (N'Ocean',[Extent1]。[vchName])AS int))> 0
我使用以下SQL填充100,000個記錄的表,但在執行LINQ查詢時未注意到任何性能下降。探查表明,查詢下一個第二RAN:
BEGIN
DECLARE @counter詮釋
組@counter = 0
而@counter 開始
組@counter = @counter + 1
INSERT INTO t_Movie(vchName)的值( '電影' + CONVERT(VARCHAR,@計數器))
端
END
這是一個有效的擔憂嗎?
PS -
(CAST(CHARINDEX(N'Ocean」,[Extent1] [vchName])爲INT)。) 這裏不是問題,因爲在LINQ到實體查詢我用只是爲了說明。
任何見解將使用XML編輯器非常感激