searchusermenu
  • 发布文章
  • 消息中心
点赞
收藏
评论
分享
原创

如何查询 SQL Server 中某个模式下某张分区表关联的分区函数和分区方案

2024-11-08 09:21:29
1
0

1. 引言

    本文将介绍如何查询Sales模式下Orders分区表关联的分区函数和分区方案

2. 查询分区表的关联信息

step one:查询分区方案

SELECT 
    t.name AS Table_Name,
    s.name AS Schema_Name,
    i.name AS Index_Name,
    ps.name AS Partition_Scheme_Name
FROM 
    sys.tables t
JOIN 
    sys.schemas s ON t.schema_id = s.schema_id
JOIN 
    sys.indexes i ON t.object_id = i.object_id
JOIN 
    sys.partition_schemes ps ON i.data_space_id = ps.data_space_id
WHERE 
    s.name = 'Sales' 
    AND t.name = 'Orders';

step two:查询分区函数

SELECT 
    ps.name AS Partition_Scheme_Name,
    pf.name AS Partition_Function_Name
FROM 
    sys.partition_schemes ps
JOIN 
    sys.partition_functions pf ON ps.function_id = pf.function_id
WHERE 
    ps.name IN (
        SELECT 
            ps.name
        FROM 
            sys.tables t
        JOIN 
            sys.schemas s ON t.schema_id = s.schema_id
        JOIN 
            sys.indexes i ON t.object_id = i.object_id
        JOIN 
            sys.partition_schemes ps ON i.data_space_id = ps.data_space_id
        WHERE 
            s.name = 'Sales' 
            AND t.name = 'Orders'
    );

step three:查询分区函数的边界值

SELECT 
    pf.name AS Partition_Function_Name,
    pf.boundary_value_on_right,
    pf.fanout,
    prv.value AS Boundary_Value,
    prv.position
FROM 
    sys.partition_functions pf
LEFT JOIN 
    sys.partition_range_values prv ON pf.function_id = prv.function_id
WHERE 
    pf.name IN (
        SELECT 
            pf.name
        FROM 
            sys.partition_schemes ps
        JOIN 
            sys.partition_functions pf ON ps.function_id = pf.function_id
        WHERE 
            ps.name IN (
                SELECT 
                    ps.name
                FROM 
                    sys.tables t
                JOIN 
                    sys.schemas s ON t.schema_id = s.schema_id
                JOIN 
                    sys.indexes i ON t.object_id = i.object_id
                JOIN 
                    sys.partition_schemes ps ON i.data_space_id = ps.data_space_id
                WHERE 
                    s.name = 'Sales' 
                    AND t.name = 'Orders'
            )
    )
ORDER BY 
    pf.name, prv.position;
0条评论
0 / 1000
c****9
2文章数
0粉丝数
c****9
2 文章 | 0 粉丝
c****9
2文章数
0粉丝数
c****9
2 文章 | 0 粉丝
原创

如何查询 SQL Server 中某个模式下某张分区表关联的分区函数和分区方案

2024-11-08 09:21:29
1
0

1. 引言

    本文将介绍如何查询Sales模式下Orders分区表关联的分区函数和分区方案

2. 查询分区表的关联信息

step one:查询分区方案

SELECT 
    t.name AS Table_Name,
    s.name AS Schema_Name,
    i.name AS Index_Name,
    ps.name AS Partition_Scheme_Name
FROM 
    sys.tables t
JOIN 
    sys.schemas s ON t.schema_id = s.schema_id
JOIN 
    sys.indexes i ON t.object_id = i.object_id
JOIN 
    sys.partition_schemes ps ON i.data_space_id = ps.data_space_id
WHERE 
    s.name = 'Sales' 
    AND t.name = 'Orders';

step two:查询分区函数

SELECT 
    ps.name AS Partition_Scheme_Name,
    pf.name AS Partition_Function_Name
FROM 
    sys.partition_schemes ps
JOIN 
    sys.partition_functions pf ON ps.function_id = pf.function_id
WHERE 
    ps.name IN (
        SELECT 
            ps.name
        FROM 
            sys.tables t
        JOIN 
            sys.schemas s ON t.schema_id = s.schema_id
        JOIN 
            sys.indexes i ON t.object_id = i.object_id
        JOIN 
            sys.partition_schemes ps ON i.data_space_id = ps.data_space_id
        WHERE 
            s.name = 'Sales' 
            AND t.name = 'Orders'
    );

step three:查询分区函数的边界值

SELECT 
    pf.name AS Partition_Function_Name,
    pf.boundary_value_on_right,
    pf.fanout,
    prv.value AS Boundary_Value,
    prv.position
FROM 
    sys.partition_functions pf
LEFT JOIN 
    sys.partition_range_values prv ON pf.function_id = prv.function_id
WHERE 
    pf.name IN (
        SELECT 
            pf.name
        FROM 
            sys.partition_schemes ps
        JOIN 
            sys.partition_functions pf ON ps.function_id = pf.function_id
        WHERE 
            ps.name IN (
                SELECT 
                    ps.name
                FROM 
                    sys.tables t
                JOIN 
                    sys.schemas s ON t.schema_id = s.schema_id
                JOIN 
                    sys.indexes i ON t.object_id = i.object_id
                JOIN 
                    sys.partition_schemes ps ON i.data_space_id = ps.data_space_id
                WHERE 
                    s.name = 'Sales' 
                    AND t.name = 'Orders'
            )
    )
ORDER BY 
    pf.name, prv.position;
文章来自个人专栏
sqlserver数据库中分区表的创建
2 文章 | 1 订阅
0条评论
0 / 1000
请输入你的评论
0
0