środa, 19 października 2011

Pobranie struktury indeksów przy pomocy SQL

Ingres:
SELECT iii.*, iic.* FROM iiindexes iii JOIN iiindex_columns iic ON (iii.index_name = iic.index_name)
    ORDER BY iii.index_name
Informix:
SELECT st.tabname, sc.colname, sc.colno, si.idxtype, si.*, st.*, sc.*
    FROM sysindexes si JOIN systables st ON (si.tabid = st.tabid) JOIN syscolumns sc ON (sc.tabid = st.tabid)
Oracle:
SELECT * FROM user_ind_columns
PostgreSQL:
SELECT i.relname, a.attname, t.*, i.*, ix.*, a.* FROM pg_class t, pg_class i, pg_index ix, pg_attribute a
    WHERE t.oid = ix.indrelid AND i.oid = ix.indexrelid AND a.attrelid = t.oid AND a.attnum = ANY(ix.indkey)
Usunięcie wszystkich indeksów z danej relacji na PostgreSQL:
CREATE OR REPLACE FUNCTION drop_all_indexes_on_table (table_name VARCHAR) RETURNS void
AS $$
DECLARE
 c_indexes FOR SELECT
     DISTINCT i.relname AS index_name
    FROM
     pg_class t, pg_class i, pg_index ix, pg_attribute a
    WHERE
     t.oid = ix.indrelid
     AND i.oid = ix.indexrelid
     AND a.attrelid = t.oid
     AND a.attnum = ANY(ix.indkey)
     AND t.relkind = 'r'
     AND ix.indisprimary != 't'
     AND t.relname = 'table_name'
BEGIN
 FOR index_entry IN c_indexes LOOP
  DROP INDEX index_entry;
 END LOOP;
END
$$ LANGUAGE PLPGSQL;

Brak komentarzy:

Prześlij komentarz