I've created a script to search for candidate tables for partitioning, and using the index information, I'd like to find the ideal column on which to partition. I'm ignoring (for now) which columns are most often queried. I have a basic query below that prioritizes columns that are part of clustered indexes, and then whether they are a primary key.
So using this admittedly rough approach, which are the best criteria / qualities of an indexed column that I can also include to determine whether they are good partitioning columns? If you can provide reasoning for those attributes as well please.
If you disagree with the approach altogether, that's fine, please comment, but keep in mind that this is for preliminary analysis of partitioning outcome, prior to delving into best practices.
;WITH cte AS
(
SELECT
t.name AS TableName, ind.name AS IndexName,
ind.type_desc AS TypeDesc, col.name AS ColumnName,
st.name AS DataType, ind.is_unique AS IsUnique,
ind.is_primary_key AS IsPrimaryKey,
ind.is_unique_constraint AS IsUniqueCons,
ROW_NUMBER() OVER (PARTITION BY t.name
ORDER BY
CASE WHEN ind.type_desc = 'CLUSTERED' THEN 1 ELSE 2 END,
CASE WHEN ind.is_primary_key = 1 THEN 1 ELSE 2 END) AS row_num
FROM
sys.indexes ind
INNER JOIN
sys.index_columns ic ON ind.object_id = ic.object_id
AND ind.index_id = ic.index_id
INNER JOIN
sys.columns col ON ic.object_id = col.object_id
AND ic.column_id = col.column_id
INNER JOIN
sys.tables t ON ind.object_id = t.object_id
INNER JOIN
sys.types st ON col.system_type_id = st.system_type_id
WHERE
t.is_ms_shipped = 0
AND st.name IN ('datetime', 'smalldatetime')
)
SELECT *
FROM cte
WHERE row_num = 1