Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

How to improve a aquery Expand / Collapse
Author
Message
Posted Thursday, September 26, 2013 3:49 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, February 11, 2014 4:03 AM
Points: 4, Visits: 43
Good morning,
I am writing a query thaht bring data from 7 tables.
In the first statge the data is stored in table variables.
Then I use 7 join to produce the data that I want to see.
The query takes 1 minute and 50 second to brin 22 records.
Could you please advise me how I can improve the performance of this query?
Thanks a lot,
Nubia
Post #1498743
Posted Thursday, September 26, 2013 3:56 AM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Sunday, August 24, 2014 11:15 PM
Points: 463, Visits: 604
need table structure and some sample data.
and Most. Imp. Your Query...!!!
Post #1498745
Posted Thursday, September 26, 2013 3:56 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Today @ 8:36 AM
Points: 1,541, Visits: 8,219
The best way to get help with this is to include the code in question along with the definitions of the tables and some test data. Without those we're just guessing.

Have a look at the following article on how to post data that will help us to help you:
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/



BrainDonor
Linkedin
Blog Site
Post #1498746
Posted Thursday, September 26, 2013 4:17 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, February 11, 2014 4:03 AM
Points: 4, Visits: 43
Hello guys,

Thanks you for your reply. This is the definition of the sp. The data is taken from a ingress db. Unfotonately I don't have acces to the tables.

CREATE PROCEDURE [dbo].[usp_RPT_HSG_PropertyRepairsHistoryTSL]
(
@PropertyReferenceNumber NVARCHAR (max),
@StartDate DATETIME,
@EndDate DATETIME,
@GetCHRepType NVARCHAR (20)


)
AS
BEGIN

SET NOCOUNT ON

-- ===================================================
-- Execute no-lock expression at named linked servers
-- ===================================================
EXECUTE ('set lockmode session where readlock=nolock') AT INGRES2
DECLARE @PropertyRepairsHistory Table
(
Jobnumber INT
, PropertyReference NVARCHAR (max)
, JobTitle nvarchar (60)
, Priority nvarchar (20)
, JobType nvarchar (20)
, Contractorcode nvarchar (4)
, Contractor nvarchar (30)
, BilledValue MONEY
, ValuePaid MONEY
, StageHistDate DATE
, JobType2 NVARCHAR (20)


)

INSERT INTO @PropertyRepairsHistory

Select * FROM OPENQUERY (ingres2, '

SELECT repheader.repairno
, repheader.propref
, repheader.repairdesc
, priority.decode
, reptype.code
, workforce.wforcecode
, workforce.wforcename
, worderline.billval
, worderline.payval
, statehist.statedate
, reptype.decode AS JobType


FROM ((repheader INNER JOIN workorder ON repheader.repairno = workorder.repairno) inner JOIN worderline ON workorder.repairno = worderline.repairno AND workorder.repsubno = worderline.repsubno)
LEFT OUTER JOIN reptype ON reptype.code = repheader.reptypenow
LEFT OUTER JOIN priority ON priority.code = workorder.prioritycode
LEFT OUTER JOIN statehist ON statehist.repaltkey = repheader.repairno
LEFT OUTER JOIN workforce ON repheader.wforcecode = workforce.wforcecode
WHERE statehist.repstatecode = 99

')

DECLARE @Property Table
(
PropertyRef nvarchar(max)
, HouseNo NVARCHAR (4)
, AddressLine1 nvarchar (30)
, AddressLine2 nvarchar (30)
, AddressLine3 nvarchar (30)
, Postcode nvarchar (8)
, Officename nvarchar (max)
, Officecode nvarchar (max)



)
INSERT INTO @Property
Select * FROM OPENQUERY (ingres2, '

SELECT
propfixed.propref
,propfixed.houseno
, propfixed.proadd1
, propfixed.proadd2
, propfixed.proadd3
, propfixed.propstcde
,mnode.mnodename
, propfixed.mnodecode


FROM propfixed inner join mnode ON propfixed.mnodecode = mnode.code

' )
-- =======================
-- OutPut for Detail
-- =======================

SELECT *
FROM @PropertyRepairsHistory r inner join @Property P on R.PropertyReference = P.PropertyRef
WHERE r.StageHistDate BETWEEN @StartDate AND @EndDate
AND r.JobType IN (SELECT * FROM fn_CSVToTable(@GetCHRepType))
AND p.PropertyRef = @PropertyReferenceNumber

ORDER BY Jobnumber DESC

Thanks,
Nubia

Post #1498751
Posted Thursday, September 26, 2013 6:04 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, February 11, 2014 4:03 AM
Points: 4, Visits: 43
Hi Briandonor,

I attach you the Estimated Execution plan and the number of rows per table.

According to the execution plan the more expensive statement is:
INSERT INTO @Property
Select * FROM OPENQUERY (ingres2, '

SELECT
propfixed.propref
,propfixed.houseno
, propfixed.proadd1
, propfixed.proadd2
, propfixed.proadd3
, propfixed.propstcde
,mnode.mnodename
, propfixed.mnodecode


FROM propfixed inner join mnode ON propfixed.mnodecode = mnode.code

I don't understand why if the statement is using a join table to bring data froma look up table that only has 20 records. the propfixed table has only 88923 records. Is there any other way to bring data from a look up table?

Thanks a lot,

Nubia


  Post Attachments 
Data sample.xlsx (4 views, 11.08 KB)
EstimatedExecutionPlan1.sqlplan (12 views, 71.21 KB)
Post #1498786
Posted Monday, September 30, 2013 3:03 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Friday, August 15, 2014 9:43 AM
Points: 32, Visits: 401
I believe you can get rid of the table variables altogether. Add the inner join used to populate the second table variable to the beginning of the first query to populate the first table variable and then add the additional "WHERE" clauses in the final query.
Post #1500201
Posted Tuesday, October 1, 2013 3:37 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 9:49 AM
Points: 7,234, Visits: 13,719
How long do the two remote queries take to run and how many rows are returned?

SELECT * 
FROM OPENQUERY (ingres2, '
SELECT repheader.repairno
, repheader.propref
, repheader.repairdesc
, priority.decode
, reptype.code
, workforce.wforcecode
, workforce.wforcename
, worderline.billval
, worderline.payval
, statehist.statedate
, reptype.decode AS JobType

FROM ((repheader
INNER JOIN workorder ON repheader.repairno = workorder.repairno)
inner JOIN worderline ON workorder.repairno = worderline.repairno
AND workorder.repsubno = worderline.repsubno)
LEFT OUTER JOIN reptype ON reptype.code = repheader.reptypenow
LEFT OUTER JOIN priority ON priority.code = workorder.prioritycode
LEFT OUTER JOIN statehist ON statehist.repaltkey = repheader.repairno
LEFT OUTER JOIN workforce ON repheader.wforcecode = workforce.wforcecode
WHERE statehist.repstatecode = 99')

SELECT *
FROM OPENQUERY (ingres2, '
SELECT
propfixed.propref
, propfixed.houseno
, propfixed.proadd1
, propfixed.proadd2
, propfixed.proadd3
, propfixed.propstcde
, mnode.mnodename
, propfixed.mnodecode
FROM propfixed
inner join mnode ON propfixed.mnodecode = mnode.code' )

At least one filter from your output detail section could be applied to the remote queries.


“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Post #1500310
Posted Tuesday, October 1, 2013 3:56 AM


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: Today @ 3:23 PM
Points: 15,664, Visits: 28,062
You're pumping 20000 rows (estimated, actual could be more) into table variables, where there are no statistics. Then you're combining that with a multi-statement table valued function, which also doesn't have statistics, to filter down to 20 rows. I'm surprised it's running as fast as it actually is.

You need to filter the data sooner as was suggested to you. Loading the data into table variables is probably unnecessary, but if you must do it that way, you're better off using temporary tables so that statistics can be generated to arrive a better execution plan.


----------------------------------------------------
"The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood..." Theodore Roosevelt
The Scary DBA
Author of: SQL Server 2012 Query Performance Tuning
SQL Server 2008 Query Performance Tuning Distilled
and
SQL Server Execution Plans

Product Evangelist for Red Gate Software
Post #1500325
Posted Tuesday, October 1, 2013 4:06 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Today @ 8:36 AM
Points: 1,541, Visits: 8,219
Thanks for that Grant. I saw the Table Variables but it just didn't register. "Looked But Failed To See" - it will be on my headstone one day.

BrainDonor
Linkedin
Blog Site
Post #1500331
Posted Tuesday, October 1, 2013 4:29 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 9:49 AM
Points: 7,234, Visits: 13,719
Are the NVARCHAR(MAX) column types necessary?
DECLARE @PropertyRepairsHistory Table
(
Jobnumber INT
, PropertyReference NVARCHAR (max) --
, JobTitle nvarchar (60)
, Priority nvarchar (20)
, JobType nvarchar (20)
, Contractorcode nvarchar (4)
, Contractor nvarchar (30)
, BilledValue MONEY
, ValuePaid MONEY
, StageHistDate DATE
, JobType2 NVARCHAR (20)
)

DECLARE @Property Table
(
PropertyRef nvarchar(max) --
, HouseNo NVARCHAR (4)
, AddressLine1 nvarchar (30)
, AddressLine2 nvarchar (30)
, AddressLine3 nvarchar (30)
, Postcode nvarchar (8)
, Officename nvarchar (max) --
, Officecode nvarchar (max) --
)



“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Post #1500336
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse