Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Stored procedure design issues

Author  Topic 

Autofreak
Starting Member

9 Posts

Posted - 2011-12-23 : 15:08:33
Hello, this SP must return at least 10 records with businesses that have been updated or added most recently. It starts with today's date range and looks for the business records. If it finds less than 10 records, the date range is increased by a day, adding yesterday, and does the same search again, and so on increasing the range, until at least 10 records is found.

Here is the algorithm/pseudo-code:

0 begin

1 do while found=false

2 select most recently updated/added business records into a temp db

3 check how many records have been returned

4 if less than 10

delete all records from the temp table

increase data range by 1 day going backword

go to 2

5 else

select * from the temp table

6 found=true

7 end

I have a couple of problems with the SP (posed below)

1. It's very slow - 10-20 sec. The execution plan shows that 50-70% is spent on Table Insert operations. I was wondering if the design could be changed and the the same could accomplished without using the temp table. BTW, In the dev database there are 8 iterations that the SP does before it finds 10 records.

2. I am not sure why, but in the data layer when a DataReader gets the results back there are things that I can's explain

a. when I run the SP on the SQL Server, 117 records returned, the DataReader returns 156

b. DataReader.Read proparty is set to False and no iteration can be done



I am posting the procedure below. I am looking for suggestions on how to tune-up/improve/fix the existing design or redesign it using more efficient ways like CTE or sub-queries. Thank you.

USE [Goldbook23]
GO
/****** Object: StoredProcedure [dbo].[GetLatestListingUpdates] Script Date: 12/23/2011 13:29:38 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER OFF
GO
ALTER PROCEDURE [dbo].[GetLatestListingUpdates]

AS

IF OBJECT_ID('tempdb..#tempListings') IS NOT NULL DROP TABLE #tempListings
create table #tempListings(
BusinessID int not null,
Name nvarchar(256) not null,
CreateDate datetime,
IsMain bit not null,
StreetAddress varchar(256) not null,
City varchar(256) not null,
PostalCode varchar(10),
Province varchar(20),
ProvinceCode varchar(5),
ActionDescription varchar(50),
[Status] varchar(10) not null,
UpdateDate datetime)

declare @c int=0
declare @date datetime = getdate()
declare @found bit=0
while @found = 0 --repeat the loop untill at least 10 listings have been found
begin
insert into #tempListings
select a.BusinessID, a.Name, a.CreateDate, b.IsMain, c.StreetAddress, d.Name as City, c.PostalCode,
f.Name as Province, f.Code as PovinceCode, h.Name as 'ActionDescription',
Case when h.Name like '%update%' Then 'Update'
when h.Name like '%added%' Then 'Update'
when h.Name like '%deleted%' Then 'Update'
when h.Name like '%created%' Then 'Added'
else 'Added'
End as [Status],
g.UpdateDate
from gb_business.Business a left join gb_business.BusinessLocation b on a.BusinessID = b.BusinessID
left join gb_location.Address c on c.AddressID = b.AddressID
left join gb_location.City d on c.CityID = d.CityID
left join gb_location.Region e on d.RegionID= e.RegionID
left join gb_location.Province f on f.ProvinceID=e.ProvinceID
left join gb_business.ActionLog g on g.ActionEntityPrimaryKeyID=a.BusinessID
left Join gb_business.ActionType h on h.ActionTypeID = g.ActionTypeID
where c.CityID is not Null -- must have value for City
and c.PostalCode is not Null and c.PostalCode <>'' and c.PostalCode <>' ' -- must have value for PostalCode
and c.StreetAddress<>'' and c.StreetAddress<>' ' -- must have value for StreetAddress
and g.ActionEntityID=1 -- changes to Business entity
and a.ShowAddress=1 --address visibility indicator - True
and (h.Name like '%deal%' or h.Name like '%name%' or h.Name like '%address%' or h.Name like '%photo%'
or h.Name like '%video%' or h.Name like '%created customer profile%') --must have changes to at least one of the following attributes
and (g.UpdateDate > @date or a.CreateDate > @date); -- filtering the most recent updates starting from today
--order by g.UpdateDate desc, a.Name;
set @c=@@rowcount;
/*if the result set returned is less than 10 records, include one more day in the search criteria*/
if @c<10
begin
set @date = @date -1
delete from #tempListings
end
else
begin
select * from #tempListings
set @found=1;
end
end;


visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-12-23 : 23:39:42
i think what you need is just this

insert into #tempListings
select BusinessID, Name, CreateDate, IsMain, StreetAddress, City, PostalCode,
Province, PovinceCode, ActionDescription,[Status],UpdateDate
from
(
select *,dense_rank() over (order by g.UpdateDate desc, a.Name) as rnk
from
(
select count(1) over () as rwcnt,
a.BusinessID, a.Name, a.CreateDate, b.IsMain, c.StreetAddress, d.Name as City, c.PostalCode,
f.Name as Province, f.Code as PovinceCode, h.Name as 'ActionDescription',
Case when h.Name like '%update%' Then 'Update'
when h.Name like '%added%' Then 'Update'
when h.Name like '%deleted%' Then 'Update'
when h.Name like '%created%' Then 'Added'
else 'Added'
End as [Status],
g.UpdateDate
from gb_business.Business a left join gb_business.BusinessLocation b on a.BusinessID = b.BusinessID
left join gb_location.Address c on c.AddressID = b.AddressID
left join gb_location.City d on c.CityID = d.CityID
left join gb_location.Region e on d.RegionID= e.RegionID
left join gb_location.Province f on f.ProvinceID=e.ProvinceID
left join gb_business.ActionLog g on g.ActionEntityPrimaryKeyID=a.BusinessID
left Join gb_business.ActionType h on h.ActionTypeID = g.ActionTypeID
where c.CityID is not Null -- must have value for City
and c.PostalCode is not Null and c.PostalCode <>'' and c.PostalCode <>' ' -- must have value for PostalCode
and c.StreetAddress<>'' and c.StreetAddress<>' ' -- must have value for StreetAddress
and g.ActionEntityID=1 -- changes to Business entity
and a.ShowAddress=1 --address visibility indicator - True
and (h.Name like '%deal%' or h.Name like '%name%' or h.Name like '%address%' or h.Name like '%photo%'
or h.Name like '%video%' or h.Name like '%created customer profile%') --must have changes to at least one of the following attributes
and (g.UpdateDate > @date or a.CreateDate > @date); -- filtering the most recent updates starting from today
)t
where rwcnt >10
)r
where rnk=1


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -