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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T23:59:03+00:00 2026-06-08T23:59:03+00:00

If I have created a single index on two columns [lastName] and [firstName] in

  • 0

If I have created a single index on two columns [lastName] and [firstName] in that order. If I then do a query to find the number of the people with first name daniel:

SELECT count(*)
FROM people
WHERE firstName = N'daniel'

will this search in each section of the first index (lastname) and use the secondary index (firstName) to quickly search through each of the blocks of LastName entries?

This seems like an obvious thing to do and I assume that it is what happens but you know what they say about assumptions.

  • 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-08T23:59:06+00:00Added an answer on June 8, 2026 at 11:59 pm

    Yes, this query may – and probably do – use this index (and do an Index Scan) if the query optimizer thinks that it’s better to “quickly search through each of the blocks of LastName entries” as you say than (do an Full Scan) of the table.

    An index on (firstName) would be more efficient though for this particular query so if there is such an index, SQL-Server will use that one (and do an Index Seek).


    Tested in SQL-Server 2008 R2, Express edition:

    CREATE TABLE Test.dbo.people
    ( lastName  NVARCHAR(30) NOT NULL
    , firstName NVARCHAR(30) NOT NULL
    ) ;
    
    INSERT INTO people 
    VALUES
    ('Johnes', 'Alex'),
    ...                   --- about 300 rows
    ('Johnes', 'Bill'),
    ('Brown', 'Bill') ;
    

    Query without any index, Table Scan:

    SELECT count(*)
    FROM people
    WHERE firstName = N'Bill' ;
    

    enter image description here


    Query with index on (lastName, firstName), Index Scan:

    CREATE INDEX last_first_idx
    ON people (lastName, firstName) ;
    
    SELECT ...
    

    enter image description here

    Query with index on (firstName), Index Seek:

    CREATE INDEX first_idx
    ON people (firstName) ;
    
    SELECT ...
    

    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 have created a test website that contains a single ASP.net index.aspx file that
I have created a binded DataGridView that has one single column. I want to
I have created a method that should ideally take a single Account object and
Currently I have created a ABCFactory class that has a single method creating ABC
I am to process single PDFs that have each been created by 'merging' multiple
I am relatively new to Objective-C / iPhone programming, and have only created single
I have a single data context (created once), and I use it to get
I have created some JQuery that will expand a div 'popup' on hover and
I have a value I am trying to index that looks like this: Test
I'm trying to use mongodb with PHP. For that, I have created a MongoHQ

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.