【发布时间】:2016-02-18 07:39:59
【问题描述】:
我使用的是 Microsoft SQL Server 企业版(64 位)。
我的查询执行时间大约是 1 分钟。
-
Docum表有 xxxxxxxx 行 -
Pers表有 xxxxxxx 行 -
Permarks表有 xxxxxx 行
Docum 表上的索引:
Pers 表上的索引:
Permarks 表上的索引:
PERSMARKS_pm_p_id
PERSON_MARKScode_AND_date_till_AND_end_date_
查询:
SELECT doc
FROM docum(NOLOCK)
INNER JOIN pers(NOLOCK) ON doc = p
INNER JOIN permarks(NOLOCK) ON pm = p
WHERE doccode IN (20, 21, 22, 23, 24, 25, 30)
AND pm_ = 14
AND (enddate IS NULL OR enddate > getdate())
AND (date_till IS NULL OR date_till > getdate())
如何加快查询速度?
这里是完整的查询,执行时间是 5 分钟 INTO #temp:
SELECT f
,0 AS viso
,count(DISTINCT p) AS el_budu
,0 AS vidinis
,0 AS pasirasyta
INTO #temp
FROM documents(NOLOCK)
INNER JOIN fo(NOLOCK) ON doc = fv
INNER JOIN for(NOLOCK) ON fve = f
INNER JOIN per(NOLOCK) ON doc = p
INNER JOIN tax ti(NOLOCK) ON p = ti
INNER JOIN permarks(NOLOCK) ON pm = p
WHERE pmtcode = 14
AND (
enddate IS NULL
OR enddate > getdate()
)
AND (
datetill IS NULL
OR datetill > getdate()
)
AND startdate >= '2015-01-01'
AND enddate <= '2015-12-31'
AND rtcode = 1
AND fvcode IN (25)
AND doccode IN (
20
,21
,22
,23
,24
,25
,30
)
GROUP BY fcode
执行计划结果:results photo
【问题讨论】:
-
doc_dprt_code 中有索引吗?
-
是的,我有两列 doc_dprt_code 和 doc_reg_date 的非聚集索引
-
您应该查看
execution plan。顺便说一句,日期过滤器不好。 -
这很奇怪。然后向您的 DBA 询问计划。
-
停止破坏你的问题...
标签: sql-server query-performance sqlperformance