Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the steps followed by sql engine to execute the query..??

My question is not how to use inner join in sql. I know about how it matches between table a and table b.

I'd like to ask how is the internal working of inner working. What algorithm it involves? What happens internally when joining multiple tables?

like image 425
henry Avatar asked May 08 '09 07:05

henry


3 Answers

There are different algorithms, depending on the DB server, indexes and data order (clustered PK), whether calculated values are joined or not etc.

Have a look at a query plan, which most SQL systems can create for a query, it should give you an idea what it does.

like image 161
Lucero Avatar answered Nov 10 '22 14:11

Lucero


In MS Sql, different join algorithms will be used in different situations depending on the tables (their size, what sort of indexes are available, etc). I imagine other DB engines also use a variety of algorithms.

The main types of join used by Ms Sql are:
- Nested loops joins
- Merge joins
- Hash joins

You can read more about them on this page: Msdn -Advanced Query Tuning Concepts

If you get SQL to display the 'execution plan' for your queries you will be able to see what type of join is being used in different situations.

like image 35
codeulike Avatar answered Nov 10 '22 12:11

codeulike


It depends on what database you're using, what you're joining (large/small, in sequence/random, indexed/non-indexed etc).

For example, SQL Server has several different join algorithms; loop joins, merge joins, hash joins. Which one is used is determined by the optimizer when it is working out an execution plan. Sometimes it makes a misjudgement and you can then force a specific join algorithm by using join hints.

You may find the following MSDN pages interesting:
http://msdn.microsoft.com/en-us/library/ms191318.aspx (loop)
http://msdn.microsoft.com/en-us/library/ms189313.aspx (hash)
http://msdn.microsoft.com/en-us/library/ms190967.aspx (merge)
http://msdn.microsoft.com/en-us/library/ms173815.aspx (hints)

like image 2
KristoferA Avatar answered Nov 10 '22 12:11

KristoferA