image: queries
Running two queries against two simple tables
R E L A T E D   C O N T E N T
ADVERTISEMENT

Hands on: Joined in perfect harmony

Cartesian joins can bite, unless you know how to tame them

Mark Whitehorn, Personal Computer World 26 Apr 2007
ADVERTISEMENT

All good database professionals have a set of tools they can apply to various problems they encounter.

Of course, to use any one tool effectively you have to understand how, when and where to apply it.

The ‘Cartesian join’ is a tool that should be in every professional’s toolbox, but is so misunderstood it’s often overlooked.

The word Cartesian means “relating to René Descartes”, the influential French philosopher and mathematician.

A Cartesian product in mathematics is the direct product of two sets. In relational databases, a table is, in essence, a set of data, so a Cartesian join is performed when we join every row in one table to every row in another table.

Rather sadly, you often hear the term used abusively, as in “You complete idiot! That was a Cartesian join! Don’t you know that…” This is mainly because the answer table produced by a Cartesian join can be what we call, in these politically correct times, ‘significantly challenged in the size department’ (or terrifyingly, massively vast).

To see why this happens, imagine two tables – one with four rows and the other with three. An example of a ‘normal’ query we could run is:

SELECT Car, Person

FROM Car INNER JOIN People ON Car.CarID=People.CarID

WHERE Car=”Land Rover”;

This returns one row because only one person was assigned the Land Rover. If we leave out the WHERE clause:

SELECT Car, Person

FROM Car INNER JOIN People ON Car.CarID=People.CarID;

then the query returns three rows, one for each person. However, if we don’t give the database engine any specific instructions about how to join the two tables:

SELECT Car.Car, People.Person

FROM Car, People;

then it assumes we want every single row in the first table joined to every single row in the second table. In other words, it assumes a Cartesian join.

So the number of rows returned is equal to the number of rows in the first table, multiplied by the number of rows in the second – in this case 12. Given these tiny tables, that’s hardly a problem, but tables are often larger in production systems – 100,000 row tables are perfectly common. Performing a Cartesian join on two such tables will generate an answer table of 10 billion rows.


All Software Applications
Tags: Hands On, 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
United Kingdom | VOSA
Management Information Analyst - Up to £30,231 plus benefits - South West This is an excellent opportunity for an experienced Business Analyst or an ambitious Information Analyst to influence a national organisation and contribute to ... more >
Aylesbury, Buckinghamshire, United Kingdom | Grass Roots
Head of Technology -Excellent Salary + Car + Benefits - Buckinghamshire Grass Roots is leading player at providing employee reward and benefits solutions to major blue chip companies.   This part of the business has grown ... more >
Shinfield Park, Reading, United Kingdom | Foster Wheeler
Analyst Programmer - HP Service Center - Competitive Salary - Reading Foster Wheeler is a leading international project management, engineering and construction organisation with global construction capabilities working on major projects within upstream oil & ... more >
Central London, United Kingdom | MI5 Security Service
Enterprise Modeller - Up to £50,000 + benefits - Central London MI5 is making significant investments to enhance its technology capability and is looking for talented IT professionals to join its technology teams in central ... more >
More job opportunities