SQL Server et les partitions

1

Vous trouverez ici quelques scripts sql permettant de visualiser et manipuler les partitions sous SQLServer

Les tables systèmes :

 

Lister les partitions d'une base :

select object_name(object_id) as tablename,* from sys.partitions



Lister les partitions de toutes les tables

select T.name, T.Object_id, T.Type_Desc,P.Partition_Id
from sys.tables T
inner join sys.partitions P on T.object_id = P.object_id

 

Affiche la fonction et le schéma de partition de toutes les tables :

SELECT
    tbl.name AS [Name],
    tbl.object_id AS [ID],
    ISNULL( ( select sum (spart.rows) from sys.partitions spart where spart.object_id = tbl.object_id and spart.index_id < 2), 0) AS [RowCount],
    CASE WHEN 'FG'=dsidx.type THEN dsidx.name ELSE N'' END AS [FileGroup],
    CAST(CASE WHEN 'PS'=dsidx.type THEN 1 ELSE 0 END AS bit) AS [IsPartitioned],
    ps.name AS [PartitionScheme],
    pf.name    AS [PartitionFunction]
FROM
    sys.tables AS tbl
    INNER JOIN sys.indexes AS idx ON idx.object_id = tbl.object_id and idx.index_id < 2
    LEFT OUTER JOIN sys.data_spaces AS dsidx ON dsidx.data_space_id = idx.data_space_id
    LEFT OUTER JOIN sys.partition_schemes AS ps ON dsidx.name = ps.name    
    LEFT OUTER JOIN sys.partition_functions pf on ps.function_id=pf.function_id



Lister les caractéristiques des partitions d'une table

select object_name(object_id) as table_name, index_id, partition_number, rows
from sys.partitions  where object_name(object_id) = 'matable'

 

Afficher le détails des fonctions et schémas de partitions

-- Affiche les schémas et fonctions de partitions

select s.name as scheme_name, f.name as function_name, limit = case f.boundary_value_on_right when 0 then 'LEFT' else 'RIGHT' END
from sys.partition_schemes s
inner join sys.partition_functions f on s.function_id=f.function_id

 

-- Liste des paramètres et des valeurs de la fonction pfDate

select    *
from    sys.partition_functions pf
        inner join sys.partition_parameters pp on pf.function_id=pp.function_id
        where pf.name = 'pfDate'

select    *
from    sys.partition_functions pf
        inner join sys.partition_range_values prv on pf.function_id=prv.function_id
        where pf.name = 'pfDate'

 

--relation scheme fonction valeurs

select    s.name as scheme_name, f.name as function_name,
        limit = case f.boundary_value_on_right when 0 then 'LEFT' else 'RIGHT' END ,
        v.boundary_id, v.value
from sys.partition_schemes s
inner join sys.partition_functions f on s.function_id=f.function_id
inner join sys.partition_range_values v on v.function_id=f.function_id