Database Research & Development

  • Home
  • NoSQL
    • NoSQL
    • Cassandra
  • Databases
    • Database Theory
    • Database Designing
    • SQL Server Coding Standards
    • SQL Server
    • PostgreSQL
    • MySQL
    • Greenplum
    • Linux
  • Interviews
    • SQL Server Interviews
    • MySQL Interviews
    • SQL Puzzles
  • DBA Scripts
    • SQL Server DBA Scripts
    • PostgreSQL DBA Scripts
    • MySQL DBA Scripts
    • Greenplum DBA Scripts
  • Home
  • Blog Archives !
  • (: Laugh@dbrnd :)
  • Contact Me !
sqlserverinterviews
Home 2017 July SQL Server Interview: Which Index uses by COUNT(*) statement

SQL Server Interview: Which Index uses by COUNT(*) statement

This article is half-done without your Comment! *** Please share your thoughts via Comment ***

In this post, I am sharing an interesting SQL Server Interview Question which may ask in the interview of SQL Server Database Developer.

As a database developer, we are executing COUNT(*) many times to check the total number of table records. But have you ever check the execution plan of your COUNT(*) statement?

If your answer is No, then you should check below demonstration.
If your answer is Yes, then don’t be surprised by checking below demonstration because COUNT(*) statement first uses a lightweight index of a table. If an index does not present, it will do a Table Scan.

Create a table with sample data:

1
2
3
4
5
CREATE TABLE tbl_Count
(ID1 INT IDENTITY, ID2 SMALLINT DEFAULT 1, Name CHAR(500) DEFAULT 'dbrnd.com')
 
INSERT INTO tbl_Count DEFAULT VALUES;
GO 10000

Check query execution plan of COUNT statement:
It performed Table scan because didn’t find any indexes on a table.

1
SELECT COUNT(1) FROM tbl_Count

Count Query Plan table scan

Now create an index on NAME column:

1
2
CREATE NONCLUSTERED INDEX idx_tbl_Count_Name ON tbl_Count (Name)
GO

Check query execution plan of COUNT statement:
You can find Index scan for idx_tbl_Count_Name.

1
SELECT COUNT(1) FROM tbl_Count

Count Query Plan Index Scan

Now create an index on ID2 column:

1
2
CREATE NONCLUSTERED INDEX idx_tbl_Count_ID2 ON tbl_Count (ID2)
GO

Check query execution plan of COUNT statement:
You can find Index scan for idx_tbl_Count_ID2 (smallint) because it lightweight than idx_tbl_Count_Name (char).

1
SELECT COUNT(1) FROM tbl_Count

Count Query Plan Index Scan

Jul 17, 2017Anvesh Patel
SQL Server: Performance Test - Problem of mismatched Column Data TypeSQL Server 2016: Use OPENJSON() to extract JSON data from Input Parameter

Leave a Reply Cancel reply

CAPTCHA
Refresh

*

Anvesh Patel
Anvesh Patel

Database Engineer

July 17, 2017 SQL Server, SQL Server InterviewAnvesh Patel, COUNT(*), database, database research and development, dbrnd, index, SQL Query, SQL Server, SQL Server Administrator, SQL Server Error, SQL Server Monitoring, SQL Server Performance Tuning, SQL Server Programming, SQL Server Tips and Tricks, TSQL
About Me!

I'm Anvesh Patel, a Database Engineer certified by Oracle and IBM. I'm working as a Database Architect, Database Optimizer, Database Administrator, Database Developer. Providing the best articles and solutions for different problems in the best manner through my blogs is my passion. I have more than six years of experience with various RDBMS products like MSSQL Server, PostgreSQL, MySQL, Greenplum and currently learning and doing research on BIGData and NoSQL technology. -- Hyderabad, India.

About DBRND !

dbrnd

This is a personal blog (www.dbrnd.com).

Any views or opinions represented in this blog are personal and belong solely to the blog owner and do not represent those of people, institutions or organizations that the owner may or may not be associated with in professional or personal capacity, unless explicitly stated.

Feel free to challenge me, disagree with me, or tell me I’m completely nuts in the comments section of each blog entry, but I reserve the right to delete any comment for any reason whatsoever (abusive, profane, rude, or anonymous comments) - so keep it polite.

The content of this website is protected by copyright. No portion of this website may be copied or replicated in any form without the written consent of the website owner.

Recent Comments !
  • Anvesh Patel { Sure will do... } – May 27, 12:43 PM
  • Anvesh Patel { Great... } – May 27, 12:41 PM
  • Anvesh Patel { Great... } – May 27, 12:39 PM
  • Anvesh Patel { Great... } – May 27, 12:36 PM
  • Anvesh Patel { Great... } – May 27, 12:28 PM
  • Anvesh Patel { Great... } – May 27, 12:27 PM
  • Anvesh Patel { Great... } – May 27, 12:16 PM
  • Older »
Follow Me !
  • facebook
  • linkedin
  • twitter
  • youtube
  • google
  • flickr
© 2015 – 2019 All rights reserved. Database Research & Development (dbrnd.com)
Posting....