473,419 Members | 2,042 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,419 software developers and data experts.

Query Result Slows Down If i Use Case Statement.

I wrote a Function who returns around 800000 records in 17-20 Seconds but if i use 7 differernt simple case Statements its time increases to 03 min and 10 seconds.. Can somebody help me.

Table Structure:
Create Table Attendence (dated DateTime,
Location_ID VarChar(3),
fs_time VarChar(6),
employee_id VarChar(32),
Present SmallInt,
time_in VarChar(8),
TimeIN_AMPM VarChar(2),
time_out VarChar(8),
TimeOut_AMPM VarChar(2),
LunchTime_Out VarChar(8),
LunchTime_In VarChar(8),
over_time VarChar(8),
FullHalf TinyInt,
TypeOfLeave VarChar(12),
LCReason VarChar(100),
BTEarlyOutgoingReason VarChar(100),
BTLCReason VarChar(100),
EarlyOutgoingReason VarChar(100),
ManuallyTemperred TinyInt,
DontCalculateOTFromTime TinyInt,

IsGazetted TinyInt,
HToM_TimeIn SmallInt,
HToM_TimeOut SmallInt,
HToM_OverTime SmallInt,
ManualOverTime Varchar(8),
HToM_ManualOverTime SmallInt,
LeaveForm_TypeOfLeave Varchar(12),
LeaveForm_FullHalf TinyInt,
CPLGivenOn DateTime,
MarkedSandwich TinyInt,
CurrentDeptt Varchar(12),
CurrentDesig Varchar(3),
CurrentShift Varchar(3),
ShortLeave_WOP TinyInt Default 0,
ShortLeave_Leave TinyInt Default 0,
ShortLeave_TypeOfLeave Varchar(12),
Constraint pk_Attendence Primary Key (Dated, fs_time, Employee_id),
Constraint fk_Attendence_EmployeeDetails Foreign Key (Employee_id) References EmployeeDetails (id),
Constraint fk_Attendence_CDeptt Foreign Key (CurrentDeptt) References DefDepartments (ID),
Constraint fk_Attendence_CDesig Foreign Key (CurrentDesig) References DefDesignations (ID),
Constraint fk_Attendence_CShift Foreign Key (CurrentShift) References DefShifts (ID)
);
=========Function===================
Alter Function f_LeaveRecords (@EmpID Varchar(32), @FromDate VarChar(11), @ToDate VarChar(11),
@iDeptt Varchar(12), @iDesig Varchar(12))
Returns Table
as
Return (
Select T.Dated,

T.Time_In TimeIn,

T.Time_Out TimeOut,

IsNull(T.ManuallyTemperred,0) ManuallyTemperred,
ED.ID Employee_ID,
Case When ED.Daily=1 then 0 When ED.Monthly=1 Then 1 When ED.Contract=1 Then 2
When ED.PieceRate=1 Then 3 End TypeOfEmployee,

Case When IsNull(T.IsGazetted,0)<>0 and IsNull(IsNull(T.LeaveForm_TypeOfLeave,T.TypeOfLeav e),'')='' Then 'GZ'
When IsNull(IsNull(T.LeaveForm_TypeOfLeave,T.TypeOfLeav e),'')='' and Present=0 and (IsNull(IsNull(T.LeaveForm_FullHalf,T.FullHalf),0) <>0 or ((IsNull(Time_In,' : ')=' : ' or IsNull(Time_In,'')=''))) Then 'Absent'
Else IsNull(IsNull(T.LeaveForm_TypeOfLeave,T.TypeOfLeav e),'') End TypeOfLeave,

Case When ((DatePart(dw, T.Dated)=PD1.DayNo or DatePart(dw, T.Dated)=PD2.DayNo or IsNull(T.IsGazetted,0)=1) and T.Present=0 and IsNull(IsNull(T.LeaveForm_TypeOfLeave,T.TypeOfLeav e),'')='') Then 1
When IsNull(T.Present, 0) <>0 Then
Case When IsNull(IsNull(T.LeaveForm_FullHalf,T.FullHalf),0)= 0 Then 1 Else 0.5 End
When T.Time_IN<>' : ' and T.Time_IN<>'' and (T.Time_Out='' or T.Time_Out=' : ') Then 0
Else 0 End DayPresent,

Case When IsNull(IsNull(T.LeaveForm_FullHalf,T.FullHalf),0)= 0 Then 0 Else 1 End HalfLeave,

Case When IsNull(IsNull(T.LeaveForm_FullHalf,T.FullHalf),0)< >0 Then 0.5
When IsNull(IsNull(T.LeaveForm_TypeOfLeave,T.TypeOfLeav e),'') In ('Absent', 'LWP', 'L.W.Pay', 'L.W/O.Pay') and T.Present=0 and ((IsNull(T.Time_In, ' : ')=' : ' or T.Time_In='') and (IsNull(T.Time_Out, ' : ')=' : ' or T.Time_Out='')) Then 1
When IsNull(IsNull(T.LeaveForm_TypeOfLeave,T.TypeOfLeav e),'')<>'' and IsNull(IsNull(T.LeaveForm_TypeOfLeave,T.TypeOfLeav e),'')<>'Rest' and IsNull(T.IsGazetted,0)=0 Then 1 Else 0 End+
((IsNull(T.ShortLeave_WOP,0)+IsNull(T.ShortLeave_L eave,0))*IsNull(GS.ShortLeaveEqualToDeductionOfLea ve,0)) LeavesCounted,

T.CurrentDeptt Deptt_ID, T.CurrentDesig Desig_ID, T.CurrentShift Shift_ID
From Attendence T
Left Join EmployeeDetails ED on T.Employee_ID=ED.ID
-- Left Join period on period.From_DT=Period.From_DT
Left Join PrintDays PD1 on ED.RestDay=PD1.ID
Left Join PrintDays PD2 on ED.RestDayOther=PD2.ID
-- Left Join DefDepartments DD on DD.ID=T.CurrentDeptt
-- Left Join DefDesignations DG on DG.ID=T.CurrentDesig
-- Left Join DefShifts DS on DS.ID=T.CurrentShift
Left Join GlobalSettings GS On 1=1
Where (@EmpID='' or T.Employee_ID=@EmpID)
and (@FromDate='' or T.Dated Between @FromDate and @ToDate)
)
================Simple Query Which Takes 19 Seconds=====
Select * from f_LeaveRecords ('', '01-Jul-2001', '30-Jun-2010', '', '') T
================Other Query Which Takes More than 3 Minutes=======
Select *,
Case When T.TypeOfLeave='Casual' Then T.LeavesCounted Else 0 End Casual,
Case When T.TypeOfLeave='Annual' Then T.LeavesCounted Else 0 End Annual,
Case When T.TypeOfLeave='Sick' Then T.LeavesCounted Else 0 End Sick,
Case When T.TypeOfLeave In ('LWP', 'L.W.Pay', 'L.W/O.Pay') Then T.LeavesCounted Else 0 End LWP,
Case When T.TypeOfLeave='Absent' Then T.LeavesCounted Else 0 End Absent,
Case When T.TypeOfLeave='GZ' Then T.LeavesCounted Else 0 End GZ,
Case When T.TypeOfLeave='Rest' Then T.LeavesCounted Else 0 End Rest from f_LeaveRecords ('', '01-Jul-2001', '30-Jun-2010', '', '') T
Feb 27 '10 #1
0 1197

Sign in to post your reply or Sign up for a free account.

Similar topics

13
by: Wescotte | last post by:
Here is a small sample program I wrote in PHP (running off Apache 1.3.31 w/ PHP 5.0.1) to help illustrates problem I'm having. The data base is using DB2 V5R3M0. The client is WinXP machine using...
20
by: | last post by:
If I need to check if a certain value does exist in a field, and return either "yes" or "not" which query would be the most effestive?
9
by: netpurpose | last post by:
I need to extract data from this table to find the lowest prices of each product as of today. The product will be listed/grouped by the name only, discarding the product code - I use...
5
by: Gerald Khin | last post by:
I encountered a performance problem with UPDATE statement and foreign key constraints. There are two tables involved: A parent table A and a child table B: create table A( ID CHAR(15) NOT...
7
by: rednexgfx_k | last post by:
All, Problem Summary: I've running about 30 make table queries via VBA in Access 2000, and my database goes from 14,000k to over 2,000,000k. In addition, the longer the procedure runs, the...
6
by: Larry R Harrison Jr | last post by:
I have a database I'm designing in Access 97. I have a custom field in a query which looks in {Table of Documents} and shows them all. It then needs a "latest revision number," stored in another...
3
by: cover | last post by:
I have a table with 50 fields that receive input depending on whether that input came in from a 'shaker' form or a 'conveyor' form. Input from the 'conveyor' form might populate 25 fields while...
5
by: Nesa | last post by:
I have a stored procedure that wraps a moderately complex query over 5, 6 related tables. The performance of the procedure is unacceptably slow as it takes on average 5-10 min to complete. To...
9
by: Kelii | last post by:
I've been trying to get this piece to work for a few hours, but have given up. I hope someone out there can help, I think the issue is relatively straightforward, but being a novice, I'm stumped....
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.