我对从分区的范围值中查找最小值/最大值有一个独特的要求。这个想法是通过查询元数据而不是表本身来获得响应时间。
例如,我有一张按营业日期划分的表。当我对表进行最小/最大/不同的业务日期计数时——当我试图对SQL Server的分区范围系统表应用相同的逻辑时,结果不匹配。
代码:
SQL Server,查询#1
Select
Min(BusinessDate) min_date,
Max(BusinessDate) max_date,
Count(Distinct BusinessDate) as working_days
From tbl_History
输出:最小日期=2018年7月13日,最大日期=2020年9月10日,工作日期=396
SQL Server查询#2
Select
Object_Name(t.object_id),
Min(lv.value) min_left_val,
Max(lv.value) max_left_val, -- only this matches with Query 1's max date
Min(rv.value) min_right_val, -- checked min value on RIGHT boundary
Max(rv.value) max_right_val, -- checked max value on RIGHT boundary
(DateDiff(dd, Cast(Min(lv.value) As Date), Cast(Max(lv.value) As Date)) + 1) - (DateDiff(wk, Cast(Min(lv.value) As Date), Cast(Max(lv.value) As Date)) *2) - (Case When DateName(dw, Cast(Min(lv.value) As Date)) = 'Sunday' Then 1 Else 0 End)
- (Case When DateName(dw, Cast(Min(lv.value) As Date)) = 'Saturday' Then 1 Else 0 End) As working_days -- this is off by a day which is ok
From
sys.partitions p
join
sys.allocation_units au on p.hobt_id = au.container_id
join
sys.indexes i on p.object_id = i.object_id and i.index_id = 1 and p.rows > 0
join
sys.tables t on t.object_id = i.object_id and t.object_id = Object_Id('tbl_History')
join
sys.partition_schemes s on i.data_space_id = s.data_space_id
join
sys.partition_functions f on s.function_id = f.function_id
left join
sys.partition_range_values rv on f.function_id = rv.function_id and p.partition_number = rv.boundary_id
left join
sys.partition_range_values lv on f.function_id = lv.function_id and p.partition_number - 1 = lv.boundary_id
group By
Object_Name(t.object_id)
输出:
object_name=tbl_History, min_lv=3/6/2019, max_lv=9/10/2020, min_rv=3/6/2019, max_rv=3/6/2019, working_days=397
--在我们的末尾,tbl_History是LEFT范围划分的,很明显,查询1和查询2之间的输出日期只在LEFT范围边界的最大值上一致。我调整了index_id(在集群和非集群之间(以及具有#>0,但帮助不大。
我想知道为什么以及这是否可以实现(即匹配最小值(。
由于实际的最小日期(2018-07-13(小于第一个边界(2019-03-06(,您在当前设置中无法尝试执行的操作。
为了实现你想要的目标,你需要为表格中的每个日期设置一个边界。使用RANGE LEFT
函数,您可以创建和维护一个检查约束,以确保没有小于第一个边界的行,但这将取决于您是否有没有间隙的分区边界。