External fragmentation (all databases)


Job details

Name: External fragmentation (all databases)
Platform: Sqlserver
Category: Maintenance
Premium package: Maintenance package
Description: External (logical) fragmentation occurs when an index leaf page is not in logical order. It occurs when the logical ordering of the index does not match the physical ordering of the index. This causes SQL Server to perform extra work to return ordered results.
Long description: Collects external fragmentation statistics for tables and indexes. The information is extracted from the dynamic management function (view) sys.dm_db_index_physical_stats.
Version: 1.6
Default schedule: 20 6 6 *
Requires engine install: Yes
Compatibility tag: .[type=‘instance’ & databasetype=‘sqlserver’]/instance[maj_version > ‘2000′]/.[hasengine=‘YES’ & eng_inst_priv = 0 & (engine_edition = ‘Microsoft SQL Server’ engine_edition = ‘Azure SQL Managed Instance’)]

Parameters

Name Default value Description
history threshold 365 The number of days the statistics (size, rows count, fragmentation, etc.) will be kept in the history table.
max objects 10 The number of objects (indexes or tables) for which statistics will be inserted into the history table.
size threshold 1 The minimum size (in MB) of the objects (tables, indexes, etc.) to be analyzed for external fragmentation.
collect record count stat NO If “YES“, the performance view sys.dm_db_index_physical_stats is queried in DETAILED mode (to collect information from the record_count column) for the objects with high values of external fragmentation.
ignore databases master,model List of databases (separated by comma) which will be ignored. You can use % (percent sign) to represent wild card characters.
max elapsed time 60 The maximum time (in minutes) the procedure is allowed to run.
max size threshold 5000 The maximum size (in MB) of the objects (tables, indexes, etc.) to be analyzed for fragmentation.
continue on next run YES If set to “YES“ (default) the procedure will continue where it left off.

Job Summary

SELECT object_name(object_id) AS ObjectName, index_id, avg_fragmentation_in_percent FROM sys.dm_db_index_physical_stats(DB_ID(), NULL, NULL, NULL, NULL) WHERE avg_fragmentation_in_percent > 10;

Dependencies

This job has several dependencies:

Implementation Details

This section can be complex due to the involvement of dynamic SQL and procedures to create necessary tables:

CREATE TABLE all_db_ext_fragm_obj (database_name varchar(200), object_name varchar(150), index_name varchar(150), type varchar(150), index_depth bigint, avg_fragmentation_in_percent bigint, fragment_count bigint, page_count bigint, record_count bigint, histr_date datetime, last_access datetime)
CREATE TABLE all_db_ext_fragm_exec_histr (db_count bigint, min_size_threshold int, max_size_threshold int, objects_checked bigint, objects_checked_size_mb bigint, heap_count bigint, index_count bigint, start_date datetime, end_date datetime, elap_sec bigint, last_db_name varchar(512), db_checked bigint)
The main procedure, all_db_ext_fragm_obj_collector, is involved in collecting data based on certain thresholds and parameters, querying index properties and usage statistics, then logging this data for later analysis.

Monitoring and Reporting

Overall, this job is crucial for optimizing SQL Server performance by ensuring that indexes and tables are maintained with minimal fragmentation, thereby supporting efficient data retrieval and management.