日期关联的优化不会改变计划

时间:2016-02-26 21:15:14

标签: sql-server optimization sql-server-2005

我有来自下表的报告要求。我使用这些表创建了一个新数据库,并从实时数据库导入了数据以用于报告目的。

报告参数是日期范围。我阅读了以下内容,发现DATE_CORRELATION_OPTIMIZATION可以通过使用seek而不是scan来更快地查询。我做了必要的设置 - 仍然是查询使用相同的旧计划和相同的执行时间。需要进行哪些其他更改才能使查询使用日期关联?

注意:我使用的是SQL Server 2005

参考

  1. Optimizing Queries That Access Correlated datetime Columns
  2. The Query Optimizer: Date Correlation Optimisation
  3. SQL

    --Database change made for date correlation
    ALTER DATABASE BISourcingTest
       SET DATE_CORRELATION_OPTIMIZATION ON;
    GO
    
    --Settings made
    SET ANSI_NULLS ON
    SET ANSI_PADDING ON
    SET ANSI_WARNINGS ON
    SET ARITHABORT ON
    SET CONCAT_NULL_YIELDS_NULL ON
    SET QUOTED_IDENTIFIER ON
    SET NUMERIC_ROUNDABORT OFF
    GO
    
    --Test Setting
    IF (  (sessionproperty('ANSI_NULLS') = 1) AND
          (sessionproperty('ANSI_PADDING') = 1) AND 
          (sessionproperty('ANSI_WARNINGS') = 1) AND 
          (sessionproperty('ARITHABORT') = 1) AND 
          (sessionproperty('CONCAT_NULL_YIELDS_NULL') = 1) AND 
          (sessionproperty('QUOTED_IDENTIFIER') = 1) AND 
          (sessionproperty('NUMERIC_ROUNDABORT') = 0)  
        )
       PRINT 'Everything is set'
    ELSE
       PRINT 'Different Setting'
    
    --Query
    SELECT C.ContainerID, C.CreatedOnDate,OLIC.OrderID
    FROM ContainersTest C
    INNER JOIN OrderLineItemContainers OLIC
        ON OLIC.ContainerID = C.ContainerID
    WHERE C.CreatedOnDate > '1/1/2015'
    AND C.CreatedOnDate < '2/01/2015'
    

    TABLES

    CREATE TABLE [dbo].[ContainersTest](
        [ContainerID] [varchar](20) NOT NULL,
        [Weight] [decimal](9, 2) NOT NULL DEFAULT ((0)),
        [CreatedOnDate] [datetime] NOT NULL DEFAULT (getdate()),
     CONSTRAINT [XPKContainersTest] PRIMARY KEY CLUSTERED 
    (
        [CreatedOnDate] ASC,
        [ContainerID] ASC
    )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
    ) ON [PRIMARY]
    
    GO
    
    CREATE TABLE [dbo].[OrderLineItemContainers](
        [OrderID] [int] NOT NULL,
        [LineItemID] [int] NOT NULL,
        [ContainerID] [varchar](20) NOT NULL,
        [CreatedOnDate] [datetime] NOT NULL DEFAULT (getdate()),
     CONSTRAINT [PK_POLineItemContainers] PRIMARY KEY CLUSTERED 
    (
        [OrderID] ASC,
        [LineItemID] ASC,
        [ContainerID] ASC
    )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY],
     CONSTRAINT [IX_OrderLineItemContainers] UNIQUE NONCLUSTERED 
    (
        [ContainerID] ASC
    )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
    ) ON [PRIMARY]
    
    GO
    SET ANSI_PADDING OFF
    GO
    ALTER TABLE [dbo].[OrderLineItemContainers]  WITH CHECK ADD  CONSTRAINT [FK_POLineItemContainers_Containers] FOREIGN KEY([ContainerID])
    REFERENCES [dbo].[Containers] ([ContainerID])
    GO
    ALTER TABLE [dbo].[OrderLineItemContainers] CHECK CONSTRAINT [FK_POLineItemContainers_Containers]
    

    计划 enter image description here

    -

1 个答案:

答案 0 :(得分:4)

根据文件: https://technet.microsoft.com/en-us/library/ms177416(v=sql.105).aspx

  

如果维护相关统计信息的任何一个日期时间列不是聚簇索引的第一个或唯一键,请考虑在其上创建聚簇索引。这样做通常可以提高相关统计信息覆盖的查询类型的性能。如果主键列上已存在聚簇索引,则可以修改表,以便聚簇索引和主键使用不同的列集。

由于OrderLineItemContainers表没有适合在Date上过滤的索引,因此它实际上无法执行任何操作。尝试在OrderLineItemContainers.CreatedOnDate上添加非聚集索引,以查看它是否会切换计划。

将它集群化会更好,但还有其他注意事项......请注意,如果这是主要查询,则可以使主键非聚簇,并使用clustered作为这个新的日期索引,这使得它值得它

所以这是最佳的:

CREATE TABLE [dbo].[OrderLineItemContainers](
      [OrderID] [int] NOT NULL,
      [LineItemID] [int] NOT NULL,
      [ContainerID] [varchar](20) NOT NULL,
      [CreatedOnDate] [datetime] NOT NULL DEFAULT (getdate()),
   CONSTRAINT [PK_POLineItemContainers] PRIMARY KEY NONCLUSTERED -- NONCLUSTERED PRIMARY KEY!!
        (
            [OrderID] ASC,
            [LineItemID] ASC,
            [ContainerID] ASC
        )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY],
         CONSTRAINT [IX_OrderLineItemContainers] UNIQUE NONCLUSTERED 
        (
            [ContainerID] ASC
        )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
        ) ON [PRIMARY]

CREATE CLUSTERED INDEX ON OrderLineItemContainers(CreatedOnDate)

或者您可以尝试新的NONCLUSTERED索引:

CREATE NONCLUSTERED INDEX ON OrderLineItemContainers(CreatedOnDate)