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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:18:49+00:00 2026-06-17T18:18:49+00:00

I have a table with primary key, and two foreign keys, where both allows

  • 0

I have a table with primary key, and two foreign keys, where both allows NULLS.

When I create indexes separately for each two columns, query running about 2-3 seconds and return around 300000 rows.

When I create composite non-clustered index for that two columns, query running around 10 minutes for same number of rows.

It’s important to notice that two columns appear in WHERE condition and works with OR clause, like this:

Select 
    SomeColumn
From
    SomeTable
Where 
    FirstColumn = x OR SecondColumn = x

Platform on which query was executed is SQL 2008 R2.

Why there is such a difference in execution time in that two cases?

  • 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-06-17T18:18:50+00:00Added an answer on June 17, 2026 at 6:18 pm

    If you have two separate indexes, and only those two columns are in the WHERE clause then the optimizer can use them both efficiently to determine the required rows (presumably an INDEX SEEK).

    A composite index is not that good as composite indexes are first sorted on the first index column, then the second, and so on. So it’s just useful for the first column in your OR condition, like you got. To determine the rows for the second column the optimizer would have to scan the complete table as it doesn’t have suitable index.

    If you got a large table with this scenario, then the single indexes will be usually faster for simple queries like you asked. It actually also depends on the selected columns, query complexity, covering indexes and so on.

    I had this problem myself. See: Query performance of combined index vs. multiple single indexes vs. fulltext index for reference.

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

Sidebar

Related Questions

If two tables have foreign keys to the same primary key of another table,
I have two SQL Server tables with Primary Keys (PK) and a Foreign Key
I have two tables with primary foreign key relationship. The table in foreign key
i have two tables both are related with primary-foreign key ralation and i have
I have two SQL statements: CREATE TABLE legs(legid INT PRIMARY KEY AUTO_INCREMENT NOT NULL,
I have an h2 database with two tables with foreign keys like CREATE TABLE
i have a table with two foreign keys as composite key. ActivityTbl - (activityNbr(PK),
I have a two-column primary key on a table. I have attempted to alter
I have a table called user_relationship. which has two foreign keys refering back to
I have a table that has two foreign keys to two different tables with

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.