SlideShare a Scribd company logo
1
SELECT COUNT (*)
FROM mytable
一定會使用Table Scan?
2
3
4
5
CREATETABLE CTest
(c1 INT IDENTITY
, c2 BIGINT DEFAULT 1
, c3 CHAR (1000) DEFAULT 'a'
);
GO
SET NOCOUNT ON;
GO
INSERT INTO CTest DEFAULTVALUES; 6
 Enable execution plan viewers in SSMS
Include Actual Execution Plan
7
 SELECT COUNT (*) from CTest;
8
CREATE NONCLUSTERED INDEX
CTest_1
ON CTest (c2);
9
 SELECT COUNT (*) from CTest;
 變成了Index Scan
 因為使用 CTest_1 index 會比使用Table Scan 使用更少的 i/o
10
CREATE NONCLUSTERED INDEX CTest_2
ON CTest (c1);
11
 SELECT COUNT (*) from CTest;
 跟 CTest_1 Index 相比 CTest_2 Index 使用更少的 i/o
12
SELECT a.[index_id], a.[page_count], b.name AS index_name
FROM sys.dm_db_index_physical_stats(
DB_ID (),
OBJECT_ID ('CTest'),
NULL,
NULL,
'LIMITED') a
INNER JOIN sys.indexes b
ON a.object_id = b.object_id AND a.index_id = b.index_id;
13
14
Count(*)
or
Count(1) 15
16
GUID
是一個好的
Cluster Key?
17
18
Random
Index
fragmentation
16 bytes long 19
20
create table tblSAMPLE_1
(xnewid uniqueidentifier
default(newid()) primary key,
xname varchar(10)
);
go
insert into tblSAMPLE_1(xname) values('rainmaker')
go 500
21
22
23
SELECT a.index_id, name,
avg_fragmentation_in_percent
FROM sys.dm_db_index_physical_stats
(DB_ID(N'tempdb'), OBJECT_ID(N'tblSAMPLE_1'), NULL,
NULL, NULL) AS a
JOIN sys.indexesAS b ON a.object_id = b.object_id
AND a.index_id = b.index_id;
24

More Related Content

PDF
Weather scraper for your data warehouse
PDF
[TechPlayConf]Rekognition導入事例
PPTX
Deleting a Node from a Binary Search Tree (Scheme
DOCX
Algorithm for bisection method
PPTX
Angular2 Form
PPTX
Sql效能調校分享-資料瘦身
PPTX
從Developer來看 效能調校
Weather scraper for your data warehouse
[TechPlayConf]Rekognition導入事例
Deleting a Node from a Binary Search Tree (Scheme
Algorithm for bisection method
Angular2 Form
Sql效能調校分享-資料瘦身
從Developer來看 效能調校

More from Rainmaker Ho (6)

PPTX
常見 Web 系統攻擊及防範
PPTX
Progressive Web Apps - Tips
PPTX
Angular2 DI
PPTX
Internet of Things
PPTX
Asp.net開發要注意的是?
PPTX
軟體弱點掃描
常見 Web 系統攻擊及防範
Progressive Web Apps - Tips
Angular2 DI
Internet of Things
Asp.net開發要注意的是?
軟體弱點掃描
Ad

Recently uploaded (20)

PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PPTX
assetexplorer- product-overview - presentation
PDF
Digital Systems & Binary Numbers (comprehensive )
PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
PPTX
history of c programming in notes for students .pptx
PDF
Digital Strategies for Manufacturing Companies
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PPTX
Operating system designcfffgfgggggggvggggggggg
PPTX
Embracing Complexity in Serverless! GOTO Serverless Bengaluru
PDF
PTS Company Brochure 2025 (1).pdf.......
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PDF
Understanding Forklifts - TECH EHS Solution
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PDF
Nekopoi APK 2025 free lastest update
PPTX
CHAPTER 2 - PM Management and IT Context
PDF
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
Which alternative to Crystal Reports is best for small or large businesses.pdf
assetexplorer- product-overview - presentation
Digital Systems & Binary Numbers (comprehensive )
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
history of c programming in notes for students .pptx
Digital Strategies for Manufacturing Companies
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
Operating system designcfffgfgggggggvggggggggg
Embracing Complexity in Serverless! GOTO Serverless Bengaluru
PTS Company Brochure 2025 (1).pdf.......
2025 Textile ERP Trends: SAP, Odoo & Oracle
Adobe Illustrator 28.6 Crack My Vision of Vector Design
VVF-Customer-Presentation2025-Ver1.9.pptx
Navsoft: AI-Powered Business Solutions & Custom Software Development
Wondershare Filmora 15 Crack With Activation Key [2025
Understanding Forklifts - TECH EHS Solution
Internet Downloader Manager (IDM) Crack 6.42 Build 41
Nekopoi APK 2025 free lastest update
CHAPTER 2 - PM Management and IT Context
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
Ad

SQL Count(*) VS Count(1)

Editor's Notes

  • #3: http://www.sqlskills.com/blogs/paul/which-index-will-sql-server-use-to-count-all-rows/