-3

我在数据库中有两个表:tb_CampaignLeadtb_Feed. 这两个表都有数千行,并且在主键上各有一个索引。tb_CampaignLead比 多 20% 的数据tb_Feed。仍然tb_Feedtb_CampaignLeada慢 6 倍SELECT *。谁能告诉我为什么tb_Feed这么慢。表结构如下:

CREATE TABLE [dbo].[tb_Feed](
    [FeedID] [uniqueidentifier] NOT NULL,
    [DateCreated] [smalldatetime] NULL,
    [LeadSourceID] [int] NULL,
    [RawLeadURL] [nvarchar](max) NULL,
    [CostPerEnquiry] [money] NULL,
    [ResultText] [nvarchar](max) NULL,
    [SrcResultText] [nvarchar](max) NULL,
    [SrcResult] [bit] NULL,
    [Encrypted] [bit] NULL,
    [ProductID] [int] NULL,
 CONSTRAINT [PK_tb_Feed] PRIMARY KEY CLUSTERED 


CREATE TABLE [dbo].[tb_CampaignLead](
    [LeadID] [uniqueidentifier] NOT NULL,
    [FeedID] [uniqueidentifier] NOT NULL,
    [CampaignID] [int] NOT NULL,
    [ProductID] [int] NOT NULL,
    [CompanyID] [int] NOT NULL,
    [LeadSourceID] [int] NOT NULL,
    [DateCreated] [smalldatetime] NULL,
    [LeadTextEmail] [nvarchar](max) NULL,
    [LeadTextOther] [nvarchar](max) NULL,
    [DateSent] [smalldatetime] NULL,
    [EmailResponse] [nvarchar](max) NULL,
    [OtherResponse] [nvarchar](max) NULL,
    [EmailOK] [bit] NULL,
    [OtherOK] [bit] NULL,
    [ResultPass] [bit] NULL,
    [L_Title] [nvarchar](50) NULL,
    [L_Email] [nvarchar](100) NULL,
    [L_Firstname] [nvarchar](max) NULL,
    [L_Surname] [nvarchar](max) NULL,
    [L_Address1] [nvarchar](max) NULL,
    [L_Address2] [nvarchar](max) NULL,
    [L_Address3] [nvarchar](max) NULL,
    [L_TownCity] [nvarchar](max) NULL,
    [L_AreaRegion] [nvarchar](max) NULL,
    [L_Country] [nvarchar](max) NULL,
    [L_PostCode] [nvarchar](50) NULL,
    [L_Telephone1] [nvarchar](50) NULL,
    [L_Telephone2] [nvarchar](50) NULL,
    [L_DOB] [smalldatetime] NULL,
    [L_Extra1] [nvarchar](max) NULL,
    [L_Extra2] [nvarchar](max) NULL,
    [L_Extra3] [nvarchar](max) NULL,
    [L_Extra4] [nvarchar](max) NULL,
    [L_Extra5] [nvarchar](max) NULL,
    [L_Extra6] [nvarchar](max) NULL,
    [L_Extra7] [nvarchar](max) NULL,
    [L_Extra8] [nvarchar](max) NULL,
    [L_Extra9] [nvarchar](max) NULL,
    [L_Extra10] [nvarchar](max) NULL,
    [L_Extra11] [nvarchar](max) NULL,
    [L_Extra12] [nvarchar](max) NULL,
    [L_Extra13] [nvarchar](max) NULL,
    [L_Extra14] [nvarchar](max) NULL,
    [L_Extra15] [nvarchar](max) NULL,
    [L_Extra16] [nvarchar](max) NULL,
    [L_Extra17] [nvarchar](max) NULL,
    [L_Extra18] [nvarchar](max) NULL,
    [L_Extra19] [nvarchar](max) NULL,
    [L_Extra20] [nvarchar](max) NULL,
    [SourceCost] [money] NULL,
    [CampaignCost] [money] NULL,
    [DeliveredPass] [bit] NULL,
    [FieldReqBWAND] [int] NULL,
    [FieldSuppliedBWAND] [int] NULL,
    [FilterBWAND] [int] NULL,
    [FilterPassBWAND] [int] NULL,
    [OPFilterBWAND] [int] NULL,
    [OPFilterPassBWAND] [int] NULL,
    [ProcessBWAND] [int] NULL,
    [ProcessPassBWAND] [int] NULL,
    [L_MobileNetwork] [nvarchar](max) NULL,
    [SrcResultPass] [bit] NULL,
 CONSTRAINT [PK_tb_CampaignLead] PRIMARY KEY CLUSTERED 
4

1 回答 1

2

使用您评论中的这些信息:

Campaignlead 表的数据空间为 734,83​​2,供稿表的数据空间为 2,595,792

如您所见,尽管行数较少,但 tb_Feed 的数据总量是 tb_CampaignLead 的 3.5 倍多。当您SELECT * From ...在 SSMS 中执行此操作时,它实际上必须检索那么多数据、对其进行格式化、通过网络连接推送它(如果您从客户端运行),然后将其渲染到输出网格中。

对于小型数据集,这非常快,以至于它不是查询执行时间的重要部分。另外,它可以被缓冲以使其看起来更快。但是,对于大型数据集,这可能需要相当长的时间,并且输出缓冲区/管道将开始备份。这不仅可以减慢查询执行时间,它实际上可以成为其主导因素。

因此,对于 SSMS 中这种大小的查询,我们实际上预计tb_Feed需要比tb_CampaignLead. 因此,虽然不完全相同,但您的结果肯定是在正确的范围内。

这些表中还有其他可能导致额外差异的内容,包括NVARCHAR(MAX)每个表“列外”存储的数据量。这是另一个对此有一些答案的问题:我应该使用内联 varchar(max) 列还是将其存储在单独的表中?

于 2013-06-12T19:57:57.550 回答