|
asif372
Posting Yak Master
100 Posts |
Posted - 2012-04-02 : 03:50:18
|
| Dear Sir,I have Written following query when i execute it takes much time i think it is due to case statement whats the procedure of improving Speed my Stored Procedure is as FollowedALTER PROCEDURE [dbo].[Procedure1] @EmpID int, @FromDate DATETIME, @ToDate DATETIME, @Dept NVARCHAR(50)asBEGIN IF ISNULL(@FromDate,'') = '' SELECT @FromDate = '20110101' IF ISNULL(@ToDate,'') = '' SELECT @ToDate = GETDATE()SELECT D1.EmployeeCode, D1.[Date], D2.[Time In], D2.[Time Out], D2.Spend, D2.UserName, D2.USERID, D2.DutyHours, D2.ShiftTimeDiff, D2.ExcShort, CONVERT(CHAR(5), D2.EShortTime,108) EShortTime , D2.DiffHours, D2.DiffMin, D2.Shift, D2.Branch, D1.Department, D1.Name,D1.EmpBranch, CASE -- WHEN ISNULL(LEFT(CONVERT(NVARCHAR, D4.TimeIn),11),'') <> '' THEN D4.Remarks WHEN D3.Date IS NOT NULL THEN D3.Remarks WHEN D1.[Date] > GETDATE() THEN NULL WHEN D2.[Time in] IS NULL THEN 'Absent' ELSE D2.Remarks END Remarks, LTIME, HDTIME, EARLYArival,D2.InBetween,D2.CommentsFROM AllDateView2 D1 LEFT OUTER JOIN HFAllAttendance D2 ON D1.EmployeeCode = D2.[Employee Code] AND D1.[Date] = convert(datetime, D2.[Date], 105) LEFT OUTER JOIN FHolidayView D3 ON D1.[Date] = D3.[Date]WHERED1.[Date] BETWEEN @FromDate and @ToDateAND (ISNULL(@Dept,'')='' OR D1.Department = @Dept)AND (ISNULL(@EmpID,'')='' OR D1.[EmployeeCode] = @EmpID)ENDThanks In Advance |
|
|
Ceemack
Starting Member
3 Posts |
Posted - 2012-04-03 : 12:23:33
|
| The case statement in and of itself doesn't look too bad. Check the actual execution plan for table scans and clustered index scans to see what indexes might be added to help speed up the query. If you're using the 2008 version of SSMS, the execution plan will show you any indexes that Query Optimizer thinks would help. I might also take the convert statement out of the conditions for the first outer join. Generally speaking, you want to keep conversions out of WHERE clauses and JOIN conditions. In this particular case, that conversion doesn't appear to be doing anything for you. |
 |
|