Ste*_*ner 26 sql-server maintainability performance common-table-expression
一直坚持使用SQL2000太长时间了,我并没有真正接触过Common Table Expressions.
我在这里给出的答案(#4025380)和这里(#4018793)已经违背了流程,因为他们没有使用CTE.
我很感激,对于递归它们是beez kneez,并且有一些查询可以通过它们的使用大大简化,但在什么时候它们的使用只是轻浮?与子查询或联接相比,它们是否具有很好的性能优势?他们真的简化了代码并使其更易于维护吗?
简而言之,何时使用CTE而不是'较小'语法是一种好习惯.
Kei*_*thS 24
在以下情况下,通常应使用CTE而非普通子查询:
简而言之,是的,它们确实使得查询在使用得当时更具可读性.
Joe*_*lli 19
就个人而言,一旦我习惯使用它们,我认为它们可以生成更清晰,更易读的代码.例如,将您的答案与我的答案进行比较#4018793.我们基本上做了同样的事情; 我使用过CTE但你没有.
你的答案没有CTE:
SELECT
course,
section,
grade,
gradeCount
FROM
table
INNER JOIN
(SELECT
grade,
Max(gradeCount) as MaxGradeCount
FROM
table
) MaxGrades
ON table.grade = MaxGrades.grade
AND table.gradeCount = MaxGrades.MaxGradeCount
ORDER BY
table.grade
Run Code Online (Sandbox Code Playgroud)
我对CTE的回答是:
;with cteMaxGradeCount as (
select
grade,
max(gradeCount) as MaxGradeCount
from @Test
group by grade
)
select
t.course,
t.SECTION,
t.grade,
t.gradeCount
from cteMaxGradeCount c
inner join @Test t
on c.grade = t.grade
and c.MaxGradeCount = t.gradeCount
order by t.grade
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5317 次 |
| 最近记录: |