SQL SERVER - COUNT(*) and Index Used - Part 2 - SQL Authority with Pinal Dave
source link: https://blog.sqlauthority.com/2021/06/18/sql-server-count-and-index-used-part-2/?utm_campaign=sql-server-count-and-index-used-part-2
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
SQL SERVER – COUNT(*) and Index Used – Part 2
Yesterday I wrote a blog post on the topic SQL SERVER – COUNT(*) and Index Used, lots of people asked me to follow up question. The most question was around what would happen if there was a heap or only clustered index. Well, let us check that out in today’s blog post.
First, let us create a sample table. I am creating the table based on the Person.Address table of AdventureWorks database.
SELECT
*
INTO
TestTable
FROM
[AdventureWorks].[Person].[Address]
GO
Please note that the table has currently no index at all. Now let us enable the execution plan.
Now run the following script to count the number of rows.
SELECT
COUNT
(*)
FROM
TestTable
GO
Now check the execution plan. In the plan, you will see Table Scan.
Next, we will create clustered index on the table.
CREATE
CLUSTERED
INDEX
[CI]
ON
[dbo].[TestTable]
([AddressID]
ASC
)
GO
Now once again run the command to count the number of rows and check the execution plan.
Here you will see Clustered Index Scan in an execution plan. This is because once you have clustered index, you will not have a table scan. I have often discussed this during Comprehensive Database Performance Health Check.
The next step is to create a nonclustered index and check the execution plan.
CREATE
NONCLUSTERED
INDEX
[NCI]
ON
[dbo].[TestTable]
([StateProvinceID]
ASC
)
GO
In the execution plan, you will notice NonClustered Index Scan because that is created on a narrow column. Please understand that SQL Server Optimization Engine always builds the execution plan which is the most effective and efficient.
A nonclustered index with less number of column is always more efficient in the COUNT(*) operations.
Once you are done with the test experiment you can run the following command to drop the table.
DROP
TABLE
TestTable
GO
Well, that’s it for today. If you liked this video, please do not forget to subscribe to my YouTube Channel – SQL in Sixty Seconds.
Here are my few recent videos and I would like to know what is your feedback about them.
Reference: Pinal Dave (http://blog.SQLAuthority.com)
Recommend
-
6
One of the conversations that keep on coming up during Comprehensive Database Performance Health Check is at what level one should s...
-
9
Recently during Comprehensive Database Performance Health Check, we had an interesting situation where we wanted to Find the Count...
-
10
SQL SERVER – Maximum Column Per Index – 32A very common question, I often receive is how many columns one can have for a single index. Well, the answer is you can have 32 Maximum Column Per Index but that does not mean you should hav...
-
2
SQL SERVER – OS Threads Used by SQL SchedulerToday we will answer the question asked by my client during
-
7
Are Index Scans Bad? The answer No. Index Scans are Not Always Bad. This was in-depth discussed with my client during the Comprehensive Database...
-
4
The best part of my job is working with my clients on the topic of SQL Server Performance Tuning when I am working with them on Comprehensive D...
-
1
SQL SERVER – SUM(1) vs COUNT(*) – Performance ObservationI have received lots of comments and emails about my recent two SQL in Sixty Seconds Videos
-
1
SQL Server – Single Scan for 3 Operations – COUNT(*) COUNT(1) SUM(1)My receive videos on COUNT(*) COUNT(1) SUM(1) have been extremely popular. Today we will take the same conversation to the next level by discussing Single Scan for 3...
-
7
SQL SERVER – Prevent Users from Changing IndexEarlier this week, I wrote a blog post SQL SERVER – Trigger on Databas...
-
7
SQL SERVER – Rebuilding Index with CompressionWe can be Rebuilding Index with Compression even if it was originally not created with compressions. Let us learn how we can do it today. I had recently discussed with my client at
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK