在这个优秀的SO问题中,讨论了CTE和之间的sub-queries差异.
我想具体问一下:
在什么情况下,以下各项更有效/更快?
传统上,我temp tables在开发中使用了很多stored procedures- 因为它们看起来比许多交织在一起的子查询更具可读性.
Non-recursive CTEs非常好地封装数据集,并且非常易读,但是在某些情况下可以说它们总能表现得更好吗?或者是否必须总是摆弄不同的选项才能找到最有效的解决方案?
编辑
我最近被告知,就效率而言,临时表是一个很好的首选,因为它们具有相关的直方图即统计数据.
可能重复:
CTE和SubQuery之间的区别?
我试图了解如何使用该WITH条款和该条款的目的WITH.
我理解的只是,该WITH条款取代了正常的子查询.
任何人都可以通过一个小例子详细解释这个问题吗?
WITH子句和子查询之间有什么区别?
1. WITH table_name as ( ... )
2. select *
from ( select curr from tableone t1
left join tabletwo t2
on (t1.empid = t2.empid)
) as temp_table
Run Code Online (Sandbox Code Playgroud) 我有以下存储过程返回A,B和降序计数.我试图用ROW_NUMBER,这样我就可以页的记录,但我希望第一个行号1是具有最高计数的记录,所以基本上,如果我返回一个表3次的记录和计数30,20,10,再行号1应计对应30,行号2应与计数对应20,和行号3应与计数对应10. dbo.f_GetCount是一个返回计数的函数.
create procedure dbo.Test
as
@A nvarchar(300) = NULL,
@B nvarchar(10) = NULL
as
select @A = nullif(@A,'')
,@B = nullif(@B,'');
select h.A
,hrl.B
,dbo.f_GetCount(hrl.A,h.B) as cnt
from dbo.hrl
inner join dbo.h
on h.C = hrl.C
where(@A is null
or h.A like '%'+@A+'%'
)
and (@B is null
or hrl.B = @B …Run Code Online (Sandbox Code Playgroud) Select id, name, ROW_NUMBER() OVER (ORDER BY id asc) as 'RowNo'
from customers
where RowNo between 50 AND 60
Run Code Online (Sandbox Code Playgroud)
我试图选择50到60之间的行的子集.问题是'RowNo'是无效的列名.
谢谢
使用SQL SERVER 2008 R2
我有一个子查询(LastActivityOn),我想在三个地方使用,我的投影(SELECTed输出),ORDER BY和WHERE子句.
SELECT TOP 175
(SELECT MAX(ActivityDate) FROM (VALUES
(UserRegistration.CreatedOn),
(UserRegistration.ActivatedOn),
(UserRegistration.LastLoginOn),
(UserRegistration.UpdatedOn),
(UserProfile.LastPostedOn)) AS AllDates(ActivityDate)) LastActivityOn,
UserRegistration.FirstName,
UserRegistration.LastName,
[15 more columns of various calculated distances, coalesces, etc...]
FROM
UserRegistration
INNER JOIN UserProfile ON UserRegistration.Id = UserProfile.RegistrationId
INNER JOIN (
SELECT PostalCode, GeoCenter, PrimaryCity, StateOrProvince
FROM PostalCodes
WHERE @OriginPostalCode IS NULL OR PostalCodes.GeoCenter.STDistance(@OriginPoint) < @WithinMeters
) AS ProximalPostalCodes ON ProximalPostalCodes.PostalCode = UserRegistration.PostalCode
[7 more joins including full-text queries]
WHERE
LastActivityOn > @OldestUserToSearch AND
[20 more lines of filtering …Run Code Online (Sandbox Code Playgroud) 我有两个等效的查询,它们提取特定地区 (ace) 和城市 (pro_com) 中建筑物(表 a)和最近的高速公路(表 v 中的高速公路)之间的平均距离。
这是 CTE 版本
WITH subq AS (
SELECT a.n, a.geom as g1, unnest(ARRAY(SELECT v.geom as g2
FROM atlas_sezioni2 as v
where v.code = '12230' and a.pro_com = v.pro_com and a.code <> v.code
ORDER BY a.geom <-> v.geom LIMIT 15)) as g2
FROM atlas_sezioni2 a
where a.pro_com = 15146 and a.ace = 1 and a.code IN('11100', '11210', '11220', '11230', '11240', '11300', '12100', '14200')
)
select avg(dist) from (
select distinct on(n) n, dist …Run Code Online (Sandbox Code Playgroud) 这是样本表
我需要实现的是仅获取或显示月份值最高的租户记录.如果月份相等,我需要根据最新的日期值.这是所需的输出样本
有了这个,我开始使用这个代码使用max函数并合并临时表,但无法获得所需的结果.
select tenant, name, date, month
into #sample
from tenant
select *
from #sample
where months = (select max(months)from #sample)
Run Code Online (Sandbox Code Playgroud)
并输出到这样的东西.我相信,代码在整个列表中获得最大值而不考虑每个租户过滤.
任何帮助将不胜感激 :)
sql ×6
sql-server ×3
t-sql ×3
subquery ×2
database ×1
oracle ×1
postgis ×1
postgresql ×1
row-number ×1
temp-tables ×1
with-clause ×1