Maison > base de données > tutoriel mysql > Comment identifier les colonnes incluses dans les index PostgreSQL ?

Comment identifier les colonnes incluses dans les index PostgreSQL ?

DDD
Libérer: 2024-12-29 19:23:11
original
578 Les gens l'ont consulté

How to Identify Columns Included in PostgreSQL Indexes?

Identification des colonnes indexées dans PostgreSQL

Dans PostgreSQL, l'obtention des colonnes qui participent à un index peut être réalisée à l'aide de tables système. Suivez ces étapes :

SELECT
    t.relname AS table_name,  -- Name of the indexed table
    i.relname AS index_name,  -- Name of the index
    a.attname AS column_name  -- Indexed column name
FROM
    pg_class t  -- Catalog table for tables
JOIN
    pg_class i ON t.oid = i.indrelid  -- Catalog table for indexes
JOIN
    pg_index ix ON i.oid = ix.indexrelid  -- Index information
JOIN
    pg_attribute a ON t.oid = a.attrelid AND a.attnum = ANY(ix.indkey)  -- Join with table attributes
WHERE
    t.relkind = 'r'  -- Restrict to regular tables
    AND t.relname LIKE 'test%';  -- Filter based on pattern (optional)
ORDER BY
    t.relname,  -- Sort by table name
    i.relname;   -- Then sort by index name
Copier après la connexion

Exemple de résultat :

table_name | index_name | column_name
------------+------------+-------------
test       | pk_test    | a
test       | pk_test    | b
test2      | uk_test2   | b
test2      | uk_test2   | c
test3      | uk_test3ab | a
test3      | uk_test3ab | b
test3      | uk_test3b  | b
test3      | uk_test3c  | c
Copier après la connexion

Pour une analyse plus approfondie, vous pouvez regrouper les noms de colonnes et regrouper les résultats :

SELECT
    t.relname AS table_name,  -- Name of the indexed table
    i.relname AS index_name,  -- Name of the index
    array_to_string(array_agg(a.attname), ', ') AS column_names  -- Concatenate column names
FROM
    pg_class t  -- Catalog table for tables
JOIN
    pg_class i ON t.oid = i.indrelid  -- Catalog table for indexes
JOIN
    pg_index ix ON i.oid = ix.indexrelid  -- Index information
JOIN
    pg_attribute a ON t.oid = a.attrelid AND a.attnum = ANY(ix.indkey)  -- Join with table attributes
WHERE
    t.relkind = 'r'  -- Restrict to regular tables
    AND t.relname LIKE 'test%';  -- Filter based on pattern (optional)
GROUP BY
    t.relname,  -- Group by table name
    i.relname  -- Then group by index name
ORDER BY
    t.relname,  -- Sort by table name
    i.relname;   -- Then sort by index name
Copier après la connexion

Exemple de sortie :

table_name | index_name | column_names
------------+------------+--------------
test       | pk_test    | a, b
test2      | uk_test2   | b, c
test3      | uk_test3ab | a, b
test3      | uk_test3b  | b
test3      | uk_test3c  | c
Copier après la connexion

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal