使用行超过3000万的表优化SQL Server数据库中的查询

时间:2016-10-21 09:25:49

标签: sql sql-server sql-server-2008 database-optimization

在本地环境中,我有一个SQL Server数据库,其中一个表具有超过3000万行

This是表结构。

我已定义nvarchar(MAX),因为我不知道我要在表格中插入的数据的文字大小。

只需要COUNT个表行,就需要超过3分钟。是否有任何工具或查询要运行以优化表并使查询运行得更快?

PS:我正在开发四核,8Gb RAM,Windows 7虚拟机

修改

这是生成表格的脚本:

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[L_Customer]
(
    [LC_Id] [int] IDENTITY(1,1) NOT NULL,
    [A] [nvarchar](max) NULL,
    [B] [nvarchar](max) NULL,
    [C] [nvarchar](max) NULL,
    [D] [nvarchar](max) NULL,
    [E] [nvarchar](max) NULL,
    [F] [nvarchar](max) NULL,
    [G] [nvarchar](max) NULL,
    [H] [float] NULL,
    [I] [float] NULL,
    [L] [nvarchar](max) NULL,
    [M] [nvarchar](max) NULL,
    [N] [nvarchar](max) NULL,
    [O] [nvarchar](max) NULL,
    [P] [nvarchar](max) NULL,
    [Q] [nvarchar](max) NULL,
    [W] [int] NULL,

    CONSTRAINT [PK_L_Customer] 
       PRIMARY KEY CLUSTERED ([LC_Id] 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

(那些不是真正的列名)

查询很简单:

SELECT COUNT(LC_Id) 
FROM dbo.L_Customer

完成了3分10秒,这就是它的执行计划:

<?xml version="1.0" encoding="utf-16"?>
<ShowPlanXML xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" Version="1.1" Build="10.50.1600.1" xmlns="http://schemas.microsoft.com/sqlserver/2004/07/showplan">
  <BatchSequence>
    <Batch>
      <Statements>
        <StmtSimple StatementCompId="1" StatementEstRows="1" StatementId="1" StatementOptmLevel="FULL" StatementSubTreeCost="806.254" StatementText="select count(LC_Id) from Anagrafiche_2.dbo.L_Customer&#xD;&#xA;" StatementType="SELECT" QueryHash="0x859AA6DAAAAF58AD" QueryPlanHash="0xBF9F762C316AEE73">
          <StatementSetOptions ANSI_NULLS="true" ANSI_PADDING="true" ANSI_WARNINGS="true" ARITHABORT="true" CONCAT_NULL_YIELDS_NULL="true" NUMERIC_ROUNDABORT="false" QUOTED_IDENTIFIER="true" />
          <QueryPlan DegreeOfParallelism="0" CachedPlanSize="16" CompileTime="1" CompileCPU="1" CompileMemory="160">
            <RelOp AvgRowSize="11" EstimateCPU="18.1674" EstimateIO="0" EstimateRebinds="0" EstimateRewinds="0" EstimateRows="1" LogicalOp="Compute Scalar" NodeId="0" Parallel="false" PhysicalOp="Compute Scalar" EstimatedTotalSubtreeCost="806.254">
              <OutputList>
                <ColumnReference Column="Expr1003" />
              </OutputList>
              <ComputeScalar>
                <DefinedValues>
                  <DefinedValue>
                    <ColumnReference Column="Expr1003" />
                    <ScalarOperator ScalarString="CONVERT_IMPLICIT(int,[Expr1006],0)">
                      <Convert DataType="int" Style="0" Implicit="true">
                        <ScalarOperator>
                          <Identifier>
                            <ColumnReference Column="Expr1006" />
                          </Identifier>
                        </ScalarOperator>
                      </Convert>
                    </ScalarOperator>
                  </DefinedValue>
                </DefinedValues>
                <RelOp AvgRowSize="11" EstimateCPU="18.1674" EstimateIO="0" EstimateRebinds="0" EstimateRewinds="0" EstimateRows="1" LogicalOp="Aggregate" NodeId="1" Parallel="false" PhysicalOp="Stream Aggregate" EstimatedTotalSubtreeCost="806.254">
                  <OutputList>
                    <ColumnReference Column="Expr1006" />
                  </OutputList>
                  <RunTimeInformation>
                    <RunTimeCountersPerThread Thread="0" ActualRows="1" ActualEndOfScans="1" ActualExecutions="1" />
                  </RunTimeInformation>
                  <StreamAggregate>
                    <DefinedValues>
                      <DefinedValue>
                        <ColumnReference Column="Expr1006" />
                        <ScalarOperator ScalarString="Count(*)">
                          <Aggregate AggType="countstar" Distinct="false" />
                        </ScalarOperator>
                      </DefinedValue>
                    </DefinedValues>
                    <RelOp AvgRowSize="9" EstimateCPU="33.307" EstimateIO="754.779" EstimateRebinds="0" EstimateRewinds="0" EstimateRows="30279000" LogicalOp="Clustered Index Scan" NodeId="2" Parallel="false" PhysicalOp="Clustered Index Scan" EstimatedTotalSubtreeCost="788.086" TableCardinality="30279000">
                      <OutputList />
                      <RunTimeInformation>
                        <RunTimeCountersPerThread Thread="0" ActualRows="30278956" ActualEndOfScans="1" ActualExecutions="1" />
                      </RunTimeInformation>
                      <IndexScan Ordered="false" ForcedIndex="false" NoExpandHint="false">
                        <DefinedValues />
                        <Object Database="[Anagrafiche_2]" Schema="[dbo]" Table="[L_Customer]" Index="[PK_L_Customer]" IndexKind="Clustered" />
                      </IndexScan>
                    </RelOp>
                  </StreamAggregate>
                </RelOp>
              </ComputeScalar>
            </RelOp>
          </QueryPlan>
        </StmtSimple>
      </Statements>
    </Batch>
  </BatchSequence>
</ShowPlanXML>

0 个答案:

没有答案