gpt4 book ai didi

c# - 从 lambda 表达式生成 sql server 查询的替代方法

转载 作者:行者123 更新时间:2023-11-30 20:07:32 25 4
gpt4 key购买 nike

我编写了一个 lambda 表达式,它产生了预期的结果,但是它产生了一个绝对庞大的 sql 查询并且性能很差。请参阅最底部的 io/time 统计信息。

还有其他方法可以实现下面的查询吗?

select distinct(searchterms) as SearchTerms, max(totalresults) FROM cmsSearchLog 
where totalresults != 0 and searchterms like 'de%' group by searchterms
order by max(totalresults) desc

C# 代码片段:

// current lamda expression; has bad performance compared to above query
List<SearchTerm> existingSearchTerms1 = context.cmsSearchLogs.Where(oq =>
context.cmsSearchLogs.Where(q =>
q.SearchTerms.ToLower().Contains(terms.ToLower()) && q.TotalResults != 0)
.Select(s => s.SearchTerms)
.Distinct()
.Contains(oq.SearchTerms))
.Select(a => new { a.SearchTerms, a.TotalResults })
.GroupBy(gb => gb.SearchTerms)
.OrderByDescending(ob => ob.Max(m => m.TotalResults))
.Select(s => new SearchTerm()
{
SearchTerms = s.FirstOrDefault().SearchTerms,
TotalResults = s.FirstOrDefault().TotalResults
}
)
.ToList();

// get the suggestions back as a list of strings
List<string> suggestions = Enumerable.Range(0,
existingSearchTerms1.Count())
.Select(x => existingSearchTerms1.ElementAt(x).SearchTerms).ToList();

这是保存查询结果的私有(private)类

private class SearchTerm
{
public string SearchTerms { get; set; }
public int TotalResults { get; set; }
}

lambda表达式生成的sql非常庞大:

SELECT 
[Project13].[C2] AS [C1],
[Project13].[C3] AS [C2],
[Project13].[C4] AS [C3]
FROM ( SELECT
[Project12].[C1] AS [C1],
1 AS [C2],
[Project12].[C2] AS [C3],
[Project12].[C3] AS [C4]
FROM ( SELECT
[Project8].[C1] AS [C1],
[Project8].[C2] AS [C2],
(SELECT TOP (1)
[Extent5].[TotalResults] AS [TotalResults]
FROM [dbo].[cmsSearchLog] AS [Extent5]
WHERE ( EXISTS (SELECT 1 AS [C1]
FROM ( SELECT DISTINCT
[Extent6].[SearchTerms] AS [SearchTerms]
FROM [dbo].[cmsSearchLog] AS [Extent6]
WHERE (( CAST(CHARINDEX(LOWER('dew'),
LOWER([Extent6].[SearchTerms])) AS int)) > 0)
AND (0 <> [Extent6].[TotalResults])
) AS [Distinct3]
WHERE [Distinct3].[SearchTerms] = [Extent5].[SearchTerms]
)) AND ([Project8].[SearchTerms] = [Extent5].[SearchTerms]))
AS [C3]
FROM ( SELECT
[Project7].[C1] AS [C1],
[Project7].[SearchTerms] AS [SearchTerms],
[Project7].[C2] AS [C2]
FROM ( SELECT
[Project3].[C1] AS [C1],
[Project3].[SearchTerms] AS [SearchTerms],
(SELECT TOP (1)
[Extent3].[SearchTerms] AS [SearchTerms]
FROM [dbo].[cmsSearchLog] AS [Extent3]
WHERE ( EXISTS (SELECT 1 AS [C1] FROM ( SELECT DISTINCT
[Extent4].[SearchTerms] AS [SearchTerms]
FROM [dbo].[cmsSearchLog] AS [Extent4]
WHERE (( CAST(CHARINDEX(LOWER('dew'),
LOWER([Extent4].[SearchTerms])) AS int)) > 0)
AND (0 <> [Extent4].[TotalResults])) AS [Distinct2]
WHERE [Distinct2].[SearchTerms] = [Extent3].[SearchTerms]
)) AND ([Project3].[SearchTerms] = [Extent3].[SearchTerms])) AS [C2]
FROM ( SELECT
[GroupBy1].[A1] AS [C1],
[GroupBy1].[K1] AS [SearchTerms]
FROM ( SELECT
[Extent1].[SearchTerms] AS [K1],
MAX([Extent1].[TotalResults]) AS [A1]
FROM [dbo].[cmsSearchLog] AS [Extent1]
WHERE EXISTS (SELECT 1 AS [C1]
FROM ( SELECT DISTINCT [Extent2].[SearchTerms]
AS [SearchTerms] FROM [dbo].[cmsSearchLog] AS [Extent2]
WHERE (( CAST(CHARINDEX(LOWER('dew'),
LOWER([Extent2].[SearchTerms])) AS int)) > 0)
AND (0 <> [Extent2].[TotalResults])) AS [Distinct1]
WHERE [Distinct1].[SearchTerms] = [Extent1].[SearchTerms])
GROUP BY [Extent1].[SearchTerms]) AS [GroupBy1]
) AS [Project3]
) AS [Project7]
) AS [Project8]
) AS [Project12]
) AS [Project13]
ORDER BY [Project13].[C1] ASC

我在打开 io 和时间统计信息的情况下执行了两个查询,结果如下。 (注意:首先是 lambda 生成的查询,其次是我手写的查询)因此这证实了我的怀疑,即与我实际想要的查询相比,生成的查询执行得非常糟糕。

(8 row(s) affected)
Table 'cmsSearchLog'. Scan count 6, logical reads 106, physical reads 0,
read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.

SQL Server Execution Times:
CPU time = 0 ms, elapsed time = 1 ms.

(7 row(s) affected)
Table 'cmsSearchLog'. Scan count 1, logical reads 5, physical reads 0,
read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.

SQL Server Execution Times:
CPU time = 0 ms, elapsed time = 0 ms.

最佳答案

试试这个查询而不是你当前的 LINQ 查询:

var query = from x in context.cmsSearchLog
where totalresults != 0 &&
searchterms.BeginsWith("de")
group x by x.searchterms into terms
select new {
SearchTerms = terms.Key(),
TotalResults = terms.Max(t => t.totalresults)
};

我还没有测试过它,但我认为它会生成一个非常高效的查询并返回所需的结果。

关于c# - 从 lambda 表达式生成 sql server 查询的替代方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8305461/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com