我在我的 EF 4.3.1 中使用了一些简单的 LINQ:
Dim Results = (
From T1 In Context.Table1
Where T1.IDColumn = 1568
Join T2 In Context.Table2 On T1.IDColumn Equals T2.IDColumn
Select New With {
.Quantity = T1.Quantity
})
不幸的是,当我运行它时,它需要永远。我已经将问题追溯到生成的 SQL 中似乎完全不必要的部分。生成的 SQL 如下所示:
SELECT 1 AS "C1"
,CAST( "Extent2"."Quantity" AS number(19,0)) AS "C2"
FROM [table1] "Extent1"
INNER JOIN [table2] "Extent2" ON
(
(
CAST( "Extent1"."IDColumn" AS number(19,0))
) = (
CAST( "Extent2"."IDColumn" AS number(19,0))
)
)
OR
(
(
CAST( "Extent1"."IDColumn" AS number(19,0)) IS NULL
)
AND
(
CAST( "Extent2"."IDColumn" AS number(19,0)) IS NULL
)
)
WHERE
(
1568 = (
CAST( "Extent1"."ForeginKey" AS number(19,0))
)
)
此查询大约需要 13 秒才能执行。如果我删除 OR / NULL 块,它会下降到 0.122 秒:
SELECT 1 AS "C1"
,CAST( "Extent2"."Quantity" AS number(19,0)) AS "C2"
FROM [table1] "Extent1"
INNER JOIN [table2] "Extent2" ON
(
(
CAST( "Extent1"."IDColumn" AS number(19,0))
) = (
CAST( "Extent2"."IDColumn" AS number(19,0))
)
)
WHERE
(
1568 = (
CAST( "Extent1"."ForeginKey" AS number(19,0))
)
)
有人对我如何让 linq 查询不生成(不需要的)空检查有建议吗?