Simple clear advice in plain English

Hands on: Understanding SQL queries

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

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.

Article tags

Reader Comments

dot net problem

Unfortunatly if you have updated beyond .net version 2 you can't use SQL 2005

Posted by Bob Veitch, 06 Jan 2009

   

Add your comment

All fields must be completed. Your email address will not be displayed or used to send marketing messages.

All messages will be checked by moderators before appearing on the site.

See our Privacy Policy for more information.

Related articles

Keep your PC up to date illustration

How healthy is your PC?

Do you think your PC's performance is lacking a certain something? We show you how to get your computer working to the best of its ability

Microsoft Word list screenshot

Improve the appearance of lists within lists in Word 2003

Whether you are writing a recipe or a complicated reference book, it is possible to create manageable lists and sub-sections to improve their appearance

Budget templates

Where can I find budget templates?

Microsoft Office's webs page offers various templates to organise your finances

Question & Answer

Q.How do I stop Windows 7 search?

> Read the answer

Q.Is it a genuine call from Microsoft?

> Read the answer

Q.How can I turn Autoplay back on?

> Read the answer

Best deals on the web

img

THREE E585 Mi-Fi Take it Away Mobile Broadband - 5GB allowance

£44.97- Buy it now

img

THREE Huawei E353u Take It Away Mobile Broadband - One Month Rolling Contract

£4.99- Buy it now

img

T-MOBILE 3G Pay As You Go iPad Micro SIM

£0.10- Buy it now

Great benefits for subscribers!

Poll

Which is your preferred web browser

Jargon Buster

Computing terms explained in plain English

Restore point

A Windows backup of system files and settings.

Great shopping deals from Computeractive