September 26, 2013 at 3:49 am
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
September 26, 2013 at 3:56 am
need table structure and some sample data.
and Most. Imp. Your Query...!!! 🙂
September 26, 2013 at 3:56 am
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/
September 26, 2013 at 4:17 am
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]
(
@PropertyReferenceNumberNVARCHAR (max),
@StartDateDATETIME,
@EndDateDATETIME,
@GetCHRepTypeNVARCHAR (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
(
JobnumberINT
,PropertyReferenceNVARCHAR (max)
,JobTitlenvarchar (60)
,Prioritynvarchar (20)
,JobTypenvarchar (20)
,Contractorcodenvarchar (4)
,Contractornvarchar (30)
,BilledValueMONEY
,ValuePaid MONEY
,StageHistDateDATE
,JobType2NVARCHAR (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 ONrepheader.repairno = workorder.repairno) inner JOIN worderlineON workorder.repairno = worderline.repairno AND workorder.repsubno = worderline.repsubno)
LEFT OUTER JOIN reptype ON reptype.code = repheader.reptypenow
LEFT OUTER JOIN priorityONpriority.code = workorder.prioritycode
LEFT OUTER JOIN statehistON statehist.repaltkey = repheader.repairno
LEFT OUTER JOIN workforceON repheader.wforcecode = workforce.wforcecode
WHERE statehist.repstatecode = 99
')
DECLARE @Property Table
(
PropertyRefnvarchar(max)
,HouseNoNVARCHAR (4)
,AddressLine1nvarchar (30)
,AddressLine2nvarchar (30)
,AddressLine3nvarchar (30)
,Postcodenvarchar (8)
,Officenamenvarchar (max)
,Officecodenvarchar (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 propfixedinner 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
September 26, 2013 at 6:04 am
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 propfixedinner 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
September 30, 2013 at 3:03 pm
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.
October 1, 2013 at 3:37 am
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.
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
October 1, 2013 at 3:56 am
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
Author of:
SQL Server Execution Plans
SQL Server Query Performance Tuning
October 1, 2013 at 4:29 am
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) --
)
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
October 1, 2013 at 7:34 am
Good afternoon,
Thank you very much for your help.
I found some problems in the queries and lots of duplicates. I managed to spped up the query from 1 min. 30 seconds to 30 seconds using a different aproach.
Cheers,
Nubia:-)
Viewing 11 posts - 1 through 10 (of 10 total)
You must be logged in to reply to this topic. Login to reply