简体   繁体   English

从SQL Server 2005中的单个表进行数据访问太慢

[英]Data Access from single table in sql server 2005 is too slow

Following is the script of table. 以下是表的脚本。 Accessing data from this table is too slow. 从该表访问数据太慢。

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Emails](
    [id] [int] IDENTITY(1,1) NOT NULL,
    [datecreated] [datetime] NULL CONSTRAINT [DF_Emails_datecreated]  
        DEFAULT (getdate()),
    [UID] [nvarchar](250) COLLATE Latin1_General_CI_AS NULL,
    [From] [nvarchar](100) COLLATE Latin1_General_CI_AS NULL,
    [To] [nvarchar](100) COLLATE Latin1_General_CI_AS NULL,
    [Subject] [nvarchar](max) COLLATE Latin1_General_CI_AS NULL,
    [Body] [nvarchar](max) COLLATE Latin1_General_CI_AS NULL,
    [HTML] [nvarchar](max) COLLATE Latin1_General_CI_AS NULL,
    [AttachmentCount] [int] NULL,
    [Dated] [datetime] NULL
) ON [PRIMARY]

Following query takes 50 seconds to fetch data. 以下查询需要50秒钟来获取数据。

select id, datecreated, UID, [From], [To], Subject, AttachmentCount, 
    Dated from emails

If I include Body and Html in select then time is event worse. 如果我在选择中包括“ 身体”和“ HTML” ,那么时间会更糟。

indexes are on: 索引在:

  • id unique clustered id唯一聚集
  • From Non unique non clustered 来自非唯一非聚集
  • To Non unique non clustered 到非唯一非聚集

Tabls has currently 180000+ records. Tabls当前拥有180000多个记录。

There might be 100,000 records each month so this will become more slow as time will pass. 每个月可能有100,000条记录,所以随着时间的流逝,它将变得越来越慢。

Does splitting data into two table will solve the problem? 将数据分成两个表是否可以解决问题? What other indexes should be there? 还有哪些其他索引?

It's almost certainly the volume of the data that's causing a problem. 几乎可以肯定,引起问题的数据量很大。 Because of this, you should not fetch the Subject column until you are need it. 因此,除非需要,否则不要获取“ Subject列。 Even fetching SUBSTRING(Subject, 100) may be noticeably faster. 即使获取SUBSTRING(Subject, 100)可能也明显更快。

This may be irrelevant, but older versions of SQL Server suffered if the BLOB columns weren't the last in the row, so just as an experiment I'd move [AttachmentCount] and [Dated] above the three nvarchar(max) columns. 这可能无关紧要,但是如果BLOB列不是该行的最后一行,则SQL Server的较旧版本会受到影响,因此,就像一个实验一样,我将[AttachmentCount][Dated]移到了三个nvarchar(max)列的上方。

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

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