- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我编写了一个 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/
可以使用 lambda 和函数创建有序对(Lisp 中的缺点),如 Use of lambda for cons/car/cdr definition in SICP 所示。 它也适用于 Python
我正在尝试从另一个调用一个 AWS lambda 并执行 lambda 链接。这样做的理由是 AWS 不提供来自同一个 S3 存储桶的多个触发器。 我创建了一个带有 s3 触发器的 lambda。第一
根据以下源代码,常规 lambda 似乎可以与扩展 lambda 互换。 fun main(args: Array) { val numbers = listOf(1, 2, 3) f
A Tutorial Introduction to the Lambda Calculus 本文介绍乘法函数 The multiplication of two numbers x and y ca
我想弄清楚如何为下面的表达式绘制语法树。首先,这究竟是如何表现的?看样子是以1和2为参数,如果n是 0,它只会返回 m . 另外,有人可以指出解析树的开始,还是一个例子?我一直找不到一个。 最佳答案
在 C++0x 中,我想知道 lambda 函数的类型是什么。具体来说: #include type1 foo(int x){ return [x](int y)->int{return x * y
我在其中一个职位发布中看到了这个问题,它询问什么是 lambda 函数以及它与高阶函数的关系。我已经知道如何使用 lambda 函数,但不太自信地解释它,所以我做了一点谷歌搜索,发现了这个:What
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
Evaluate (((lambda(x y) (lambda (x) (* x y))) 5 6) 10) in Scheme. 我不知道实际上该怎么做! ((lambda (x y) (+ x x
我正在处理 MyCustomType 的实例集合如下: fun runAll(vararg commands: MyCustomType){ commands.forEach { it.myM
Brian 在他对问题 "Are side effects a good thing?" 的论证中的前提很有趣: computers are von-Neumann machines that are
在 Common Lisp 中,如果我希望两个函数共享状态,我将按如下方式执行 let over lambda: (let ((state 1)) (defun inc-state () (in
Evaluate (((lambda(x y) (lambda (x) (* x y))) 5 6) 10) in Scheme. 我不知道实际上该怎么做! ((lambda (x y) (+ x x
作为lambda calculus wiki说: There are several possible ways to define the natural numbers in lambda cal
我有一个数据类,我需要初始化一些 List .我需要获取 JsonArray 的值(我使用的是 Gson)。 我做了这个函数: private fun arrayToList(data: JsonAr
((lambda () )) 的方案中是否有简写 例如,代替 ((lambda () (define x 1) (display x))) 我希望能够做类似的事情 (empty-lam
我在 Java library 中有以下方法: public void setColumnComparator(final int columnIndex, final Comparator colu
我正在研究一个函数来计算国际象棋游戏中棋子的有效移动。 white-pawn-move 函数有效。当我试图将其概括为任一玩家的棋子 (pawn-move) 时,我遇到了非法函数调用。我已经在 repl
考虑这段代码(在 GCC 和 MSVC 上编译): int main() { auto foo = [](auto p){ typedef decltype(p) p_t;
我正在阅读一个在 lambda 内部使用 lambda 的片段,然后我想通过创建一个虚拟函数来测试它,该函数从文件中读取然后返回最大和最小数字。 这是我想出来的 dummy = lambda path
我是一名优秀的程序员,十分优秀!