简体   繁体   English

优化SQL表的性能-索引

[英]Optimize performance on SQL table - indexing

I have a table in my DB which contains 5 million records: 我的数据库中有一个表,其中包含500万条记录:

CREATE TABLE [dbo].[PurchaseFact](
    [Branch] [int] NOT NULL,
    [ProdAnal] [varchar](30) NULL,
    [Account] [varchar](12) NULL,
    [Partno] [varchar](24) NULL,
    [DteGRN] [date] NULL,
    [DteAct] [date] NULL,
    [DteExpect] [date] NULL,
    [OrderNo] [bigint] NULL,
    [GRNNO] [varchar](75) NULL,
    [SuppAdv] [varchar](75) NULL,
    [Supplier] [varchar](12) NULL,
    [OrdType] [varchar](4) NULL,
    [UnitStock] [varchar](4) NULL,
    [OrderQty] [float] NULL,
    [RecdQty] [float] NULL,
    [Batch] [varchar](100) NULL,
    [CostPr] [float] NULL,
    [Reason] [varchar](2) NULL,
    [TotalCost] [float] NULL,
    [Magic] [bigint] IDENTITY(1,1) NOT NULL,
PRIMARY KEY CLUSTERED 
(
    [Magic] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

As you can see from the above - a CLUSTERED INDEX is being used on the MAGIC column which is a UNIQUE column. 从上面可以看到,在MAGIC列( UNIQUE列)上使用了CLUSTERED INDEX

Data retrieval time for the following SELECT statement is well over 8mins which causes reporting issues: 以下SELECT statement数据检索时间超过8分钟,这会导致报告问题:

SELECT Branch,
    Supplier,
    ProdAnal,
    DteGRN AS Date,
    PartNo AS Partno,
    OrderNo,
    OrderQty,
    TotalCost,
    CostPr
FROM dbo.PurchaseFact src
WHERE YEAR(DteGRN) = 2016

Excluding the WHERE clause also doesn't make the query run any faster. 排除WHERE clause也不会使查询运行得更快。 I have tried, together with the CLUSTERED index to include a UNIQUE index in the hopes that it would run faster but to no avail : 我曾尝试与CLUSTERED index一起包含UNIQUE index ,希望它运行得更快但无济于事:

CREATE UNIQUE INDEX Unique_Index ON dbo.PurchaseFact ([Branch], [Supplier], [Magic]) 
INCLUDE ([ProdAnal], [Account], [Partno], [DteAct], [DteExpect], [OrderNo], [GRNNO], 
[SuppAdv], [OrdType], [UnitStock])

Is there any way I can optimize performance time on this table or should I resort to archiving old data? 有什么方法可以优化此表的性能时间,还是应该归档旧数据?

Any advice would be greatly appreciated. 任何建议将不胜感激。

This is your where clause: 这是您的where子句:

WHERE YEAR(DteGRN) = 2016

If the table has 5 million rows, then this is going to return a lot of data, assuming any reasonable distribution of dates. 如果表中有500万行,则假定日期的合理分布,这将返回很多数据。 The volume of data is probably responsible for the length of time for the query. 数据量可能负责查询的时间长度。

One thing you can do is to rephrase the WHERE and then put an index on the appropriate column: 您可以做的一件事是改写WHERE ,然后将索引放在适当的列上:

WHERE DteGRN >= '2016-01-01' and DteGRN < '2017-01-01'

This can then take advantage of an index on PurchaseFact(DteGRN) . 然后,这可以利用PurchaseFact(DteGRN)上的索引。 However, given the likely number of rows being returned, the index probably will not help very much. 但是,考虑到可能返回的行数,索引可能不会有太大帮助。

The bigger question is why your reporting application is bringing back all the rows from 2016, rather than summarizing them inside the database. 更大的问题是,为什么您的报表应用程序要带回2016年以来的所有行,而不是将其汇总到数据库中。 I suspect you have an architecture issue with the reporting application. 我怀疑您的报表应用程序存在体系结构问题。

Sorry, can't add comments. 抱歉,无法添加评论。

To help improve performance further (if you can live with the UPDATE overhead), create a COVERING INDEX that INCLUDES only the columns in the SELECT part of the query. 为了帮助进一步提高性能(如果您可以忍受UPDATE开销),请创建一个仅包含查询的SELECT部分​​中的列的COVERING INDEX。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM