Select from multiple tables with multiple where clauses - sql

I am trying to write a stored procedure that will give a count of all the cases in a table that are not deleted, grouped by a CaseStatusID in another table, but only the cases that have a CaseStatusID that also isn't deleted. I also want it to return any CaseStatusID that does not have case related to it (i.e. a count of 0)
So far I have tried
Create Table #tCaseStatus (CaseStatusID int,CaseStatusDesc varchar(200) )
declare #NofCases int
declare #CaseStatusID int
declare #CaseStatusDesc varchar(200)
BEGIN
INSERT #tCaseStatus
Select CaseStatusID, CaseStatusDesc From dbo.CaseStatus Where IsDeleted = 0
Select #NofCases = Count(*) From #tCaseStatus
While (#NofCases > 0)
Begin
Select Top (1) #CaseStatusID = CaseStatusID, #CaseStatusDesc = CaseStatusDesc from #tCaseStatus
SELECT CaseStatusDesc, Count(CaseID) AS CountCases
FROM Cases inner join #tCaseStatus on Cases.CaseStatusID = #tCaseStatus.CaseStatusID
WHERE (IsDeleted = 0) AND Cases.CaseStatusID = #CaseStatusID
Group by #tCaseStatus.CaseStatusDesc
Set #NofCases = #NofCases - 1
Delete Top(1) from #tCaseStatus
End
END
AND
This returns the correct cases but excludes any of the CaseStatusDesc that have a count of 0
SELECT CaseStatus.CaseStatusDesc, COUNT(Cases.CaseID) AS CaseCount
FROM Cases FULL OUTER JOIN
CaseStatus ON Cases.CaseStatusID = CaseStatus.CaseStatusID
WHERE (CaseStatus.IsDeleted = 0) AND
(Cases.IsDeleted = 0)
GROUP BY CaseStatus.CaseStatusDesc, CaseStatus.CaseStatusID
ORDER BY CaseStatus.CaseStatusID
AND
this returns all the CaseStatusDesc's even the ones that are deleted
SELECT CaseStatus.CaseStatusDesc, COUNT(CASE
WHEN CaseStatus.IsDeleted = 0 THEN 'ok'
WHEN Cases.IsDeleted = 0 THEN 'ok'
Else null
END) AS [Case]
FROM Cases FULL OUTER JOIN CaseStatus ON Cases.CaseStatusID = CaseStatus.CaseStatusID
GROUP BY CaseStatus.CaseStatusDesc, CaseStatus.CaseStatusID
Order By CaseStatus.CaseStatusID asc
But I cant seem to get the desired results

Is this what you're after?
select
casestatus.casestatusid,
casestatusdesc,
COUNT(caseid)
from casestatus
left join cases
on casestatus.casestatusid = cases.casestatusid
and cases.isdeleted=0
where
casestatus.isdeleted=0
group by
casestatus.casestatusid,
casestatusdesc

Related

How to use If Else if and Else in stored procedures | SQL Server

I have created the following stored procedure where user passes input parameters to get the data. The input parameters are optional i.e. passing just one parameter should produce some data.
Here is my code.
ALTER PROC [dbo].[SPR_ECRM_CASE_INFORMATION]
#ID_APPLICATION_NO NVARCHAR(20),
#TX_ID_NO NVARCHAR(20)
AS
BEGIN
SET NOCOUNT ON
IF (#ID_APPLICATION_NO IS NOT NULL ) OR (LEN(#ID_APPLICATION_NO) > 0) AND ((#TX_ID_NO IS NOT NULL) OR (LEN(#TX_ID_NO) > 0))
BEGIN
SELECT DISTINCT
[A].[ID_APPLICATION_NO]
,[A].[TX_APPLICATION_STATUS]
FROM [VW_T_APPLICATION] As [A]
INNER JOIN [CT_L1_APPLICATION_STATUS] [L] ON [L].[TX_L1_APPLICATION_STATUS_CODE] = [A].[TX_APPLICATION_STATUS_CODE]
LEFT JOIN [VW_ST_APPLICATION_APPLICANT] [I] WITH(NOLOCK) ON [I].[ID_APPLICATION_APPLICANT] = (SELECT TOP 1
[II].[ID_APPLICATION_APPLICANT]
FROM
[ST_APPLICATION_APPLICANT] [II] WITH(NOLOCK)
WHERE [II].[ID_APPLICATION_GUID] = [A].[ID_APPLICATION_GUID]
AND [II].[CD_APPLICANT_TYPE] = 81
AND [II].[IN_ACTIVE] = 1
ORDER BY
[II].[ID_APPLICATION_APPLICANT] DESC)
LEFT JOIN [VW_ST_APPLICATION_APPLICANT] [J] WITH(NOLOCK) ON [J].[ID_APPLICATION_APPLICANT] = (SELECT TOP 1
[JJ].[ID_APPLICATION_APPLICANT]
FROM
[ST_APPLICATION_APPLICANT] [JJ] WITH(NOLOCK)
WHERE [JJ].[ID_APPLICATION_GUID] = [A].[ID_APPLICATION_GUID]
AND [JJ].[CD_APPLICANT_TYPE] = 82
AND [JJ].[IN_ACTIVE] = 1
ORDER BY
[JJ].[ID_APPLICATION_APPLICANT] DESC)
WHERE
[I].[TX_ID_NO] = #TX_ID_NO AND [J].[TX_ID_NO] = #TX_ID_NO
AND
[A].[TX_APPLICANT_ID_NO] = #ID_APPLICATION_NO
AND
[A].[IN_ACTIVE] = 1
END
ELSE IF (#ID_APPLICATION_NO IS NOT NULL) OR (LEN(#ID_APPLICATION_NO) > 0)
BEGIN
SELECT DISTINCT
[A].[ID_APPLICATION_NO]
,[A].[TX_APPLICATION_STATUS]
FROM [VW_T_APPLICATION] As [A]
INNER JOIN [CT_L1_APPLICATION_STATUS] [L] ON [L].[TX_L1_APPLICATION_STATUS_CODE] = [A].[TX_APPLICATION_STATUS_CODE]
LEFT JOIN [VW_ST_APPLICATION_APPLICANT] [I] WITH(NOLOCK) ON [I].[ID_APPLICATION_APPLICANT] = (SELECT TOP 1
[II].[ID_APPLICATION_APPLICANT]
FROM
[ST_APPLICATION_APPLICANT] [II] WITH(NOLOCK)
WHERE [II].[ID_APPLICATION_GUID] = [A].[ID_APPLICATION_GUID]
AND [II].[CD_APPLICANT_TYPE] = 81
AND [II].[IN_ACTIVE] = 1
ORDER BY
[II].[ID_APPLICATION_APPLICANT] DESC)
LEFT JOIN [VW_ST_APPLICATION_APPLICANT] [J] WITH(NOLOCK) ON [J].[ID_APPLICATION_APPLICANT] = (SELECT TOP 1
[JJ].[ID_APPLICATION_APPLICANT]
FROM
[ST_APPLICATION_APPLICANT] [JJ] WITH(NOLOCK)
WHERE [JJ].[ID_APPLICATION_GUID] = [A].[ID_APPLICATION_GUID]
AND [JJ].[CD_APPLICANT_TYPE] = 82
AND [JJ].[IN_ACTIVE] = 1
ORDER BY
[JJ].[ID_APPLICATION_APPLICANT] DESC)
WHERE
[A].[TX_APPLICANT_ID_NO] = #ID_APPLICATION_NO
AND
[A].[IN_ACTIVE] = 1
END
ELSE IF (#TX_ID_NO IS NOT NULL) OR (LEN(#TX_ID_NO) > 0)
BEGIN
SELECT DISTINCT
[A].[ID_APPLICATION_NO]
,[A].[TX_APPLICATION_STATUS]
FROM [VW_T_APPLICATION] As [A]
INNER JOIN [CT_L1_APPLICATION_STATUS] [L] ON [L].[TX_L1_APPLICATION_STATUS_CODE] = [A].[TX_APPLICATION_STATUS_CODE]
LEFT JOIN [VW_ST_APPLICATION_APPLICANT] [I] WITH(NOLOCK) ON [I].[ID_APPLICATION_APPLICANT] = (SELECT TOP 1
[II].[ID_APPLICATION_APPLICANT]
FROM
[ST_APPLICATION_APPLICANT] [II] WITH(NOLOCK)
WHERE [II].[ID_APPLICATION_GUID] = [A].[ID_APPLICATION_GUID]
AND [II].[CD_APPLICANT_TYPE] = 81
AND [II].[IN_ACTIVE] = 1
ORDER BY
[II].[ID_APPLICATION_APPLICANT] DESC)
LEFT JOIN [VW_ST_APPLICATION_APPLICANT] [J] WITH(NOLOCK) ON [J].[ID_APPLICATION_APPLICANT] = (SELECT TOP 1
[JJ].[ID_APPLICATION_APPLICANT]
FROM
[ST_APPLICATION_APPLICANT] [JJ] WITH(NOLOCK)
WHERE [JJ].[ID_APPLICATION_GUID] = [A].[ID_APPLICATION_GUID]
AND [JJ].[CD_APPLICANT_TYPE] = 82
AND [JJ].[IN_ACTIVE] = 1
ORDER BY
[JJ].[ID_APPLICATION_APPLICANT] DESC)
WHERE
[I].[TX_ID_NO] = #TX_ID_NO OR [J].[TX_ID_NO] = #TX_ID_NO
AND
[A].[IN_ACTIVE] = 1
END
ELSE
BEGIN
RETURN NULL
END
END
I have used if else if and else condition but it fails to return the data.
DECLARE #A NVARCHAR(30)
DECLARE #B NVARCHAR(20)
SET #A = 'C192'
SET #B = 'ABC'
EXEC SPR_ECRM_CASE_INFORMATION #A, ''
EXEC SPR_ECRM_CASE_INFORMATION '', #B
EXEC SPR_ECRM_CASE_INFORMATION '', ''
Firstly, the else if condition fails and my code here is redundant. Is there any better way to optimize this. Your help is appreciated.
If you want to optimize for performance, you can create a single procedure for each query. Each will get it's own query plan. You can call each in this procedure.
Looking at each query, I see the sub-query in the ON clause. I also see NOLOCK which I normally consider a bad sign. Also see the DISTINCT. These are all clues that the query can be simplified. SQL Server will then be more likely to optimize the query. (You can ask for the same thing in more than one way. The more complex the logic, the less likely SQL will find the best plan.)
If the data in [VW_T_APPLICATION] is already distinct, then get rid of DISTINCT and use EXISTS instead of LEFT JOINS - one EXISTS for 81 and one for 82. (I'm assuming you are not going to need columns from these.)
The use of "VW" implies the use of views. The view can hide a lot of logic that gets in the way of a simple query.
Is this part (your first IF) correct in terms of bracketing? It will run if #ID_Application_No is NOT NULL, regardless of whether #TX_ID_No has a value.
IF (#ID_APPLICATION_NO IS NOT NULL ) OR (LEN(#ID_APPLICATION_NO) > 0) AND ((#TX_ID_NO IS NOT NULL) OR (LEN(#TX_ID_NO) > 0))
I'm guessing you want either
a) brackets around the ID_Application_No part e.g.,
IF ((#ID_APPLICATION_NO IS NOT NULL ) OR (LEN(#ID_APPLICATION_NO) > 0)) AND ((#TX_ID_NO IS NOT NULL) OR (LEN(#TX_ID_NO) > 0))
or b) want it to be an AND (this is my most likely guess) e.g.,
IF (#ID_APPLICATION_NO IS NOT NULL ) AND (LEN(#ID_APPLICATION_NO) > 0) AND ((#TX_ID_NO IS NOT NULL) OR (LEN(#TX_ID_NO) > 0))
Note you can simplify some things e.g., when you have IF (#ID_APPLICATION_NO IS NOT NULL) OR (LEN(#ID_APPLICATION_NO) > 0), the first part will always be true if the second part is true. In those cases, you can use only IF (#ID_APPLICATION_NO IS NOT NULL) and it will give the same results.

SQL Server function return unexpected result

I have a function to check if user is authenticated but the result is not correct :
CREATE FUNCTION [dbo].[IsAuthorized]
(#UserToken nvarchar(250),
#UserCode nvarchar(250))
RETURNS int
AS
BEGIN
IF (SELECT [User].UserId
FROM [User]
INNER JOIN UserLogin ON [User].UserId = UserLogin.UserId
WHERE [User].UserCode = #UserCode
AND UserLogin.UserToken = #UserToken
AND UserLogin.UserTokenExpiration > GETDATE()) > 0
RETURN 1;
IF NOT EXISTS (SELECT [User].UserId
FROM [User]
WHERE [User].UserCode = #UserCode)
RETURN -201; -- User does not exist
IF NOT EXISTS (SELECT [User].UserId
FROM [User]
INNER JOIN UserLogin ON [User].UserId = UserLogin.UserId
WHERE [User].UserCode = #UserCode
AND UserLogin.UserToken = #UserToken)
RETURN -202; -- Token is not valid
IF EXISTS (SELECT [User].UserId
FROM [User]
INNER JOIN UserLogin ON [User].UserId = UserLogin.UserId
WHERE [User].UserCode = #UserCode
AND UserLogin.UserToken = #UserToken
AND UserLogin.UserTokenExpiration < GETDATE())
RETURN -203; -- Token expired
RETURN 0;
END
When I try to run:
DECLARE #UserCode nvarchar(250) = N'7C6898E2-0529-4C3F-B4B2-FA69087CDF4A';
DECLARE #UserToken nvarchar(250)= N'DE3B193D-65BC-4F75-970A-932C9F825D81';
SELECT dbo.IsAuthorized(#UserCode,#UserToken) as FunctionResult
SELECT *
FROM [User]
INNER JOIN UserLogin ON [User].UserId = UserLogin.UserId
WHERE
[User].UserCode = #UserCode
AND UserLogin.UserToken = #UserToken
AND UserLogin.UserTokenExpiration > GETDATE()
I get this result:
==> the function does not find the line
-201
==> the query finds a line
4 7C6898E2-0529-4C3F-B4B2-FA69087CDF4A Ahmed ALOUI TROY aloui.ahmed#wanadoo.fr 0 0 1 0 /Ressources/img/aloui.jpg 4 73828562FADE36DD6774C6854F52965C CC6CA2373C2240743D051352BC3AF3C0 DE3B193D-65BC-4F75-970A-932C9F825D81 2016-11-25 16:02:12.083
Any clues?
The difference is that in your function, you are running a SELECT and comparing the result of that select to see whether it is greater than zero, (which will not work), whereas in your raw SELECT without using the function, you are just returning the whole record(s) if found.
You probably want this in your function:
IF (SELECT COUNT([User].UserId)
FROM [User]
INNER JOIN UserLogin ON [User].UserId = UserLogin.UserId
WHERE [User].UserCode = #UserCode
AND UserLogin.UserToken = #UserToken
AND UserLogin.UserTokenExpiration > GETDATE()) > 0
RETURN 1;
which will count the number of records returned by your SELECT and if the number is greater than zero, will return a value of 1 from the function.
IF EXISTS (SELECT 1
FROM
User u
INNER JOIN UserLogin l
ON u.UserId = l.UserId
AND l.UserToken = #UserTokey
AND l.UserTokenExpiration = GETDATE()
WHERE
u.UserCode = #UserCode
)
BEGIN
RETURN 1;
END
So the way you have User.UserId > 0 should work but only if a single scalar numeric value is returned from the query. If multiple are returned it would error out. Given what you are showing that would seem to be true but perhaps you are only showing some of the results etc. Anyway, in the rest of the function you are using the perfect technique to change this too as well. Just use IF EXISTS(SELECT....).
Note it is a pet peeve of mine to include conditions to restrict a joined table in the WHERE clause instead of the ON conditions. putting them in the ON could be more optimized but your intentions are clearer as such I have modified the query to reflect that suggestion.

SQL Server / T-SQL : query optimization assistance

I have this QA logic that looks for errors into every AuditID within a RoomID to see if their AuditType were never marked Complete or if they have two complete statuses. Finally, it picks only the maximum AuditDate of the RoomIDs with errors to avoid showing multiple instances of the same RoomID, since there are many audits per room.
The issue is that the AUDIT table is very large and takes a long time to run. I was wondering if there is anyway to reach the same result faster.
Thank you in advance !
IF object_ID('tempdb..#AUDIT') is not null drop table #AUDIT
IF object_ID('tempdb..#ROOMS') is not null drop table #ROOMS
IF object_ID('tempdb..#COMPLETE') is not null drop table #COMPLETE
IF object_ID('tempdb..#FINALE') is not null drop table #FINALE
SELECT distinct
oc.HotelID, o.RoomID
INTO #ROOMS
FROM dbo.[rooms] o
LEFT OUTER JOIN dbo.[hotels] oc on o.HotelID = oc.HotelID
WHERE
o.[status] = '2'
AND o.orderType = '2'
SELECT
t.AuditID, t.RoomID, t.AuditDate, t.AuditType
INTO
#AUDIT
FROM
[dbo].[AUDIT] t
WHERE
t.RoomID IN (SELECT RoomID FROM #ROOMS)
SELECT
t1.RoomID, t3.AuditType, t3.AuditDate, t3.AuditID, t1.CompleteStatus
INTO
#COMPLETE
FROM
(SELECT
RoomID,
SUM(CASE WHEN AuditType = 'Complete' THEN 1 ELSE 0 END) AS CompleteStatus
FROM
#AUDIT
GROUP BY
RoomID) t1
INNER JOIN
#AUDIT t3 ON t1.RoomID = t3.RoomID
WHERE
t1.CompleteStatus = 0
OR t1.CompleteStatus > 1
SELECT
o.HotelID, o.RoomID,
a.AuditID, a.RoomID, a.AuditDate, a.AuditType, a.CompleteStatus,
c.ClientNum
INTO
#FINALE
FROM
#ROOMS O
LEFT OUTER JOIN
#COMPLETE a on o.RoomID = a.RoomID
LEFT OUTER JOIN
[dbo].[clients] c on o.clientNum = c.clientNum
SELECT
t.*,
Complete_Error_Status = CASE WHEN t.CompleteStatus = 0
THEN 'Not Complete'
WHEN t.CompleteStatus > 1
THEN 'Complete More Than Once'
END
FROM
#FINALE t
INNER JOIN
(SELECT
RoomID, MAX(AuditDate) AS MaxDate
FROM
#FINALE
GROUP BY
RoomID) tm ON t.RoomID = tm.RoomID AND t.AuditDate = tm.MaxDate
One section you could improve would be this one. See the inline comments.
SELECT
t1.RoomID, t3.AuditType, t3.AuditDate, t3.AuditID, t1.CompleteStatus
INTO
#COMPLETE
FROM
(SELECT
RoomID,
COUNT(1) AS CompleteStatus
-- Use the above along with the WHERE clause below
-- so that you are aggregating fewer records and
-- avoiding a CASE statement. Remove this next line.
--SUM(CASE WHEN AuditType = 'Complete' THEN 1 ELSE 0 END) AS CompleteStatus
FROM
#AUDIT
WHERE
AuditType = 'Complete'
GROUP BY
RoomID) t1
INNER JOIN
#AUDIT t3 ON t1.RoomID = t3.RoomID
WHERE
t1.CompleteStatus = 0
OR t1.CompleteStatus > 1
Just a thought. Streamline your code and your solution. you are not effectively filtering your datasets smaller so you continue to query the entire tables which is taking a lot of your resources and your temp tables are becoming full copies of those columns without the indexes (PK, FK, ++??) on the original table to take advantage of. This by no means is a perfect solution but it is an idea of how you can consolidate your logic and reduce your overall data set. Give it a try and see if it performs better for you.
Note this will return the last audit record for any room that has either not had an audit completed or completed more than once.
;WITH cte AS (
SELECT
o.RoomId
,o.clientNum
,a.AuditId
,a.AuditDate
,a.AuditType
,NumOfAuditsComplete = SUM(CASE WHEN a.AuditType = 'Complete' THEN 1 ELSE 0 END) OVER (PARTITION BY o.RoomId)
,RowNum = ROW_NUMBER() OVER (PARTITION BY o.RoomId ORDER BY a.AuditDate DESC)
FROm
dbo.Rooms o
LEFT JOIN dbo.Audit a
ON o.RoomId = a.RoomId
WHERE
o.[Status] = 2
AND o.OrderType = 2
)
SELECT
oc.HotelId
,cte.RoomId
,cte.AuditId
,cte.AuditDate
,cte.AuditType
,cte.NumOfAuditsComplete
,cte.clientNum
,Complete_Error_Status = CASE WHEN cte.NumOfAuditsComplete > 1 THEN 'Complete More Than Once' ELSE 'Not Complete' END
FROM
cte
LEFT JOIN dbo.Hotels oc
ON cte.HotelId = oc.HotelId
LEFT JOIN dbo.clients c
ON cte.clientNum = c.clientNum
WHERE
cte.RowNum = 1
AND cte.NumOfAuditsComplete != 1
Also note I changed your
WHERE
o.[status] = '2'
AND o.orderType = '2'
TO
WHERE
o.[status] = 2
AND o.orderType = 2
to be numeric without the single quotes. If the data type is truely varchar add them back but when you query a numeric column as a varchar it will do data conversion and may not take advantage of indexes that you have built on the table.

Query taking too much

I am trying to execute updates while my condition returns results, the problem is that when i am testing the query it never finishes.
Here is the query;
While(select COUNT(*) from Agreement as agr where agr.Id in (
select toa.Id from Agreement_TemporaryOnceAgreement as toa where toa.Executed =1)
and agr.EndingDate is null) > 0
begin
DECLARE #AgreementID int;
SET #AgreementID =
(
select top 1 agr.id from Agreement as agr where agr.Id in (
select toa.Id from Agreement_TemporaryOnceAgreement as toa where toa.Executed =1)
and agr.EndingDate is null
)
update Agreement SET EndingDate = (
select tado.Date from TemporaryAgreementsDateOfExecution tado
where tado.AgreementId = CAST(#AgreementID AS INT))
where Agreement.Id = CAST(#AgreementID AS INT);
end;
You don't need a loop. A single update query resembling this should get the job done.
update a
set EndingDate = tado.date
from Agreement a join TemporaryAgreementsDateOfExecution tado
on a.AgreementId = tado.AgreementId
join Agreement_TemporaryOnceAgreement toa
on a.Id = toa.id
where EndingDate is null
and toa.Executed = 1
There might be slight variations depending on the RDBMS you are using.

SQL SP runs Slow

Hi
I am using an SP which takes 7 minutes in a server which has 7336 recrds
and 6seconds in another server which has 3500 records.
Can anybody help me to know why is it happening?
Thanks,
-Divya
THE SP:
SELECT WORKSHEET_ID
FROM PERSON PER
INNER JOIN PERSON EMPLEE
ON EMPLEE.PERSON_ID = PER.PERSON_ID
AND
dbo.FN_CHECKRPTSECURITY(EMPLEE.PERSON_ID, #p_SEC_ACCOUNT_ID) > 0
LEFT JOIN SEARCH_ASSIGNMENT_VW PERSON_ASGN
ON PERSON_ASGN.ASSIGNMENT_ID =
dbo.FN_GETRPTASSIGNMENTID(EMPLEE.PERSON_ID)
LEFT JOIN LOOKUP EMPLEE_ASGN_STAT
ON EMPLEE_ASGN_STAT.TYPE_ = 'ASSIGNMMENT_STATUS_CODE'
AND EMPLEE_ASGN_STAT.CODE = PERSON_ASGN.ASGN_STAT_CODE
INNER JOIN
(SELECT w1.ASSIGNMENT_ID, w1.WORKSHEET_ID, w1.EFFECTIVE_DATE, w1.APPROVED_BY, w3.CREATED_BY
FROM WORKSHEET_PAYROLL_VW w1
INNER JOIN WORKSHEET w3
ON w3.WORKSHEET_ID = w1.WORKSHEET_ID
WHERE w1.EFFECTIVE_DATE = CASE
WHEN #p_MOST_RECENT_ONLY = 'Y'
THEN (SELECT MAX(w2.EFFECTIVE_DATE)
FROM WORKSHEET_PAYROLL_VW w2
WHERE w1.ASSIGNMENT_ID = w2.ASSIGNMENT_ID
AND (ISNULL(#p_WKS_EFFECTIVE_DATE,w2.EFFECTIVE_DATE) =w2.EFFECTIVE_DATE))
ELSE ISNULL(#p_WKS_EFFECTIVE_DATE,w1.EFFECTIVE_DATE)
END
)
PERSON_WKS
ON PERSON_WKS.ASSIGNMENT_ID = dbo.FN_GETRPTASSIGNMENTID(EMPLEE.PERSON_ID)
INNER JOIN
(SELECT ASSIGNMENT_ID, VALUE
FROM ASSIGNMENT_HISTORY AH
WHERE FIELD_NAME ='HOME PAYROLL GROUP'
AND EFFECTIVE_DATE = (SELECT MAX(EFFECTIVE_DATE)
FROM ASSIGNMENT_HISTORY
WHERE ASSIGNMENT_ID = AH.ASSIGNMENT_ID
AND EFFECTIVE_DATE <=getDate()
AND FIELD_NAME = 'HOME PAYROLL GROUP')
)HOME_PAYROLL
ON HOME_PAYROLL.ASSIGNMENT_ID = dbo.FN_GETRPTASSIGNMENTID(EMPLEE.PERSON_ID)
WHERE
(#p_SELECTED_PERSON_ONLY = 'N' OR EMPLEE.PERSON_ID = #p_PERSON_ID)
AND
(#p_ASGN_STAT_CODE IS NULL OR PERSON_ASGN.ASGN_STAT_CODE = SUBSTRING(#p_ASGN_STAT_CODE,1,1)
OR PERSON_ASGN.ASGN_STAT_CODE = SUBSTRING(#p_ASGN_STAT_CODE,2,1))
AND
(#p_POLICY_ID IS NULL OR PERSON_ASGN.PROGRAM_CODE = #p_POLICY_ID)
AND
(#p_HOME_COUNTRY_ID IS NULL OR PERSON_ASGN.HOMECOUNTRYID=#p_HOME_COUNTRY_ID)
AND
(#p_HOME_CITY_ID IS NULL OR PERSON_ASGN.HOMECITYID=#p_HOME_CITY_ID )
AND
(#p_HOME_COMPANY_ID IS NULL OR PERSON_ASGN.HOMEBUSINESSID=#p_HOME_COMPANY_ID )
AND
(#p_HOME_DIVISION_ID IS NULL OR PERSON_ASGN.HOMECOMPONENTID=#p_HOME_DIVISION_ID )
AND
(#p_HOST_COUNTRY_ID IS NULL OR PERSON_ASGN.HOSTCOUNTRYID=#p_HOST_COUNTRY_ID )
AND
(#p_HOST_CITY_ID IS NULL OR PERSON_ASGN.HOSTCITYID=#p_HOST_CITY_ID )
AND
(#p_HOST_COMPANY_ID IS NULL OR PERSON_ASGN.HOSTBUSINESSID=#p_HOST_COMPANY_ID )
AND
(#p_HOST_DIVISION_ID IS NULL OR PERSON_ASGN.HOSTCOMPONENTID=#p_HOST_DIVISION_ID )
AND
(#p_CREATED_BY IS NULL OR PERSON_WKS.CREATED_BY=#p_CREATED_BY )
AND
(#p_APPROVED_BY IS NULL OR PERSON_WKS.APPROVED_BY=#p_APPROVED_BY )
AND
(#p_payroll_code IS NULL OR HOME_PAYROLL.VALUE=#p_payroll_code )
ORDER BY PER.LAST_NAME ASC,
PER.FIRST_NAME ASC,
PERSON_WKS.EFFECTIVE_DATE DESC
The Function in the 5th line is the one which is running slow. rest of the part is running in 4secs
The FUNCTION:
BEGIN
DECLARE
#v_ASGN_COUNT INT,
#v_RESULT INT
SELECT #v_ASGN_COUNT = COUNT(ASSIGNMENT_ID) --to find out if this employee has any assignment
FROM ASSIGNMENT
WHERE EXPATRIATE_PERSON_ID = #p_PERSON_ID AND
ASGN_STAT_CODE IN ('PD','A','I')
IF(#v_ASGN_COUNT > 0) --yes assignment, check against SECURITY_ASSIGNMENT_VW
BEGIN
SELECT #v_RESULT = COUNT(ASSIGNMENT_ID)
FROM SECURITY_ASSIGNMENT_VW
WHERE SEC_ACCOUNT_ID = #p_SEC_ACCOUNT_ID AND
ASSIGNMENT_ID IN (SELECT ASSIGNMENT_ID
FROM ASSIGNMENT
WHERE EXPATRIATE_PERSON_ID = #p_PERSON_ID AND
ASGN_STAT_CODE IN ('PD','A','I'))
END
ELSE --no assignment, so check against SECURITY_PERSON_VW
BEGIN
SELECT #v_RESULT = COUNT(PERSON_ID)
FROM SECURITY_PERSON_VW
WHERE SEC_ACCOUNT_ID = #p_SEC_ACCOUNT_ID AND
PERSON_ID = #p_PERSON_ID
END
RETURN #v_RESULT
END
Do the schemas match exactly... in particular check for missing indexes.
Well to begin with you have scalar functions which will run significantly slower as the number of records increase becasue they process row-by-agonizing-row. Not only that you've used the functions in joins which is a horrible practice if you need performance. You have a bunch of OR conditions which tend to slowness. And while it is too hard to actually read the code you posted (please try to format and only use all caps for keywords), I would suspect that some of those conditions are not sargable.
To know what is actually happening check the Execution plan (SQL Server) or Explain Plan (mySQL and others I think) or the equivalent feature in your database. Likely you wil find table scans which of course are going to get significantly slower as the number of records increases.
You may also have a problem with parameter sniffing. Please google to see how to fix that.
One improvement would be to make sure that dbo.FN_GETRPTSSIGNMENTID only gets executed once.
Currently, it gets executed three times.
You can replace two of those calls by joining to the field of the (one) remaing call.
Something like
SELECT WORKSHEET_ID
FROM PERSON PER
INNER JOIN PERSON EMPLEE ON EMPLEE.PERSON_ID = PER.PERSON_ID AND dbo.FN_CHECKRPTSECURITY(EMPLEE.PERSON_ID, #p_SEC_ACCOUNT_ID) > 0
INNER JOIN (
SELECT w1.ASSIGNMENT_ID
, w1.WORKSHEET_ID
, w1.EFFECTIVE_DATE
, w1.APPROVED_BY
, w3.CREATED_BY
FROM WORKSHEET_PAYROLL_VW w1
INNER JOIN WORKSHEET w3 ON w3.WORKSHEET_ID = w1.WORKSHEET_ID
WHERE w1.EFFECTIVE_DATE =
CASE WHEN #p_MOST_RECENT_ONLY = 'Y'
THEN (
SELECT MAX(w2.EFFECTIVE_DATE)
FROM WORKSHEET_PAYROLL_VW w2
WHERE w1.ASSIGNMENT_ID = w2.ASSIGNMENT_ID AND (ISNULL(#p_WKS_EFFECTIVE_DATE,w2.EFFECTIVE_DATE) = w2.EFFECTIVE_DATE)
)
ELSE ISNULL(#p_WKS_EFFECTIVE_DATE,w1.EFFECTIVE_DATE)
END
) PERSON_WKS ON PERSON_WKS.ASSIGNMENT_ID = dbo.FN_GETRPTASSIGNMENTID(EMPLEE.PERSON_ID)
INNER JOIN (
SELECT ASSIGNMENT_ID
, VALUE
FROM ASSIGNMENT_HISTORY AH
WHERE FIELD_NAME ='HOME PAYROLL GROUP'
AND EFFECTIVE_DATE = (
SELECT MAX(EFFECTIVE_DATE)
FROM ASSIGNMENT_HISTORY
WHERE ASSIGNMENT_ID = AH.ASSIGNMENT_ID
AND EFFECTIVE_DATE <=getDate()
AND FIELD_NAME = 'HOME PAYROLL GROUP'
)
LEFT JOIN SEARCH_ASSIGNMENT_VW PERSON_ASGN ON PERSON_ASGN.ASSIGNMENT_ID = PERSON_WKS.ASSIGNMENT_ID
LEFT JOIN LOOKUP EMPLEE_ASGN_STAT ON EMPLEE_ASGN_STAT.TYPE_ = 'ASSIGNMMENT_STATUS_CODE' AND EMPLEE_ASGN_STAT.CODE = PERSON_ASGN.ASGN_STAT_CODE
) HOME_PAYROLL ON HOME_PAYROLL.ASSIGNMENT_ID = PERSON_WKS.ASSIGNMENT_ID
WHERE (#p_SELECTED_PERSON_ONLY = 'N' OR EMPLEE.PERSON_ID = #p_PERSON_ID)
AND (#p_ASGN_STAT_CODE IS NULL OR PERSON_ASGN.ASGN_STAT_CODE = SUBSTRING(#p_ASGN_STAT_CODE,1,1) OR PERSON_ASGN.ASGN_STAT_CODE = SUBSTRING(#p_ASGN_STAT_CODE,2,1))
AND (#p_POLICY_ID IS NULL OR PERSON_ASGN.PROGRAM_CODE = #p_POLICY_ID)
AND (#p_HOME_COUNTRY_ID IS NULL OR PERSON_ASGN.HOMECOUNTRYID=#p_HOME_COUNTRY_ID)
AND (#p_HOME_CITY_ID IS NULL OR PERSON_ASGN.HOMECITYID=#p_HOME_CITY_ID )
AND (#p_HOME_COMPANY_ID IS NULL OR PERSON_ASGN.HOMEBUSINESSID=#p_HOME_COMPANY_ID )
AND (#p_HOME_DIVISION_ID IS NULL OR PERSON_ASGN.HOMECOMPONENTID=#p_HOME_DIVISION_ID )
AND (#p_HOST_COUNTRY_ID IS NULL OR PERSON_ASGN.HOSTCOUNTRYID=#p_HOST_COUNTRY_ID )
AND (#p_HOST_CITY_ID IS NULL OR PERSON_ASGN.HOSTCITYID=#p_HOST_CITY_ID )
AND (#p_HOST_COMPANY_ID IS NULL OR PERSON_ASGN.HOSTBUSINESSID=#p_HOST_COMPANY_ID )
AND (#p_HOST_DIVISION_ID IS NULL OR PERSON_ASGN.HOSTCOMPONENTID=#p_HOST_DIVISION_ID )
AND (#p_CREATED_BY IS NULL OR PERSON_WKS.CREATED_BY=#p_CREATED_BY )
AND (#p_APPROVED_BY IS NULL OR PERSON_WKS.APPROVED_BY=#p_APPROVED_BY )
AND (#p_payroll_code IS NULL OR HOME_PAYROLL.VALUE=#p_payroll_code )
ORDER BY
PER.LAST_NAME ASC
, PER.FIRST_NAME ASC
, PERSON_WKS.EFFECTIVE_DATE DESC