Solved

SQL Query Optimisation

Posted on 2012-04-03
8
307 Views
Last Modified: 2012-04-04
I have a query that is selecting the last known Latitude and Longitude along with the Name from a table. At the moment the query is taking over 10 seconds to execute with only 18 Units to sort through, I can see this being a rather large issue when there are 300+ Units.

So I need a way to optimise this Query to execute much faster.

SELECT DISTINCT T1.ID, T1.sVesselName,
                          (SELECT     TOP (1) Latitude
                            FROM          tGPSData AS T3
                            WHERE      (VesselID = T1.ID)
                            ORDER BY ID DESC) AS LastLat,
                          (SELECT     TOP (1) Longitude
                            FROM          tGPSData AS T3
                            WHERE      (VesselID = T1.ID)
                            ORDER BY ID DESC) AS LastLon
FROM         tUser INNER JOIN
                      tUserVesselMap ON tUser.ID = tUserVesselMap.iUserID INNER JOIN
                      tVesselMaster AS T1 ON tUserVesselMap.iVesselID = T1.ID
WHERE     (T1.bEnabled = 1) AND (tUser.sUsername = @Username)
0
Comment
Question by:mgordon-spi
  • 4
  • 2
  • 2
8 Comments
 
LVL 69

Accepted Solution

by:
ScottPletcher earned 500 total points
ID: 37803956
For *this* query -- not taking into account any other workload on the tables -- you need to have these indexes:

tUser (sUsername) --preferably clustered
tUserVesselMap (iUserID)
tVesselMaster (ID)
tGPSData (VesselID, bEnabled ) --if bEnabled is bit, make it a tinyint instead
0
 

Author Comment

by:mgordon-spi
ID: 37804014
I'm sorry - I dont quite understand your response?

I was more referring to the fact that I was using two nested select statements to retrieve the last record in the GPS table for each vessel. I was wondering if there was a faster way to do that?

The GPSData table holds hundreds of thousands of records for many vessels.

Untimately I am trying to retrieve a list of vessels and their last known location. This query achieves that but takes about 20 seconds to execute.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 37804200
I was wondering if there was a faster way to do that?
Yes, as Scott has pointed out it is called indexes.  Until you have confirmed the existence of those indexes, there is not a lot we can add...
0
 

Author Comment

by:mgordon-spi
ID: 37804406
I'm sorry but this might be stretching my SQL abilities. Jsut to confirm we are not talking about primary keys?

I have not created indexes before. Will the creation of them help with query execute speed?
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 37804525
Jsut to confirm we are not talking about primary keys?
Maybe, but not necessarily.

Will the creation of them help with query execute speed?
Yes.  Although you should be aware that too many indexes can slow down INSERTs and UPDATEs, But I suspect you are a long way from that.
0
 

Author Comment

by:mgordon-spi
ID: 37804535
So it is as simple as creating indexes (the ones mentioned above) and I should see a difference?
0
 

Author Closing Comment

by:mgordon-spi
ID: 37804592
Your help has directed me to learn a whole new aspect of SQL. Indexing!

Cheers.
0
 
LVL 69

Expert Comment

by:ScottPletcher
ID: 37806452
>> creating indexes ... and I should see a difference? <<

Yes, definitely!
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

760 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now