基数估计问题?日期时间参数vs演员?

时间:2011-05-04 23:54:42

标签: .net vb.net sql-server-2005

我在下面发布的查询中搜索性能不佳时遇到了问题。

这是我的问题:

如果我在sql server managment studio中运行下面的确切查询或使用.NET sqlclient,则查询平均需要14秒才能运行。

但是,如果在.NET中,我会取出这部分查询:

DECLARE @time_diff INT
DECLARE @start_date DATETIME
DECLARE @end_date DATETIME

SET @time_diff = 2
SET @start_date = '05/04/11 00:00:00 AM'
SET @end_date = '5/4/2011 11:59:59 PM'

并在代码中使用参数:

sqlParameter = sqlCmd.Parameters.Add(New SqlParameter("@start_date", System.Data.SqlDbType.DateTime))
sqlParameter.Value = parameters.StartDate
sqlParameter = sqlCmd.Parameters.Add(New SqlParameter("@end_date", System.Data.SqlDbType.DateTime))
sqlParameter.Value = parameters.EndDate

然后查询大约需要2到3分钟才能运行。


另外,我注意到运行大约需要2-3分钟,如果我用字符串日期常量替换参数值并在.NET或SSMS中运行

--AND v.call_start_time BETWEEN @start_date AND @end_date
AND v.call_start_time BETWEEN '05/04/11' AND '5/4/2011 11:59:59 PM'

我在基数和查询优化器上发现了这篇文章http://msdn.microsoft.com/en-us/library/ms175933%28SQL.90%29.aspx,我认为它与我的问题有关但在我的情况下似乎没有意义。 根据这篇文章,在确定基数时应该执行强制转换。在我看来,这似乎并没有发生。文章还说使用参数代替局部变量 - 这也不适用于.NET参数。

关于使用存储过程: 我有一个可变数量的参数,我不知道它会有多好用。即使这是唯一的选择,那么我仍然想知道问题到底是什么。

查询:

DBCC DROPCLEANBUFFERS
DBCC FREEPROCCACHE

SET STATISTICS IO ON

DECLARE @time_diff INT
DECLARE @start_date DATETIME
DECLARE @end_date DATETIME

SET @time_diff = 2
SET @start_date = '05/04/11 00:00:00 AM'
SET @end_date = '5/4/2011 11:59:59 PM'

--INSERT QUERY
SELECT * INTO ##tmp_15 FROM (-- PRI CALL RECORDINGS SEARCH QUERY:
    SELECT DISTINCT
    v.call_recording_id,
    v.call_start_time,
    v.call_source,
    v.call_type,
    IsNull(v.phone, '') AS phone,
    d.call_duration_seconds AS pri_call_duration_seconds,
    IsNull(cdr.extension, 'N/A') AS pri_extension,
    IsNull(users.last_name, 'N/A') AS pri_last_name,
    IsNull(users.first_name, 'N/A') AS pri_first_name,
    NULL AS debtor_no,
    NULL AS d_extension,
    NULL AS d_last_name,
    NULL AS d_first_name
    FROM tbl_call_recordings AS v
    JOIN tbl_pri_call_details AS d ON v.call_recording_id = d.call_recording_id
    LEFT JOIN (
        SELECT extension, phone, call_start_time
        FROM tbl_pri_cdr_records
        WHERE is_discard = 0
    ) AS cdr ON v.phone = cdr.phone AND
        ABS(DATEDIFF(mi, v.call_start_time, cdr.call_start_time)) <= @time_diff

    -- MATCH RECORDS TO USER INFO VIA EXTENSION
     LEFT JOIN (
        SELECT extension,
               start_date,
               IsNull(end_date, GETDATE()) AS end_date,
               usr.user_id,
               last_name,
               first_name,
              cr_user_id
        FROM tbl_extensions AS ext
        JOIN tbl_extension_users AS ext_usr ON ext.id = ext_usr.extension_id
        JOIN tbl_users AS usr ON ext_usr.user_id = usr.user_id
    ) AS users ON cdr.extension = users.extension
    AND v.call_start_time BETWEEN users.start_date AND users.end_date
    WHERE 1 = 1

    -- INSERT PRI SEARCH CONSTRAINTS HERE:
    AND v.call_start_time BETWEEN @start_date AND @end_date
    --AND v.call_start_time BETWEEN '05/04/11' AND '5/4/2011 11:59:59 PM'

UNION

-- DIALER RECORDINGS SEARCH QUERY:
      SELECT
        v.call_recording_id,
        v.call_start_time,
        v.call_source,
        v.call_type,
        IsNull(v.phone, '') AS phone,
        NULL AS pri_call_duration_seconds,
        NULL AS pri_extension,
        NULL AS pri_last_name,
        NULL AS pri_first_name,
       d.debtor_no,
        IsNull(users.extension, 'N/A') AS extension,
        IsNull(users.last_name, 'N/A') AS last_name,
        IsNull(users.first_name, 'N/A') AS first_name
      FROM tbl_call_recordings AS v
      JOIN tbl_dialer_call_details AS d ON v.call_recording_id = d.call_recording_id

      -- MATCH RECORDS TO USER INFO VIA EXTENSION
      LEFT JOIN (
                SELECT extension,
                       start_date,
                       IsNull(end_date, GETDATE()) AS end_date,
                       last_name,
                       first_name,
                       cr_user_id
                FROM tbl_extensions AS ext
                JOIN tbl_extension_users AS ext_usr ON ext.id = ext_usr.extension_id
                JOIN tbl_users AS usr ON ext_usr.user_id = usr.user_id

      ) as users ON d.agent = users.cr_user_id
        AND v.call_start_time BETWEEN users.start_date AND users.end_date
      WHERE 1 = 1
      -- INSERT DIALER SEARCH CONSTRAINTS HERE:
    AND v.call_start_time BETWEEN @start_date AND @end_date
    --AND v.call_start_time BETWEEN '05/04/11' AND '5/5/2011'
)t

1 个答案:

答案 0 :(得分:1)

自上次更新统计数据以来,对数据的更改似乎可能对当前考虑范围内的范围产生了不成比例的影响,这意味着统计数据目前正在误导,需要手动更新。

A good article discussing this issue is here.