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 546515
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:52:30+00:00 2026-05-13T10:52:30+00:00

Are query 1) == 2) in terms of estimated query plan AND actual plan?

  • 0

Are query 1) == 2) in terms of estimated query plan AND actual plan? (can statistics affect the actual plan here, ever?)

declare @cat int — input param from prc

…

1)

select * 
from A as a
  join B as b
    on b.id = a.id
    on b.cat = @cat
  join C as c
    on c.fid = b.fid
    on c.cat = @cat
  where a.cat = @cat

2)

select * 
from A as a
  join B as b
    on b.id = a.id
    on b.cat = a.cat
  join C as c
    on c.fid = b.fid
    on c.cat = b.cat
  where a.cat = @cat

It seems to me that these should logically be equivalent and the execution plan should always be the same regardless of actual difference in tables. And adding more conditions either in join, or where, or add more tables to join shouldn’t change this.

Are there cases this is not true?

  • 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-13T10:52:30+00:00Added an answer on May 13, 2026 at 10:52 am

    The optimizer could be affected by whether there is a usable index on any of a.cat, b.cat or c.cat – and whether that index includes the relevant id or fid column too. It might be smart enough to push the simple WHERE clause predicate down to the table level operations. It might also be affected by statistics for the tables. (If the value of the parameter happens to appear just once in C, it may be much more efficient to start processing C than to start processing A; or it may be more efficient to start with B; or it may still be more efficient to start with A. And it may or may not matter that the optimizer doesn’t know the value of the parameter until the statement is executed, rather than being able to see it when it is prepared.)

    So, as already intimated in a comment, there is no definitive statement that can be made without knowing a lot more about the system you are using – including the schemas and so on.

    The good news is that the result contents should be the same – there can be no guarantee about the execution plans.


    I note that most SQL systems would require a single keyword ON for each join:

    select * 
    from A as a
      join B as b
        on b.id = a.id
        AND b.cat = @cat
      join C as c
        on c.fid = b.fid
        AND c.cat = @cat
      where a.cat = @cat
    

    Xerion asked:

    Given that the execution plans may differ, which fashion of query is “better”. Where better is defined as:

    1. More likely to be optimized to give better plan (or less likely to confuse SQL optimizer).
    2. More consistent with SQL convention.

    It depends a bit on the DBMS and its optimizer; there is no substitute for empiricism in this area, but remember that what is empirically determined at one point in time may be erroneous at another point in time because:

    1. The data sets have changed size
    2. The data sets have different statistical distributions now
    3. The optimizer may have changed
    4. There may be different indexes available

    There are many ways to do the joins, and it does (still) depend on the indexes available. I would probably write:

    SELECT * 
      FROM A JOIN B ON b.id  = a.id  AND b.cat = a.cat
             JOIN C ON c.fid = b.fid AND c.cat = b.cat
     WHERE a.cat = @cat
       AND b.cat = @cat
       AND c.cat = @cat
    

    The logic here is that there are likely to be indexes on A(id, cat) and B(id, cat), and on on B(fid, cat) and C(fid, cat), and the optimizer can therefore fully exploit those indexes. The WHERE clause contains two redundant terms, but lets the optimizer know what is required and tells it explicitly what it may not otherwise deduce for itself. If you are confident in the quality of the optimizer (and have checked the query plans it produces), then you might eliminate two of the three conditions in the WHERE clause.

    If you place the parameter in the ON clauses, the optimizer might not make as good use of indexes as it can without that – again, you must experiment to find out how your optimizer behaves.

    Finally, as already intimated, the set of indexes on the tables is crucial, and ensuring that any statistics the DBMS needs are up to date is often important. Also remember that if the tables are initially small, the optimizer may choose a different query plan from when the tables grow big – so do your testing at sensible levels. Unless you production tables will only contain a few tens of rows, don’t do your performance testing for the query on tables that have only a few tens of rows.

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

Sidebar

Related Questions

I have following criteria in the query. The terms list for seen by can
Is there anyway I can guarantee that every document with all query terms always
I'm trying to code If (the query contains any common attack terms, over 80
I am receiving a wildcard query expansion resulted in too many terms error when
http://localhost:8080/search/terms?terms.prefix=ab&terms.fl=text&terms.sort=count I have the above terms query which works as I expect. Returns all
What is the difference (especially in terms of performance) between: Using a *:* query
What is the best way to find out which terms in a query matched
How do I stop thinking every query in terms of cursors, procedures and functions
I am looking to design a query in which I need DISTINCT terms in
Does Lucene (or Solr) still have the 1024 max terms query limit? I could

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.