English 中文(简体)
2005年 过滤30 000 000 000 000个登记册的适当索引
原标题:SQL Server 2005 proper index to filter 30,000,000 registers

我对交易表格的存储程序有问题,用户有网络形式以几种价值找到交易。

这一进程耗时太长,我不知道如何制定适当的指数。

我储存的程序如下:

CREATE PROCEDURE dbo.cg_searchTransactions
(
    @id_Ent tinyint,
    @transactionTypeID int = NULL,
    @transactionID numeric(18,0) = NULL,
    @channelID int = NULL,
    @transactionDateFrom datetime = NULL,
    @transactionDateTo datetime = NULL,
    @transactionStatusID INT = NULL,
    @documentType INT = NULL,
    @documentNumber varchar(50) = NULL,
    @userName varchar(50) = NULL,
    @accountFromNumber varchar(20) = NULL,
    @accountToNumber varchar(20) = NULL,
    @amountFrom money = NULL,
    @amountTo money = NULL,
    @correlationID varchar(30) = NULL,
    @externalReference varchar(20) = NULL,
    @externalReference2 varchar(20) = NULL,
    @PageIndex INT = 1, 
    @PageSize INT = 20
)
AS
BEGIN
    SET NOCOUNT ON


        DECLARE @QUERY VARCHAR(MAX)
        SET @QUERY =  
            WITH Trans AS (
            SELECT
                ROW_NUMBER() OVER (ORDER BY transactionID DESC) AS Row,
                T.id_Ent,
                T.transactionID,
                T.trnTypeCurrencyID,
                T.transactionDate,
                T.transactionStatusID,
                T.documentType,
                T.documentNumber,
                T.childDocumentType,
                T.childDocumentNumber,
                T.userName,
                T.accountFromNumber,
                T.accountFromType,
                T.accountFromCurrency,
                T.accountDescriptionFrom,
                T.costCenterFrom,
                T.subtotalFrom,
                T.taxamountFrom,
                T.taxamountFrom2,
                T.amountFrom,
                T.accountToNumber,
                T.accountToType,
                T.accountToCurrency,
                T.accountDescriptionTo,
                T.costCenterTo,
                T.subtotalTo,
                T.taxamountTo,
                T.taxamountTo2,
                T.amountTo,
                T.exchangeCurrency,
                T.traderAuthNumber,
                T.benefContractNumber,
                T.contractNumber,
                T.merchantID,
                T.creditCardAuthorizationNumber,
                T.comment,
                T.companyServiceCommision,
                T.usercommission,
                T.companyServiceAuthorizationNumber,
                T.customerBranchId,
                T.correlationID,
                T.transactionStartTime,
                T.transactionEndTime,
                T.enlapsedTime,
                T.serverName,
                T.externalReference,
                T.externalReference2,
                T.externalTrxType,
                T.beneficiaryName,

                C.shortName AS ChannelsShortName,
                TT.shortName AS TransactionTypesShortName,
                TS.shortName AS TransactionStatusDefShortName,
                DT.shortName AS DocumentTypesShortName,
                CDT.shortName AS ChildDocumentTypesShortName,
                AFT.shortName AS AccountTypesShortNameFrom,
                ATT.shortName AS AccountTypesShortNameTo,
                CURF.shortName AS CurrenciesShortNameFrom,
                CURT.shortName AS CurrenciesShortNameTo
            FROM
                Transactions T (NOLOCK) 

                    INNER JOIN TransactionTypesCurrencies TTC
                        ON  T.id_Ent = TTC.id_Ent
                            AND T.trnTypeCurrencyID = TTC.trnTypeCurrencyID

                        INNER JOIN Channels C
                            ON  TTC.id_Ent = C.id_Ent
                                AND TTC.channelID = C.ID

                        INNER JOIN TransactionTypes TT
                            ON  TTC.id_Ent = TT.id_Ent
                                AND TTC.transactionTypeID = TT.transactionTypeID

                    INNER JOIN TransactionStatusDef TS
                        ON  T.id_Ent = TS.ent_Ent
                            AND T.transactionStatusID = TS.ID

                    INNER JOIN DocumentTypes DT
                        ON  T.id_Ent = DT.id_Ent
                            AND T.documentType = DT.ID

                    INNER JOIN DocumentTypes CDT
                        ON  T.id_Ent = CDT.id_Ent
                            AND T.childDocumentType = CDT.ID

                    INNER JOIN AccountTypes AFT
                        ON  T.id_Ent = AFT.id_Ent
                            AND T.accountFromType = AFT.ID

                    INNER JOIN AccountTypes ATT
                        ON  T.id_Ent = ATT.id_Ent
                            AND T.accountToType = ATT.ID

                    INNER JOIN Currencies CURF
                        ON  T.id_Ent = CURF.id_Ent
                            AND T.accountFromCurrency = CURF.ID

                    INNER JOIN Currencies CURT
                        ON  T.id_Ent = CURT.id_Ent
                            AND T.accountToCurrency = CURT.ID
            WHERE 
                T.id_Ent =   + CONVERT(VARCHAR,@id_Ent)
                IF NOT @transactionDateFrom IS NULL
                    SET @QUERY = @QUERY +   AND T.transactionDate >=     + CONVERT(VARCHAR,@transactionDateFrom,121) +     

                IF NOT @transactionDateTo IS NULL
                    SET @QUERY = @QUERY +   AND T.transactionDate <=     + CONVERT(VARCHAR,@transactionDateTo,121) +     

                IF NOT @transactionStatusID IS NULL
                    SET @QUERY = @QUERY +   AND T.transactionStatusID =   + CONVERT(VARCHAR,@transactionStatusID)

                IF NOT @documentType IS NULL
                    SET @QUERY = @QUERY +   AND T.documentType =   + CONVERT(VARCHAR,@documentType)

                IF NOT @userName IS NULL
                    SET @QUERY = @QUERY +   AND T.userName =     + @userName +     

                IF NOT @documentNumber IS NULL
                    SET @QUERY = @QUERY +   AND T.documentNumber =     + @documentNumber +     

                IF NOT @accountFromNumber IS NULL
                    SET @QUERY = @QUERY +   AND T.accountFromNumber =     + @accountFromNumber +     

                IF NOT @accountToNumber IS NULL
                    SET @QUERY = @QUERY +   AND T.accountToNumber =     + @accountToNumber +     

                IF NOT @amountFrom IS NULL
                    SET @QUERY = @QUERY +   AND T.amountTo >=   + CONVERT(VARCHAR,@amountFrom)

                IF NOT @amountTo IS NULL
                    SET @QUERY = @QUERY +   AND T.amountTo <=   + CONVERT(VARCHAR,@amountTo)

                IF NOT @correlationID IS NULL
                    SET @QUERY = @QUERY +   AND T.correlationID =     + @correlationID +     

                IF NOT @externalReference IS NULL
                    SET @QUERY = @QUERY +   AND T.externalReference =     + @externalReference +     

                IF NOT @externalReference2 IS NULL
                    SET @QUERY = @QUERY +   AND T.externalReference2 =     + @externalReference2 +     

                IF NOT @channelID IS NULL
                    SET @QUERY = @QUERY +   AND C.ID =   + CONVERT(VARCHAR,@channelID)

                IF NOT @transactionTypeID IS NULL
                    SET @QUERY = @QUERY +   AND TT.transactionTypeID =   + CONVERT(VARCHAR,@transactionTypeID)

            SET @QUERY = @QUERY +  ) 
            SET @QUERY = @QUERY +  SELECT * FROM Trans WHERE Row BETWEEN (  + CONVERT(VARCHAR,@PageIndex) +   - 1) *   + CONVERT(VARCHAR,@PageSize) +   + 1 AND   + CONVERT(VARCHAR,@PageIndex) +  *  + CONVERT(VARCHAR,@PageSize)

            SET @QUERY = @QUERY +  OPTION (FAST 1) 

            EXEC(@QUERY)

END
最佳回答

您仅需要就<代码>中的所有领域分别编制索引。 WHERE 条款,即<代码>transactionDate,transactionStatusID。 如果您有<条码>id_ent作为补充过滤器,则将其作为主要一栏:

CREATE INDEX ix_transaction_transactionDate ON transaction (id_ent, transactionDate)
CREATE INDEX ix_transaction_transactionStatusID ON transaction (id_ent, transactionStatusID)
-- etc.

请注意,每组只使用一个索引,<代码>SQL 服务器/代码>将尝试选择最合适的索引(最有选择的索引)。

还指出,在生产表上安装NOLOCK是一个非常坏的想法。 您可在一份查询上查阅。

如果<条码>id_ent 是<条码>的一部分。 在您的所有表格中,您最好以不变的方式取代。 问题:

SELECT  *
FROM    Transactions t
JOIN    TransactionTypesCurrencies ttc
ON      ttc.trnTypeCurrencyID = t.trnTypeCurrencyID 
WHERE   t.id_ent = @id_ent
        AND ttc.id_ent = @id_ent

通常比这好:

SELECT  *
FROM    Transactions t
JOIN    TransactionTypesCurrencies ttc
ON      ttc.id_ent = t.id_ent
        AND ttc.trnTypeCurrencyID = t.trnTypeCurrencyID 
WHERE   t.id_ent = @id_ent

由于可以进行早期过滤。

如果你只拥有<条码>id_ent的单一价值,那么,如果你会增加另一个数值,那不会有变化。

<>Update:

如果你一再提出质询,在不止一种条件下进行过滤,而且速度缓慢,你可以考虑在几个条件下增设综合指数。

在我对关于如何做到这一点的一些建议的意见的博客中,见这一条:

问题回答

通过利用简介员记录工作量,然后利用该工作量的指数调整,确定最能处理工作量的指数,你可以获得一些经验信息。

你们创造的指数越高,加起来的工作越多,就不必做些什么,因此,就所搜寻的所有事项制定指数可能不是一个好的想法。

我认为,在这种情况下,可以更快地形成一个温床而不是一个共同的表态。 这还使你能够回去总的数字。

我在我面前问我的问题,我提出一个指数:

  1. Is this table(or tables) going to be read only or read write ?

阅读标准——每当表上有一个更新/编辑,索引将更新。 插入、更新和删除“选择”可能很快。 引用MS “如果在表格上有大量索引,则会增加优化者选择排位指数的机会。

  1. 你正在对传单提出询问。 我将做些什么是试图在QA(或特定时间生产出行)上追踪,看看用户试图操作什么。 您可从生产中向您的沙箱/试验工具倾斜,如指数校正(能告诉你需要哪些指数)SQLDMVs

  2. 如果你有理由相信这个表是主人,你也应尝试横向分割表格。

  3. 如果你执行任何询问,看执行计划,看看桌扫描,通常意味着一些指数缺失。

  4. rel=“nofollow noreferer”





相关问题
SQL Server database is not visible

I have installed ASP.NET application with database on our server. ASP.NET application created database using connection string below. The problem is that I do not see database in SQL Server Management ...

Most efficient way to store number 11.111 in SQL Server

What datatype is the most efficient to store a number such as 11.111. The numbers will have up 2 digits before the point and up to three after. Currently the column is a bigint , I am guessing that ...

表格和数据表

是否需要在提瓜表之后更新表格统计数据,还是自动更新?

Inconsistent Generate Change Script

I add a column of type tinyint and being set to not allow nulls in a table and generate the change scripts. The table has data in it at this time. The script has code that creates a temp table and ...

Performance of Sql subqueriesfunctions

I am currently working on a particularly complex use-case. Simplifying below :) First, a client record has a many-to-one relationship with a collection of services, that is, a single client may have ...

selecting one value out of an xml column

I have a particularly troublesome xml column to query data from. The schema is fixed by the Quebec Ministry of Revenue so "it is what it is" The important part of the query looks like this: with ...

Selecting records during recursive stored procedure

I ve got a content management system that contains a hierarchical structure of categories, with sub-categories subject to different ordering options at each level. Currently, that s retrieved by a (...