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

  • Home
  • SEARCH
  • 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 567375
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:04:34+00:00 2026-05-13T13:04:34+00:00

1)Are SQL query execution times O(n) compared to the number of joins, if indexes

  • 0

1)Are SQL query execution times O(n) compared to the number of joins, if indexes are not used? If not, what kind of relationship are we likely to expect? And can indexing improve the actual big-O time-complexity, or does it only reduce the entire query time by some constant factor?

Slightly vague question, I’m sure it varies a lot but I’m talking in a general sense here.

2) If you have a query like:

SELECT  T1.name, T2.date
FROM    T1, T2
WHERE   T1.id=T2.id
        AND T1.color='red'
        AND T2.type='CAR'

Am I right assuming the DB will do single table filtering first on T1.color and T2.type, before evaluating multi-table conditions? In such a case, making the query more complex could make it faster because less rows are subjected to the join-level tests?

  • 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-13T13:04:34+00:00Added an answer on May 13, 2026 at 1:04 pm

    This depends on the query plan used.

    Even without indexes, modern servers can use HASH JOIN and MERGE JOIN which are faster than O(N * M)

    More specifically, complexity of a HASH JOIN is O(N + M), where N is the hashed table and M the is lookup table. Hashing and hash lookups have constant complexity.

    Complexity of a MERGE JOIN is O(N*Log(N) + M*Log(M)): it’s the sum of times to sort both tables plus time to scan them.

    SELECT  T1.name, T2.date
    FROM    T1, T2
    WHERE   T1.id=T2.id
            AND T1.color='red'
            AND T2.type='CAR'
    

    If there are no indexes defined, the engine will select either a HASH JOIN or a MERGE JOIN.

    The HASH JOIN works as follows:

    1. The hashed table is chosen (usually it’s the table with fewer records). Say it’s t1

    2. All records from t1 are scanned. If the records holds color='red', this record goes into the hash table with id as a key and name as a value.

    3. All records from t2 are scanned. If the record holds type='CAR', its id is searched in the hash table and the values of name from all hash hits are returned along with the current value of data.

    The MERGE JOIN works as follows:

    1. The copy of t1 (id, name) is created, sorted on id

    2. The copy of t2 (id, data) is created, sorted on id

    3. The pointers are set to the minimal values in both tables:

      >1  2<
       2  3
       2  4
       3  5
      
    4. The pointers are compared in a loop, and if they match, the records are returned. If they don’t match, the pointer with the minimal value is advanced:

      >1  2<  - no match, left pointer is less. Advance left pointer
       2  3
       2  4
       3  5
      
       1  2<  - match, return records and advance both pointers
      >2  3
       2  4
       3  5
      
       1  2  - match, return records and advance both pointers
       2  3< 
       2  4
      >3  5
      
       1  2 - the left pointer is out of range, the query is over.
       2  3
       2  4<
       3  5
      >
      

    In such a case, making the query more complex could make it faster because less rows are subjected to the join-level tests?

    Sure.

    Your query without the WHERE clause:

    SELECT  T1.name, T2.date
    FROM    T1, T2
    

    is more simple but returns more results and runs longer.

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

Sidebar

Related Questions

I'm noticing exponential performance degradation in MySQL query execution times when queries include calls
Based on getting Query Execution Statistics using this extremely useful piece of SQL obtained
In LINQ, you can write a manual SQL query, take the results from it
I have a SQL query and it fails at times. That means that query
i am executing this sql query to update the database $sql_select = UPDATE `database1`.`media`
I am executing the following raw sql query in one of my controllers :
In Microsoft SQL Server 2008, when executing .query('{xpath}/text()') on an XML stream column value,
in DB2 while executing a query i got SqlException: DB2 SQL Error: SQLCODE=-668, SQLSTATE=57016
I have this query which on executing in my sql command line client executes
I am successfully executing the following query in SQL Server 2008 built into VS2008:

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.