Awash In A Sea Of Sub-Queries: Optimising SQL

By David Dennis
5th March 2024

Optimising SQL code can be a complicated and frustrating process. Recently, we were working with a client that had a critical SQL script which they needed assistance with. The script needed to collate data from 4 different data providers, and format it for insertion into a database table. They had a prototype of the script which produced the correct results but took over 30 minutes to complete.

We were asked to investigate and speed the script up. At 6000 lines of SQL, returning around 130 columns, understanding the code was a job in itself, but upon initial inspection there were a few areas that could be improved:

  1. Sub queries were being used throughout, in a script of this size, this would create a heavy load on the SQL server.
  2. Many of the subqueries were repeated throughout the code, making for a wasteful use of the database resources.
  3. Having such a big query makes future maintenance confusing and frustrating.

The original intention was for the query to be created as a view. There can be advantages in using views, such as the ease of use for the end of user – views can be queryable just as if they were themselves a database table – but in this case it would have limited the potential for optimising the code. Replacing sub queries in this case with both temporary tables and variables would allow us cut down the execution time drastically. But using views in SQL prohibits the use of both, so we changed the view to a stored procedure.

This, combined with other changes, cut the execution time of the code to around 20 seconds.

We discussed with the customer their future requirements for the query. We agreed with such a large query, it would be desirable to make it easily maintainable for them in future, therefore it made sense to split out the code for each data provider into their own script. This was done with the idea that if they needed to add a new provider in future, adding a separate script would be more straightforward rather than trying to edit the existing one.

If you have long running queries and would like us to assist with optimising them, why don’t you get in touch? Contact us

<< Back to Articles

Here's what other people think

Google Rating
5.0
High standard, professional service. I've worked with Welldata for a number of years and always found their DBA's to be knowledgeable and proactive. Throw any question at them and it will be resolved, it gives real peace of mind knowing you have a partner you can rely on from emergencies to general questions. They also like a challenge and we do throw a few curved balls at them.read more
Paul Miller
Paul Miller
12:34 27 Aug 21
The support I receive from Well Data is extremely responsive. Richard Parsons is my contact and keeps me informed daily on the health of our SQL Databases. Whenever I ask for assistance from Well Data it is quick and professional.read more
Kay Riley
Kay Riley
13:41 19 Aug 21
Really know their stuff and their response times are very good. They provide us with a vital service.
Andy Cole
Andy Cole
13:36 19 Aug 21
The guys at WellData really know their stuff. We wanted a quick route to doing something complex and they sorted it for us very quickly and to a high standard. Very professional, no ego, just excellent and to the point consultancy.read more
Steve Goacher
Steve Goacher
11:31 14 Nov 19
I have worked with the WellData team for many years now and they have always delivered.I would highly recommend them to any business looking for best-value outsourced Expert DBA’s.Being able to offer a rolling contract on the basis that if they don’t deliver you can leave shows their confidence and commitment.Their professionalism and knowledge is second to none.read more
Petro Bartoszyk
Petro Bartoszyk
08:51 18 Oct 19
A very dedicated team of extremely professional and knowledgeable experts
Giulia Iannucci
Giulia Iannucci
14:08 16 Nov 18
We use Welldata for SQL support and are very happy with their service.
David Sadler
David Sadler
09:40 15 Nov 18
Really know their stuff and their response times are very good.
Andy Cole
Andy Cole
11:45 06 Nov 18
Our partnership with WellData has enabled us to resolve critical database problems. Their attitude and service makes them a natural extension to our own technology team, which extends our capability in a critical area of our business.read more
Steve Fenton
Steve Fenton
10:56 05 Nov 18
Having worked with the team at Welldata for approaching 17 years, my company and I have always found them professional, reliable and generally a great firm to work with.read more
John Lynes
John Lynes
14:00 04 Oct 17
WellData provide an excellent level of support and their team are very knowledgeable and always go the extra mile. I would wholeheartedly recommend them.read more
Stefan Parrott
Stefan Parrott
13:12 19 Sep 17