查询包含简单功能时

时间:2019-04-05 13:19:19

标签: sql-server azure-sql-database database-partitioning

使用Azure SQL Server数据库。我有一些表在90天的日期范围内分区。我们有一个存储过程来移位数据,以保持适当的分区断点/范围。我正在使用一个小函数为查询提供正确的日期断点,因此我不必不断更新所有视图。

但是仅仅由于在查询中使用了该功能,分区就被忽略了。除了将硬编码的值放在我的查询中并不断修改它们之外,我别无选择吗?

这里是重现问题的样本。

更新:根据标记的答案更改了以下PartitionDate函数之后,短时间内就可以了(发生分区消除)。然后,查询又开始变得糟透了。当我运行由date函数过滤的简单查询时,不再消除分区。

------------------------------- setup
-- Create functions PartitionDate and PartitionQueryDate
create function PartitionDate() returns date as
begin
  return GETDATE() - 91 -- returns 1/4/2019 today
end
go
create function PartitionQueryDate() returns date as
begin
  return GETDATE() - 90 -- returns 1/5/2019
end
go

-- Create partition func and scheme using above functions
CREATE PARTITION FUNCTION order_pf (smalldatetime) AS RANGE RIGHT FOR VALUES (dbo.PartitionDate())
CREATE PARTITION SCHEME order_ps AS PARTITION order_pf ALL TO ([PRIMARY])

-- Create Order (pk, OrderDate, Fk), Customer (pk) tables.  Order is partitioned
create table Customer
(
    id int primary key identity(1,1), 
    FirstName varchar(255) not null
)
create table [Order]
(
    id int identity(1,1), OrderDate smalldatetime not null, 
    CustomerId int not null,
    CONSTRAINT [FK_Orders_Customer] FOREIGN KEY ([CustomerId]) REFERENCES Customer([id])
) on order_ps(OrderDate);

-- Add in indexes to Order: only OrderDate on the partition func
CREATE CLUSTERED INDEX [Order_OrderDate] ON [Order]([OrderDate] ASC) ON [order_ps] ([OrderDate]);
CREATE NONCLUSTERED INDEX [FK_Order_Customer] ON [Order](CustomerId, OrderDate) ON [order_ps] ([OrderDate]) -- seems to work the same with or without the partition reference.
go

-- Add some data before and after the partition break
insert Customer values ('bob')
insert [Order] values('12-31-2018', SCOPE_IDENTITY())
insert Customer values ('hank')
insert [Order] values('1-6-2019', SCOPE_IDENTITY())

---------------------------- test
-- verify a row per partition:
SELECT $PARTITION.order_pf(OrderDate) as Partition_Number, COUNT(*) as Row_Count 
FROM [Order]
GROUP BY $PARTITION.order_pf(OrderDate)

-- Simple queries with actual execution plan turned on.  The queries are logically equivalent.
select COUNT(1) from [Order] where OrderDate > '1-5-2019'   -- Index seek Order_OrderDate; actual partition count 1
select COUNT(1) from [Order] where OrderDate > dbo.PartitionQueryDate() -- Index seek Order_OrderDate; actual partition count 2

-- Cleanup
drop table if exists [Order]
drop table if exists Customer
drop partition scheme order_ps
drop partition function order_pf
drop function if exists PartitionDate
drop function if exists PartitionQueryDate

1 个答案:

答案 0 :(得分:0)

一种解决方法是先将函数结果分配给变量。

declare @pqd smalldatetime = dbo.PartitionQueryDate();

select COUNT(1) from [Order] where OrderDate > @pqd

另一种选择是使用嵌入式TVF

CREATE FUNCTION dbo.PartitionQueryDateTVF ()
RETURNS TABLE 
AS
RETURN 
(
    SELECT CAST(CAST( GETDATE() - 90 AS  DATE) AS SMALLDATETIME) AS Date
)

GO

SELECT COUNT(1) from [Order] where OrderDate > (SELECT Date FROM dbo.PartitionQueryDateTVF())

这可能是内联标量UDF所改进的,但目前我无法对此进行测试