?
Solved

Need help with a qry's efficiency

Posted on 2009-05-15
13
Medium Priority
?
212 Views
Last Modified: 2012-05-07
Friends,

I have a qry (attached) that is taking forever to run.   Can anyone look to see where the problem lies?  It really runs into trouble with the Order By.  If I take out the group by and the order by statement, it works great, it just doesn't order it right (obviously).

Thanks in advance!

Best Regards,
Eric
Select
t.TrackOrder,
RTRIM(r.No) as CarNumber,
p.ResultItemID,
p.PassingTime,
p.ElapsedTime,
p.PassingID,
p.DecoderID,
p.LeaderLap, 
p.LapCount 
From Passings p with (nolock) inner join Results r with (nolock)
ON p.RunID=r.RunID
AND p.ResultItemID=r.ResultID
Inner Join Timelines t 
on t.DecoderID=p.DecoderID
AND t.LoopID=p.LoopID
Where 
p.Pit = 0
And 
passingtime in (SELECT max(i.passingtime) FROM passings i with (NOLOCK) WHERE
    i.ResultItemID = p.ResultItemID
    AND i.passingtime >=0
    AND i.lapcount>0
    AND i.Deleted=0
    AND p.Deleted=0
    AND i.RunID=6
	AND p.RunID=6
    AND i.DecoderID<> 0)--(Select Top 1 DecoderID From Timelines where IsLapCounter=1))
And p.RunID=6
And r.RunID=6
Group by p.ResultItemID, 
p.PassingTime, 
p.ElapsedTime, 
p.PassingID, 
p.DecoderID, 
p.LapCount, 
p.LeaderLap, 
t.TrackOrder,
r.No, 
r.RaceRank
Order By t.TrackOrder Desc, p.ElapsedTIme

Open in new window

0
Comment
Question by:indy500fan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
13 Comments
 
LVL 6

Expert Comment

by:bull_rider
ID: 24397616
You have missed one more WITH (NOLOCK) in this statement

Inner Join Timelines t

Can you add and check?
0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 24397619
pls post the table structue including the indexes
0
 
LVL 9

Expert Comment

by:cdaly33
ID: 24397630
You have a couple options.  You could try putting an index on the t.TrackOrder and p.ElapsedTIme fields which may or may not help depending on the amount of data.  Or you can try to find a way to sort it after the data has been retrieved such as in the program utilizing the data.  Or you if this is in a stored procedure you could create a temp table variable matching the structure that is returned from the statement and then select out of that temp table variable with the appropriate sorting applied then.
0
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 

Author Comment

by:indy500fan
ID: 24397695
ttached is the passings table structure.  By the way, the data inside the Timelines table doesn't change.

SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Passings](
	[RunID] [int] NOT NULL,
	[PassingID] [int] NOT NULL,
	[FlagState] [int] NOT NULL,
	[DecoderID] [int] NOT NULL,
	[TranNr] [int] NOT NULL,
	[PassingTime] [int] NOT NULL,
	[PhotocellTime] [int] NOT NULL,
	[ElapsedTime] [int] NOT NULL,
	[Hits] [int] NOT NULL,
	[Strength] [int] NOT NULL,
	[LowBatt] [bit] NOT NULL,
	[LoopID] [int] NOT NULL,
	[Pit] [bit] NOT NULL,
	[Deleted] [bit] NOT NULL,
	[Manual] [bit] NOT NULL,
	[Photocell] [bit] NOT NULL,
	[PhotocellState] [int] NOT NULL,
	[Flag] [bit] NOT NULL,
	[LeaderLap] [int] NOT NULL,
	[LapCount] [int] NOT NULL,
	[Laptime] [int] NOT NULL,
	[LaptimeViolation] [bit] NOT NULL,
	[Finish] [bit] NOT NULL,
	[ResultItemID] [int] NOT NULL,
	[EventID] [int] NOT NULL DEFAULT ((1000)),
	[SeasonID] [int] NOT NULL DEFAULT ((1001))
) ON [PRIMARY]
 
/****** Object:  Index [PASSINGID_INDEX]    Script Date: 05/15/2009 12:45:05 ******/
CREATE NONCLUSTERED INDEX [PASSINGID_INDEX] ON [dbo].[Passings] 
(
	[PassingID] DESC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
 
/****** Object:  Table [dbo].[Results]    Script Date: 05/15/2009 12:45:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Results](
	[RunID] [int] NOT NULL,
	[ResultID] [int] NOT NULL,
	[TranNr] [int] NOT NULL,
	[Deleted] [bit] NOT NULL,
	[ExtraTotaltime] [int] NOT NULL,
	[ExtraLapcount] [int] NOT NULL,
	[ExtraBestLaptime] [int] NOT NULL,
	[OverallBestLaptime] [int] NOT NULL,
	[ResultStatus] [int] NOT NULL,
	[StartPosition] [int] NOT NULL,
	[Comment] [char](255) NOT NULL,
	[QualifyRank] [int] NOT NULL,
	[RaceRank] [int] NOT NULL,
	[BestLaptime] [int] NOT NULL,
	[BestLap] [int] NOT NULL,
	[BestSpeed] [float] NOT NULL,
	[SecondBestLaptime] [int] NOT NULL,
	[SecondBestLap] [int] NOT NULL,
	[SecondBestSpeed] [float] NOT NULL,
	[LastLaptime] [int] NOT NULL,
	[LastSpeed] [float] NOT NULL,
	[Laps] [int] NOT NULL,
	[Totaltime] [int] NOT NULL,
	[DiffQual] [char](70) NOT NULL,
	[GapQual] [char](70) NOT NULL,
	[DiffRace] [char](70) NOT NULL,
	[GapRace] [char](70) NOT NULL,
	[CompetitorID] [int] NOT NULL,
	[RaceMarker] [int] NOT NULL,
	[QualMarker] [int] NOT NULL,
	[AvgSpeed] [float] NOT NULL,
	[OnTrack] [int] NOT NULL,
	[PitStops] [int] NOT NULL,
	[LastPitLap] [int] NOT NULL,
	[SincePit] [int] NOT NULL,
	[SincePitLap] [int] NOT NULL,
	[FlagStatus] [char](255) NOT NULL,
	[No] [char](70) NOT NULL,
	[FirstName] [char](70) NOT NULL,
	[LastName] [char](70) NOT NULL,
	[Class] [char](70) NOT NULL,
	[DriverID] [char](70) NOT NULL,
	[TrpndrCtrl#] [char](70) NOT NULL,
	[Equipment] [char](70) NOT NULL,
	[License] [char](70) NOT NULL,
	[Team] [char](70) NOT NULL,
	[TeamID] [char](70) NOT NULL,
	[Engine] [char](70) NOT NULL,
	[EntrantID] [char](70) NOT NULL,
	[EventID] [int] NOT NULL DEFAULT ((1000)),
	[SeasonID] [int] NOT NULL DEFAULT ((1001))
 
) ON [PRIMARY]
 
GO
SET ANSI_PADDING OFF
 
GO
/****** Object:  Index [RESULT_RUNID_INDEX]    Script Date: 05/15/2009 12:47:15 ******/
CREATE NONCLUSTERED INDEX [RESULT_RUNID_INDEX] ON [dbo].[Results] 
(
	[RunID] DESC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
 
 
/****** Object:  Table [dbo].[Timelines]    Script Date: 05/15/2009 12:48:11 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Timelines](
	[m_UniqueID] [int] NOT NULL,
	[TrackID] [int] NOT NULL,
	[Name] [char](70) NOT NULL,
	[DecoderID] [int] NOT NULL,
	[LoopID] [int] NOT NULL,
	[TrackOrder] [int] NOT NULL,
	[DistanceFromSF] [float] NOT NULL,
	[IsSendToFeed] [bit] NOT NULL,
	[IsLapCounter] [bit] NOT NULL,
	[IsInPit] [bit] NOT NULL,
	[IsPitIn] [bit] NOT NULL,
	[IsPitOut] [bit] NOT NULL,
	[IsFirstAfterPitOut] [bit] NOT NULL,
	[IsBlendline] [bit] NOT NULL,
	[EventID] [int] NOT NULL DEFAULT ((1000)),
	[SeasonID] [int] NOT NULL DEFAULT ((1001))
) ON [PRIMARY]
 
GO
SET ANSI_PADDING OFF

Open in new window

0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 24397870
Question?
Do you really need a GROUP BY part ?
I am not sure about the size of and values on the timelines table, can u create the following index on that table and try it..
if possibgle post the execution plan too
create index idx_Timelines_DecoderID_LoopID_TrackOrder ON TimeLines (DecoderID) INCLUDE (LoopID,TrackOrder)  
0
 

Author Comment

by:indy500fan
ID: 24397947
aneeshattingal,

I have tried it with the index.  No change.  Execution Plan attached as image.
ExecutionPlan.bmp
0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 24398005
You need to create indexes on the RUNID's (both the tables)
Wgy dont you have a Primary key / Clustured indexes on those tables /
0
 

Author Comment

by:indy500fan
ID: 24398052
These tables are created by 3rd pary software  I'm not very good with DB's, and I'm weary about creating indexes.  I have very little sql experience.

Do you have suggestions as to creating the indexes?  Which fields on each table or do I create a new field and seed it?
0
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 2000 total points
ID: 24398148
create index idx_Results_ResultID_RunID ON Results (RunID) INCLUDE (ResultID)  
create index idx_Passings_ResultItemID_RunID ON Passings (RunID) INCLUDE (ResultItemID)
0
 

Author Comment

by:indy500fan
ID: 24398208
Interesting.  I ran it and it was faster against today's dataset, but I need to try one more test tomorrow morning with that dataset.

Thank you for your help so far!

Regards,
Eric
0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 24398233
the query can be even faster with a primary key &clustured index, as a rule you should make sure that you have a primary key for all the tables
0
 

Author Comment

by:indy500fan
ID: 24398280
I may try that!  I really think we are on the right path!
0
 

Author Comment

by:indy500fan
ID: 24402936
The indexes did the trick!  Thank you!
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you having speed problem in loading SQL Server Management Studio, try to uncheck these options in your internet browser (IE -> Internet Options / Advanced / Security):    . Check for publisher's certificate revocation    . Check for server ce…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question