Image: SQL query
A simple query created in SQL Server
R E L A T E D   C O N T E N T
ADVERTISEMENT

Hands on: Understanding SQL queries

Discover how SQL queries are executed and decide whether to upgrade to SQL Server

Mark Whitehorn, Personal Computer World 30 Dec 2008
ADVERTISEMENT

All relational databases use SQL (Structured Query Language) to query the data. But when you think about it (and even when you don’t), SQL belongs to an odd class of computer language because it is used to tell the database what to do, but not how to do it.

So, at some point the engine itself has to decide how it should carry out our wishes. Understanding how it does this is vital to be able to tune databases for speed.
So, stepping back a fraction, there are essentially two classes of programming language: procedural and declarative. SQL is declarative and is also an excellent illustration of the difference between the two classes.

In a procedural language (such as C++ or Java) we specify the steps the computer must follow in order to solve the problem ­ take variable A, add it to variable B2, divide by variable J and place the result in variable Z. In declarative languages such as SQL, we simply describe what we want to see as the result, without spelling out the necessary steps. For example, if you write an SQL query like this:
SELECT Employee.FirstName, Car.CarName
FROM Car INNER JOIN
Employee ON Car.CarID = Employee.CarID
WHERE (Car.CarName = ‘GT6’)
you are telling the database engine to find out who was allocated a GT6 ­ pulling the CarName from the Car table and the FirstName from the Employee table.

But you haven’t told it, for example, which table to query first. (Even though the tables are named in the order Car then Employee, this doesn’t carry any information about which is to be queried first). But the order in which the tables are queried can make a huge difference to the performance. Suppose Car has one row and Employee has 20 million. It will be much more efficient to query the car table first. So how is that decision made?

When the SQL string arrives at the database engine it is passed to the ‘query optimiser’ which works out a procedural plan for executing the SQL. The optimiser will look at many pieces of information, including the sizes of the tables, which columns are indexed and so on.

Based on this information the optimiser builds an ‘execution plan’, a procedural description of how it will deliver the requested data. An execution plan is also created in Access but, sadly, it cannot be viewed in this way. As the query becomes more complex so, of course, does the plan.


All Software Applications
Tags: Databases

Like this story? Spread the news by clicking below:

Post this to Delicious del.icio.us    Post this to Digg Digg this    Post this to reddit reddit!

Permalink for this story
R E A D E R   C O M M E N T S
M A R K E T P L A C E
Get your free demo of Numara Track-It! 8 - the leading help desk solution for IT related issues.
Make presentations, review documents & share your entire desktop. 30-day free trial! (cc required).
Discover how remote support can fuel your IT business in ways you've never thought of before.
Apply ITIL best practices at your service desk while eliminating integration cost. Learn more here.
WAN based, automated, daily vulnerability assessments. Click here to try and request our whitepapers.
Have your product or service listed here >   
Sponsored links
F E A T U R E D   J O B S
| Aston Carter
C++ Market Data Developer - Unix - Java - SQL My client are a top financial software house based in the city. They are currently seeking a C++ developer to work in their market data ... more >
| Aston Carter
Junior Python Developer - SQL - Functional Programming - Javascript - Django - Perl - Ruby - MVC My client are a startup software house that specialise in online gaming. The team is small and ... more >
| Aston Carter
Required: Salesforce CRM, Excel My client are Britain's leading financial spread betting firm and are the world's leading trading platform for private investors. This is an excellent opportunity for Salesforce Administrator to join a leading ... more >
| Aston Carter
C# Developer, Hedge Fund, Algo Trading, London The role is working for a boutique derivatives trading company who focus on options high frequency algorithmic trading. They are looking for a bright junior Microsoft .net developer ... more >
More job opportunities