优化SQL查询以使用标记返回Record

时间:2015-03-20 00:11:19

标签: sql sql-server database query-optimization

我一直在寻找帮助来优化我为SQL Server编写的查询。鉴于此数据库架构:

TradeLead对象,此表中的记录是一篇小文章。

CREATE TABLE [dbo].[TradeLeads]
(
    [TradeLeadID] INT NOT NULL PRIMARY KEY IDENTITY(1,1),
    Title nvarchar(250),
    Body nvarchar(max),
    CreateDate datetime,
    EditDate datetime,
    CreateUser nvarchar(250),
    EditUser nvarchar(250), 
    [Views] INT NOT NULL DEFAULT(0)

)

以下是将TradeLead文章与行业记录相关联的交叉参考表。

CREATE TABLE [dbo].[TradeLeads_Industries]
(
    [ID] INT NOT NULL PRIMARY KEY IDENTITY(1,1), 
    [TradeLeadID] INT NOT NULL, 
    [IndustryID] INT NOT NULL
)

最后,Industry对象的架构。这些基本上只是标签,但用户无法输入这些标签。数据库将具有特定数量。

CREATE TABLE [dbo].[Industries]
(
    IndustryID INT NOT NULL PRIMARY KEY identity(1,1),
    Name nvarchar(200)
)

我写的程序用于搜索特定的TradeLead记录。用户可以在TradeLead对象的标题中搜索关键字,使用日期范围进行搜索,并搜索具有特定行业标签的TradeLead。

数据库很可能会持有大约1,000,000条TradeLead文章和大约30个行业标签。

这是我提出的查询:

DECLARE @Title nvarchar(50);
SET @Title = 'Testing';
-- User defined table type containing a list of IndustryIDs. Would prob have around 5 selections max.
DECLARE @Selectedindustryids IndustryIdentifierTable_UDT;
DECLARE @Start DATETIME;
SET @Start = NULL;
DECLARE @End DATETIME;
SET @End = NULL;


SELECT *
FROM(
-- Subquery to return all the tradeleads that match a user's criteria.
-- These fields can be null.
SELECT TradeLeadID, 
            Title, 
            Body, 
            CreateDate, 
            CreateUser, 
            Views
     FROM TradeLeads
     WHERE(@Title IS NULL OR Title LIKE '%' + @Title + '%') AND (@Start IS NULL OR CreateDate >= @Start) AND (@End IS NULL OR CreateDate <= @End)) AS FTL

    INNER JOIN
    -- Subquery to return the TradeLeadID for each TradeLead record with related IndustryIDs
    (SELECT TI.TradeLeadID
           FROM TradeLeads_Industries TI
           -- Left join the selected IndustryIDs to the Cross reference table to get the TradeLeadIDs that are associated with a specific industry.
           LEFT JOIN @SelectedindustryIDs SIDS
             ON SIDS.IndustryID = TI.IndustryID
           -- It's possible the user has not selected any IndustryIDs to search for.
           WHERE (NOT EXISTS(SELECT 1 FROM @SelectedIndustryIDs) OR SIDS.IndustryID IS NOT NULL)
           -- Group by to reduce the amount of records.
           GROUP BY TI.TradeLeadID) AS SelectedIndustries ON SelectedIndustries.TradeLeadID = FTL.TradeLeadID



       With about 600,000 TradeLead records and  with an average of 4 IndustryIDs attached to each one, the query takes around 8 seconds to finish on a local machine. I would like to get it as fast as possible. Any tips or insight would be appreciated.

1 个答案:

答案 0 :(得分:1)

这里有几点。

使用像(@Start IS NULL OR CreateDate >= @Start)这样的结构会导致称为参数嗅探的问题。解决它的两种方法是

  1. Option (Recompile)添加到查询的末尾
  2. 使用动态SQL仅包含用户要求的条件。
  3. 我赞成这个数据的第二种方法。

    接下来,通过使用exists(假设用户已输入行业ID),可以重写查询以提高效率

    select
        TradeLeadID, 
        Title, 
        Body, 
        CreateDate, 
        CreateUser, 
        [Views]
    from
        dbo.TradeLeads t
    where
        Title LIKE '%' + @Title + '%' and
        CreateDate >= @Start and
        CreateDate <= @End and
        exists (
            select
                'x'
            from
                dbo.TradeLeads_Industries ti
                    inner join
                @Selectedindustryids sids
                    on ti.IndustryID = sids.IndustryID
            where
                t.TradeLeadID = ti.TradeLeadID
        );
    

    最后,您需要dbo.TradeLeads_Industries表上至少有一个索引。以下是候选人。

    (TradeLeadID, IndustryID)
    (IndustryID, TradeLeadID)
    

    测试会告诉您一个或两个是否有用。