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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T07:27:42+00:00 2026-05-15T07:27:42+00:00

In my application I have two queries which will be quite frequently used. The

  • 0

In my application I have two queries which will be quite frequently used. The Where clauses of these queries are the following:

WHERE FieldA = @P1 AND (FieldB = @P2 OR FieldC = @P2)

and

WHERE FieldA = @P1 AND FieldB = @P2

P1 and P2 are parameters entered in the UI or coming from external datasources.

  • FieldA is an int and highly non-unique, means: only two, three, four different values in a table with say 20000 rows
  • FieldB is a varchar(20) and is “almost” unique, there will be only very few rows where FieldB might have the same value
  • FieldC is a varchar(15) and also highly distinct, but not as much as FieldB
  • FieldA and FieldB together are unique (but do not form my primary key, which is a simple auto-incrementing identity column with a clustered index)

I’m wondering now what’s the best way to define an index to speed up specifically these two queries. Shall I define one index with…

FieldB (or better FieldC here?)
FieldC (or better FieldB here?)
FieldA

… or better two indices:

FieldB
FieldA

and

FieldC
FieldA

Or are there even other and better options? What’s the best way and why?

Thank you for suggestions in advance!

Edit:

Just as an info to other readers: Here was another answer which has been deleted now. Actually the answer seemed very useful to me. The recommendation was to create two indices (according to my second option above) and to reformulate the first query by using a UNION of two select statements (one with WHERE FieldA = @P1 AND FieldB = @P2 and one with WHERE FieldA = @P1 AND FieldC = @P2) instead of OR to benefit from both indices (which wouldn’t be the case with the OR-operator).

Edit2:

The statement that with OR the indexes are not used and that a UNION is preferable seems to be wrong – at least according to my own tests (see my own answer below).

  • 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-15T07:27:43+00:00Added an answer on May 15, 2026 at 7:27 am

    Extending Remus’ (edit: now deleted) answer…

    • if @p2 is varchar(15) then you can’t compare against FieldB reliably, it’s varchar(20)
    • if @p2 is varchar(20) then FieldC will be converted to varchar(20) and not use an index (or at best scan it)
    • if @p1 only has 2, 3, 4 values then why not tinyint and reduce table/index size?

    I wouldn’t bother with indexes until you resolve this datatype precedence issue: this is on top of the OR clause issue.

    Finally, a column is unique or non-unique: there is no in between. Statistics help here with selectivity, but it’s irrelevant.

    I would reverse the indexes from Remus’ answer to be FieldB, FieldA (and unique) and FieldC, FieldA because of FieldA’s selectivity

    Edit, after comments: you can’t compare the use of @p2 against the use of constant strings.

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

Sidebar

Related Questions

In Application i have two buttons in which one button is used as a
I am developing a application to create chart. I have two types of Queries.
In the asp.net mvc 3 application I have two views which have the same
In my application, I have two queries that occur from time to time (from
I have to build an Automated Chat Application which will have it's front end
I have application where i have two view controllers my first view and second
I have two XML files that are generated by another application I have no
I have application in VB.net that have two different form (Form1 and Form2). Now
In my application i have two drop down boxes first box had origin second
I have two application that need to talk to each other. App1 needs to

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.