Index Scan retrieves all the rows from the table.
Index Seek retrieves selective rows from the table.
Index Scan: (Index scan = Table scan)
- Since a scan touches every row in the table whether or not it qualifies, the cost is proportional to the total number of rows in the table.
- Thus, a scan is an efficient strategy if the table is small or if most of the rows qualify for the predicate.
Index Seek:
- Since a seek only touches rows that qualify and pages that contain these qualifying rows, the cost is proportional to the number of qualifying rows and pages rather than to the total number of rows in the table.
Differentiation
between three different types of Index scan vs Table scan
A table scan, where there
exist no clustered indices on the table. Offers the worst performance.
A clustered index scan,
which is confusingly sometimes also referred to as a table scan. Can be faster
than a table scan because it may have to load less information.
An index seek.
Index Scan is
nothing but scanning on the data pages from the first page to the last page. If
there is an index on a table, and if the query is touching a larger amount of
data, which means the query is retrieving more than 50 percent or 90 percent of
the data, and then optimizer would just scan all the data pages to retrieve the
data rows. If there is no index, then you might see a Table Scan (Index Scan)
in the execution plan.
Index seeks are generally preferred for the highly
selective queries. What that means is that the query is just requesting a fewer
number of rows or just retrieving the other 10 (some documents says 15 percent)
of the rows of the table.
Missing Index Script
-- Original Author: Pinal Dave (C) 2011
-- Missing Index Script
--
Original Author: Pinal Dave (C) 2011
SELECT TOP 25
dm_mid.database_id
AS DatabaseID,
dm_migs.avg_user_impact
*(
dm_migs.user_seeks
+
dm_migs.user_scans
)
Avg_Estimated_Impact
,
dm_migs.last_user_seek
AS Last_User_Seek,
OBJECT_NAME
(
dm_mid.
OBJECT_ID
,
dm_mid.database_id
)
AS [TableName],
'CREATE
INDEX [IX_'
+
OBJECT_NAME
(
dm_mid.
OBJECT_ID
,
dm_mid.database_id
) +
'_'
+
REPLACE
(
REPLACE
(
REPLACE
(
ISNULL
(
dm_mid.equality_columns
,
''
),
', '
,
'_'
),
'['
,
''
),
']'
,
''
) +
CASE
WHEN dm_mid.equality_columns IS NOT
NULL AND dm_mid.inequality_columns IS NOT NULL THEN '_'
ELSE ''
END
+
REPLACE
(
REPLACE
(
REPLACE
(
ISNULL
(
dm_mid.inequality_columns
,
''
),
', '
,
'_'
),
'['
,
''
),
']'
,
''
)
+
']'
+
' ON '
+
dm_mid.statement
+
' ('
+
ISNULL
(
dm_mid.equality_columns
,
''
)
+
CASE
WHEN dm_mid.equality_columns
IS NOT NULL AND dm_mid.inequality_columns
IS NOT NULL THEN ',' ELSE
''
END
+
ISNULL
(
dm_mid.inequality_columns
,
''
)
+
')'
+
ISNULL
(
' INCLUDE ('
+
dm_mid.included_columns
+
')'
,
''
)
AS Create_Statement
FROM sys.dm_db_missing_index_groups dm_mig
INNER JOIN sys.dm_db_missing_index_group_stats dm_migs
ON dm_migs.group_handle = dm_mig.index_group_handle
INNER JOIN sys.dm_db_missing_index_details dm_mid
ON dm_mig.index_handle = dm_mid.index_handle
WHERE dm_mid.database_ID = DB_ID()
ORDER BY Avg_Estimated_Impact DESC
GO
No comments:
Post a Comment