I am building a stored procedure against a SQL 2005 database and need some help optimizing the query. It will end up running several hundred times a day so I want to make it as efficient as possible.
I ended up having to use temp tables to get where i was going, and that is what i would like to eliminate. I couldn't get the logic to pull the Top(1) appointments without first pulling the data together, ordering it appropriately, and then using Max to get the appropriate appointment returned.
The query is as follows, any assistance is appreciated in cleaning it up. Thanks in advance. MH
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[cuslrdc_LOV_NOV]
@PersonID UNIQUEIDENTIFIER,
@EncID UNIQUEIDENTIFIER,
@ProvID UNIQUEIDENTIFIER,
@EntID VARCHAR(5),
@PracID VARCHAR(4),
@LOV VARCHAR(8) OUTPUT,
@NOV VARCHAR(8) OUTPUT
AS
select a.appt_date
into #LOV
from appointments a
where a.person_id = @PersonID
and a.rendering_provider_id = @ProvID
and a.event_id in
(select event_id
from events e
where e.[event] like 'office%'
or e.[event] like 'consult%'
or e.[event] like 'reev%'
or e.[event] like 'complete ph%')
and (a.cancel_ind is null or a.cancel_ind = 'N')
and (a.delete_ind is null or a.delete_ind = 'N')
and (a.resched_ind is null or a.resched_ind = 'N')
and a.appt_kept_ind = 'Y'
and a.appt_date !=
(select CONVERT(varchar(8), pe.enc_timestamp, 112) from patient_encounter pe where pe.enc_id = @EncID)
order by a.appt_date desc
select @LOV = max(appt_date)
from #LOV
select a.appt_date
into #NOV
from appointments a
where a.person_id = @PersonID
and a.rendering_provider_id = @ProvID
and a.event_id in
(select event_id
from events e
where e.[event] like 'office%'
or e.[event] like 'consult%'
or e.[event] like 'reev%'
or e.[event] like 'complete ph%')
and (a.cancel_ind is null or a.cancel_ind = 'N')
and (a.delete_ind is null or a.delete_ind = 'N')
and (a.resched_ind is null or a.resched_ind = 'N')
and a.appt_kept_ind != 'Y'
and a.appt_date > (CONVERT(varchar(8), getdate(), 112)-1)
order by a.appt_date asc
select @NOV = max(appt_date)
from #NOV
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
I ended up having to use temp tables to get where i was going, and that is what i would like to eliminate. I couldn't get the logic to pull the Top(1) appointments without first pulling the data together, ordering it appropriately, and then using Max to get the appropriate appointment returned.
The query is as follows, any assistance is appreciated in cleaning it up. Thanks in advance. MH
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[cuslrdc_LOV_NOV]
@PersonID UNIQUEIDENTIFIER,
@EncID UNIQUEIDENTIFIER,
@ProvID UNIQUEIDENTIFIER,
@EntID VARCHAR(5),
@PracID VARCHAR(4),
@LOV VARCHAR(8) OUTPUT,
@NOV VARCHAR(8) OUTPUT
AS
select a.appt_date
into #LOV
from appointments a
where a.person_id = @PersonID
and a.rendering_provider_id = @ProvID
and a.event_id in
(select event_id
from events e
where e.[event] like 'office%'
or e.[event] like 'consult%'
or e.[event] like 'reev%'
or e.[event] like 'complete ph%')
and (a.cancel_ind is null or a.cancel_ind = 'N')
and (a.delete_ind is null or a.delete_ind = 'N')
and (a.resched_ind is null or a.resched_ind = 'N')
and a.appt_kept_ind = 'Y'
and a.appt_date !=
(select CONVERT(varchar(8), pe.enc_timestamp, 112) from patient_encounter pe where pe.enc_id = @EncID)
order by a.appt_date desc
select @LOV = max(appt_date)
from #LOV
select a.appt_date
into #NOV
from appointments a
where a.person_id = @PersonID
and a.rendering_provider_id = @ProvID
and a.event_id in
(select event_id
from events e
where e.[event] like 'office%'
or e.[event] like 'consult%'
or e.[event] like 'reev%'
or e.[event] like 'complete ph%')
and (a.cancel_ind is null or a.cancel_ind = 'N')
and (a.delete_ind is null or a.delete_ind = 'N')
and (a.resched_ind is null or a.resched_ind = 'N')
and a.appt_kept_ind != 'Y'
and a.appt_date > (CONVERT(varchar(8), getdate(), 112)-1)
order by a.appt_date asc
select @NOV = max(appt_date)
from #NOV
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO