我得到了优化数据库的任务,因为这个数据库的响应很慢。 这是可能的,如果我有大约90k记录,如果我想选择大约27k记录,响应时间是15秒? 我的查询有什么问题吗?或者这是关于索引的?
SELECT dbo.tblTimesheet.workID,
dbo.tblTimesheet.staffID,
dbo.tblTimesheet.workDate,
dbo.tblTimesheet.startTime,
dbo.tblTimesheet.endTime,
dbo.tblTimesheet.projectID,
dbo.tblTimesheet.timeFor,
dbo.tblTimesheet.workType,
dbo.tblTimesheet.workTitle,
dbo.tblTimesheet.workDescription,
dbo.tblProject.projectName,
dbo.tblProject.isDeleted AS isDeletedProject,
dbo.tblProject.isActive AS isActiveProject,
dbo.tblUser.firstName,
dbo.tblUser.lastName,
dbo.tblUser.isDeleted AS isDeletedStaff,
dbo.tblUser.staffType
FROM dbo.tblTimesheet WITH (NOLOCK)
INNER JOIN dbo.tblUser WITH (NOLOCK) ON dbo.tblTimesheet.staffID = dbo.tblUser.userID
INNER JOIN dbo.tblProject WITH (NOLOCK) ON dbo.tblTimesheet.projectID = dbo.tblProject.projectID
WHERE (dbo.tblTimesheet.isDeleted = 0)
此查询视图
select being_followed , count(1) as count
from table
group by being_followed
order by count desc ;
答案 0 :(得分:1)
ALTER TABLE dbo.tblTimesheet
ADD workDescription2 AS CAST(workDescription AS VARCHAR(MAX))
GO
CREATE INDEX ix ON dbo.tblTimesheet (projectID, staffID, workDate DESC, workID DESC)
INCLUDE (startTime, endTime, workType, workTitle, workDescription2, timeFor, isDeleted)
WHERE isDeleted = 0 AND workType = 1 AND timeFor = 2
--WITH (DROP_EXISTING=ON)
SELECT t.workID AS pageID,
t.workDate AS recDate,
t.startTime,
t.endTime,
t.workType AS recType,
t.workTitle AS recTitle,
t.workDescription2 AS workDescription,
p.projectName AS recProject,
RTRIM(LTRIM(u.firstName + ' ' + u.lastName)) AS recName
FROM dbo.tblTimesheet t --WITH (INDEX (ix))
JOIN dbo.tblUser u ON t.staffID = u.userID
JOIN dbo.tblProject p ON t.projectID = p.projectID
WHERE t.isDeleted = 0
AND p.isActive = 1
AND t.workType = 1
AND t.timeFor = 2
ORDER BY t.workDate DESC, t.workID DESC
this is the new execution plan
但这比以前慢了
答案 1 :(得分:1)
CREATE INDEX ix1 ON dbo.tblTimesheet (projectID, staffID, workID)
INCLUDE (workType, timeFor, isDeleted)
WHERE isDeleted = 0 AND workType = 1 AND timeFor = 2
IF OBJECT_ID('tempdb.dbo.#temp') IS NOT NULL
DROP TABLE #temp
CREATE TABLE #temp (
workID INT PRIMARY KEY,
recProject VARCHAR(50),
recName VARCHAR(150)
)
INSERT INTO #temp (workID, recProject, recName)
SELECT t.workID,
p.projectName,
RTRIM(LTRIM(u.firstName + ' ' + u.lastName)) AS recName
FROM dbo.tblTimesheet t WITH (INDEX (ix1))
JOIN dbo.tblUser u ON t.staffID = u.userID
JOIN dbo.tblProject p ON t.projectID = p.projectID
WHERE t.isDeleted = 0
AND p.isActive = 1
AND t.workType = 1
AND t.timeFor = 2
SELECT t.workID AS pageID,
t.workDate AS recDate,
t.startTime,
t.endTime,
1 AS recType,
t.workTitle AS recTitle,
t.workDescription,
tt.recProject,
tt.recName
FROM dbo.tblTimesheet t
JOIN #temp tt ON t.workID = tt.workID
ORDER BY t.workDate DESC, t.workID DESC
--OPTION(RECOMPILE)