SQL GROUP BY:获取索引视图最近更新的记录

发布于 2024-07-27 04:46:50 字数 1448 浏览 7 评论 0原文

我正在尝试使用索引视图进行行版本控制,按记录的键和时间戳对记录进行分组,并获取最大(时间戳)记录。 这很好,但是我使用的查询(请参见下面的视图)执行自连接,这意味着它不能在索引视图中使用,我认为这对性能至关重要。 有没有办法重写查询,以便可以使用 SCHEMABINDING 成功创建视图?

我使用的是 2005 年,但仅 2008 年的解决方案也可以。

IF EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[dbo].[Items]'))
    DROP VIEW [dbo].[Items]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[ItemHistory]') AND type in (N'U'))
    DROP TABLE [dbo].[ItemHistory]
GO
CREATE TABLE [dbo].[ItemHistory](
    [Key] [nchar](10) NOT NULL,
    [Value] [int] NOT NULL,
    [TimeStamp] Timestamp NOT NULL,
    [LastUpdateBy] [varchar](50) NOT NULL CONSTRAINT [DF_ItemHistory_LastUpdateBy]  DEFAULT (SUSER_NAME()),
    [Deleted] BIT NOT NULL DEFAULT (0)
    CONSTRAINT [PK_ItemHistory] PRIMARY KEY CLUSTERED 
    (
        [Key] ASC,
        [TimeStamp] ASC
    ) ON [PRIMARY]
)
GO
CREATE VIEW dbo.Items
--WITH SCHEMABINDING --doesnt work with the query below :(
AS
    SELECT ih.[key], ih.[Value] FROM ItemHistory ih
    INNER JOIN (
        SELECT [Key], Max([TimeStamp]) [TimeStamp]
        FROM ItemHistory
        GROUP BY [Key]
    ) ih2 ON ih.[key] = ih2.[key] AND ih.[TimeStamp] = ih2.[TimeStamp] AND Deleted = 0
GO 
INSERT INTO Items ([Key], [Value]) VALUES ('ItemA', 1)
INSERT INTO Items ([Key], [Value]) VALUES ('ItemA', 2)
INSERT INTO Items ([Key], [Value]) VALUES ('ItemA', 3)

GO
SELECT * FROM ItemHistory
SELECT * FROM Items

I'm trying to do row versioning using an indexed view, grouping records by their key and timestamp, and taking the max(timestamp) record. This is fine, but the query I have used (see the view below) does a self join, meaning it can't be used in an indexed view, which i think will be essential to performance. Is there a way to rewrite the query so the view can be created successfully WITH SCHEMABINDING?

I'm using 2005, but a 2008 only solution would be fine too.

IF EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[dbo].[Items]'))
    DROP VIEW [dbo].[Items]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[ItemHistory]') AND type in (N'U'))
    DROP TABLE [dbo].[ItemHistory]
GO
CREATE TABLE [dbo].[ItemHistory](
    [Key] [nchar](10) NOT NULL,
    [Value] [int] NOT NULL,
    [TimeStamp] Timestamp NOT NULL,
    [LastUpdateBy] [varchar](50) NOT NULL CONSTRAINT [DF_ItemHistory_LastUpdateBy]  DEFAULT (SUSER_NAME()),
    [Deleted] BIT NOT NULL DEFAULT (0)
    CONSTRAINT [PK_ItemHistory] PRIMARY KEY CLUSTERED 
    (
        [Key] ASC,
        [TimeStamp] ASC
    ) ON [PRIMARY]
)
GO
CREATE VIEW dbo.Items
--WITH SCHEMABINDING --doesnt work with the query below :(
AS
    SELECT ih.[key], ih.[Value] FROM ItemHistory ih
    INNER JOIN (
        SELECT [Key], Max([TimeStamp]) [TimeStamp]
        FROM ItemHistory
        GROUP BY [Key]
    ) ih2 ON ih.[key] = ih2.[key] AND ih.[TimeStamp] = ih2.[TimeStamp] AND Deleted = 0
GO 
INSERT INTO Items ([Key], [Value]) VALUES ('ItemA', 1)
INSERT INTO Items ([Key], [Value]) VALUES ('ItemA', 2)
INSERT INTO Items ([Key], [Value]) VALUES ('ItemA', 3)

GO
SELECT * FROM ItemHistory
SELECT * FROM Items

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

债姬 2024-08-03 04:46:50

如果您在 Items 视图定义中将 ItemHistory 替换为 dbo.ItemHistory ,它应该可以工作。

此外,您可能会发现以下查询性能更好,因为它避免使用 MAX 函数。

CREATE VIEW dbo.Items
WITH SCHEMABINDING
AS
    SELECT ih.[key], ih.[Value] FROM dbo.ItemHistory ih
    WHERE NOT EXISTS (SELECT [Key]
        FROM dbo.ItemHistory AS ih2
        WHERE ih.[key] = ih2.[key] AND ih.[TimeStamp] < ih2.[TimeStamp]) AND Deleted = 0
GO 

If you replace ItemHistory with dbo.ItemHistory in the Items view definition it should work.

Also you might find the following query performs better as it avoid the use of the MAX function.

CREATE VIEW dbo.Items
WITH SCHEMABINDING
AS
    SELECT ih.[key], ih.[Value] FROM dbo.ItemHistory ih
    WHERE NOT EXISTS (SELECT [Key]
        FROM dbo.ItemHistory AS ih2
        WHERE ih.[key] = ih2.[key] AND ih.[TimeStamp] < ih2.[TimeStamp]) AND Deleted = 0
GO 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文