我有這樣的LINQ語句,爲什麼實體框架生成這個SQL?
var carriageways = from carriageway in dataModelCurrentEntities.Carriageway
where carriageway.RoadId == roadId && carriageway.DistanceBreak == false
orderby carriageway.CarriagewayStartInMetre
select new CarriagewaySummary
{
StartMetres = carriageway.CarriagewayStartInMetre,
EndMetres = carriageway.CarriagewayEndInMetre
};
它生成SQL以這種形式(LINQ到實體),
SELECT
Project1.field1 AS field1
Project1.field2 AS field2
FROM (SELECT
Extent1.field1 AS field1,
Extent1.field2 AS field2
FROM table AS Extent1
WHERE blah
) AS Project1
ORDER BY blah ASC
,這是什麼原因何在呢?我會認爲這樣的東西已經足夠了,我記得LINQ to SQL會傾向於生成更簡單的SQL。
我看過更復雜的例子,連接等,和LINQ到實體似乎生成更復雜的SQL。
UPDATE:
這很有趣,因爲我是想測試一下你說的話,我碰到這個LINQ來了,
var attachments = (from a in entities.Attachments
where a.AttachmentID == 749
select new {a.AddedOn, a.AddedBy});
和產生這個SQL,
SELECT
[Extent1].[AttachmentID] AS [AttachmentID],
[Extent1].[AddedOn] AS [AddedOn],
[Extent1].[AddedBy] AS [AddedBy]
FROM [dbo].[Attachment] AS [Extent1]
WHERE 749 = [Extent1].[AttachmentID]
這個沒有子查詢。
區別是(至少其中一個)...等待它。 Informix的。上面生成子查詢的第一個查詢使用informix。第二個查詢不是SQL服務器。
它可能不那麼簡單,因爲查詢是不同的。
我確實需要第二個查詢,並將其分解成這樣(手動)一個子查詢,
SELECT
[Project1].[AttachmentID] AS [AttachmentID],
[Project1].[AddedOn] AS [AddedOn],
[Project1].[AddedBy] AS [AddedBy]
FROM (SELECT
[Extent1].[AttachmentID] AS [AttachmentID],
[Extent1].[AddedOn] AS [AddedOn],
[Extent1].[AddedBy] AS [AddedBy]
FROM [dbo].[Attachment] AS [Extent1]
WHERE 749 = [Extent1].[AttachmentID]
) AS Project1
SQL服務器顯示了兩種相同的執行計劃,所以就像你說的SQL服務器能夠優化它相當好。另一方面,Informix在優化事物方面是陰暗的。