Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

Chapter 16. High Performance SQL Tuning > Use Table Joins Rather than Multiple ...

Use Table Joins Rather than Multiple Queries

It is generally more efficient to perform table joins rather than using multiple queries when retrieving data from multiple related tables. In the following example, two queries are used to get the product name and the product type name for product #1 (using two queries is wasteful). The first query gets the name and product_type_id column values from the products table for product #1. The second query then uses that product_type_id to get the name column from the product_types table.

-- BAD (two separate queries when one would work)
SELECT name, product_type_id
FROM products
WHERE product_id = 1;

NAME                            PRODUCT_TYPE_ID
------------------------------ ---------------
Modern Science                                1

SELECT name
FROM product_types
WHERE product_type_id = 1;

NAME
----------
Book


  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial