SQL Query Optimisation

Posted on 2012-04-03
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     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)
Question by:mgordon-spi
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
  • 4
  • 2
  • 2
LVL 69

Accepted Solution

Scott Pletcher 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

Author Comment

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.
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...
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users


Author Comment

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?
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.

Author Comment

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

Author Closing Comment

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

LVL 69

Expert Comment

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

Yes, definitely!

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

I have a large data set and a SSIS package. How can I load this file in multi threading?
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to shrink a transaction log file down to a reasonable size.

617 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