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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T03:03:54+00:00 2026-06-02T03:03:54+00:00

IMHO SQL Server can choose itself (unless being told) what is the best index

  • 0

IMHO SQL Server can choose itself (unless being told) what is the best index to use for the query.

Ok

What about something like this (pseudo code):

select __a from tbl where __a not in
 (
   select __b  from tbl 

 ) 

(let’s say we have index_1 which is for (__a) and index_2 which is for (__b)

Will SQL Server still use one index at execution or multiple indexes together…?

  • 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-02T03:03:56+00:00Added an answer on June 2, 2026 at 3:03 am

    First, create your tables:

    USE tempdb;
    GO
    CREATE TABLE dbo.tbl(__a INT, __b INT);
    

    Then create two indexes:

    CREATE INDEX a_index ON dbo.tbl(__a);
    CREATE INDEX b_index ON dbo.tbl(__b);
    

    Now populate with some data:

    INSERT dbo.tbl(__a, __b)
      SELECT [object_id], column_id
        FROM sys.all_columns;
    

    Now run your query and turn actual execution plan on. You will see something like this, showing that yes, both indexes are used (in fact the index on __b is used both for data retrieval in the subquery and as a seek to eliminate rows):

    enter image description here

    A more efficient way to write your query would be:

    select __a from dbo.tbl AS t where not exists
     (
       select 1 from dbo.tbl AS t2 
         where t2.__b = t.__a
     );
    

    Now here’s your whole plan (again, both indexes are used, but notice how there are much fewer operations):

    enter image description here

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

Sidebar

Related Questions

I'm making a stateless web service which uses Microsoft SQL Server to read data
The best way to avoid SQL injection for defined value type such as numbers,
IMHO, there are two techiques to handle a query for a resource: For http
I have a pretty abnormal(IMHO) error that it's being raised whenever I try do
HI all! My client currently has a SQL Server database that performs 3-4 Million
Does exists a java library that can create sql statements? I'm not in search
A customers ERP solution has (imho) a really ugly database structure. It doesn't use
IMHO, I heard about this few times lately. On some web portals I saw
I've got some Java code with SQL statements written as Java strings (please no
IMHO the following is the typical 3-column-layout using CSS. Without the surrounding #main-div it

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.