Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6583647
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:26:10+00:00 2026-05-25T16:26:10+00:00

What is more efficient to use in SQL Server 2005: PIVOT or MULTIPLE JOIN?

  • 0

What is more efficient to use in SQL Server 2005: PIVOT or MULTIPLE JOIN?

For example, I got this query using two joins:

SELECT p.name, pc1.code as code1, pc2.code as code2
FROM product p
    INNER JOIN product_code pc1
    ON p.product_id=pc1.product_id AND pc1.type=1
    INNER JOIN product_code pc2
    ON p.product_id=pc2.product_id AND pc2.type=2

I can do the same using PIVOT:

SELECT name, [1] as code1, [2] as code2
FROM (
    SELECT p.name, pc.type, pc.code
    FROM product p
        INNER JOIN product_code pc
        ON p.product_id=pc.product_id
    WHERE pc.type IN (1,2)) prods1
PIVOT(
    MAX(code) FOR type IN ([1], [2])) prods2

Which one will be more efficient?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T16:26:11+00:00Added an answer on May 25, 2026 at 4:26 pm

    The answer will of course be “it depends” but based on testing this end…

    Assuming

    1. 1 million products
    2. product has a clustered index on product_id
    3. Most (if not all) products have corresponding information in the product_code table
    4. Ideal indexes present on product_code for both queries.

    The PIVOT version ideally needs an index product_code(product_id, type) INCLUDE (code) whereas the JOIN version ideally needs an index product_code(type,product_id) INCLUDE (code)

    If these are in place giving the plans below

    Plans

    then the JOIN version is more efficient.

    In the case that type 1 and type 2 are the only types in the table then the PIVOT version slightly has the edge in terms of number of reads as it doesn’t have to seek into product_code twice but that is more than outweighed by the additional overhead of the stream aggregate operator

    PIVOT

    Table 'product_code'. Scan count 1, logical reads 10467
    Table 'product'. Scan count 1, logical reads 4750
       CPU time = 3297 ms,  elapsed time = 3260 ms.
    

    JOIN

    Table 'product_code'. Scan count 2, logical reads 10471
    Table 'product'. Scan count 1, logical reads 4750
       CPU time = 1906 ms,  elapsed time = 1866 ms.
    

    If there are additional type records other than 1 and 2 the JOIN version will increase its advantage as it just does merge joins on the relevant sections of the type,product_id index whereas the PIVOT plan uses product_id, type and so would have to scan over the additional type rows that are intermingled with the 1 and 2 rows.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it more efficient to use PHP's include_once or require_once instead of using a
I have got hold of a sql server 2008 developer edition and this is
Consider the following SQL Server 2005/2008 query: Select [UID], [DESC] From SomeTable Order By
In SQL Server, you can apply the use function to point a query to
Two quick (I hope!) questions: Counting rows Is it more efficient to use COUNT(id)
Is there a more efficient way to clamp real numbers than using if statements
Using SQL Server and T-SQL, how can I update ALL tables tables with a
I have a query which involves 2 tables 'Coupons' and 'CouponUsedLog' in SQL Server,
Is it more efficient to use a varchar field sized as a power of
Is using the .NET DataAdapter's batch insert features any more efficient, as far as

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.