PostgreSQL 批量处理index sql

PostgreSQL 批量处理index sql–查询索引详细信息 SELECT t.tablename, indexname, c.reltuples AS num_rows, pg_size_pretty(pg_relation_size(…

PostgreSQL 批量处理index sql

 

--查询索引详细信息
SELECT
  t.tablename,
  indexname,
  c.reltuples AS num_rows,
  pg_size_pretty(pg_relation_size(quote_ident(t.tablename)::text)) AS table_size,
  pg_size_pretty(pg_relation_size(quote_ident(indexrelname)::text)) AS index_size,
  CASE WHEN indisunique THEN "Y"
    ELSE "N"
  END AS UNIQUE,
  idx_scan AS number_of_scans,
  idx_tup_read AS tuples_read,
  idx_tup_fetch AS tuples_fetched
FROM pg_tables t
  LEFT OUTER JOIN pg_class c ON t.tablename=c.relname
  LEFT OUTER JOIN
    ( SELECT c.relname AS ctablename, ipg.relname AS indexname, x.indnatts AS number_of_columns, idx_scan, idx_tup_read, idx_tup_fetch, indexrelname, indisunique FROM pg_index x
      JOIN pg_class c ON c.oid = x.indrelid
      JOIN pg_class ipg ON ipg.oid = x.indexrelid
      JOIN pg_stat_all_indexes psai ON x.indexrelid = psai.indexrelid )
    AS foo
  ON t.tablename = foo.ctablename
WHERE t.schemaname="public"
ORDER BY 1,2;


--查询整库索引大小并排序
SELECT c.relname,c2.relname, c2.relpages*8/1024 as size_MB, indexdef||";" as index_def
FROM pg_class c, pg_class c2, pg_index i,pg_indexes iv 
WHERE  c.oid = i.indrelid AND c2.oid = i.indexrelid and c2.relname=iv.indexname ORDER BY c2.relpages*8 desc;

--查看索引是valid 还是ready
说明:
The indisvalid and indisready are only meaningful for indexes created using concurrently ---while they are being created or if creation fails. 
 Once they are successfully created those columns have no meaning any more .
 indisvalid indicates whether the index will be used when querying. indisready indicates whether it gets updated on table modifications. 
 You can set them explicitely if you have the correct access rights

SELECT
    trel.relname AS table_name,
    irel.relname AS index_name,
    string_agg(a.attname, ", " ORDER BY c.ordinality) AS columns
FROM pg_index AS i
         JOIN pg_class AS trel ON trel.oid = i.indrelid
         JOIN pg_class AS irel ON irel.oid = i.indexrelid
         JOIN pg_attribute AS a ON trel.oid = a.attrelid
         JOIN LATERAL unnest(i.indkey)
    WITH ORDINALITY AS c(colnum, ordinality)
              ON a.attnum = c.colnum
WHERE i.indisvalid -- WHERE not i.indisvalid
GROUP BY i, trel.relname, irel.relname;


--生成删除主键sql
select "alter table "||t.tablename||" drop CONSTRAINT "||i.indexname||";" from  pg_indexes i ,pg_tables t 
where i.schemaname=t.schemaname and i.tablename=t.tablename  and i.indexname like "%pk%";

--生成索引删除语句
select "drop index  " ||i.indexname||";"  from pg_indexes i ,pg_tables t where i.schemaname=t.schemaname and i.tablename=t.tablename;

--生成索引创建语句
select  indexdef||";" from pg_indexes i ,pg_tables t where i.schemaname=t.schemaname and i.tablename=t.tablename;

--生成添加主键语句
select "alter table " ||t.tablename||"   add primary key using index "||i.indexname||";" from  pg_indexes i ,pg_tables t 
where i.schemaname=t.schemaname and i.tablename=t.tablename  and i.indexname like "%pk%";


--查询gin索引
 select * from pg_indexes where indexdef like "%gin%";

--生成删除外键
SELECT "alter table "|| r.conrelid::regclass ||" drop constraint " ||conname ||";"
FROM pg_catalog.pg_constraint r
WHERE r.contype = "f" ORDER BY 1;


--生成创建外键的脚本
SELECT "alter table " || r.conrelid::regclass  || " add " ||pg_catalog.pg_get_constraintdef(r.oid, true) ||";"
FROM pg_catalog.pg_constraint r
WHERE r.contype = "f" ORDER BY 1;


--生产失效trigger,生效trigger 脚本
select "alter table " ||t3.nspname||"."||t2.relname || " DISABLE TRIGGER " || tgname ||";" from pg_trigger t1,pg_class t2,pg_namespace t3 where t1.tgrelid=t2.oid and t2.relnamespace=t3.oid ;
	
select "alter table " ||t3.nspname||"."||t2.relname || " ENABLE TRIGGER " || tgname ||";" from pg_trigger t1,pg_class t2,pg_namespace t3 where t1.tgrelid=t2.oid and t2.relnamespace=t3.oid ;
	
--查询trigger状态
select t2.relname,tgname,tgenabled from pg_trigger t1,pg_class t2 where t1.tgrelid=t2.oid ;

代码100分

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
转载请注明出处: https://daima100.com/7303.html

(0)
上一篇 2023-03-22
下一篇 2023-03-22

相关推荐

  • SQL抽象语法树及改写场景应用[亲测有效]

    SQL抽象语法树及改写场景应用[亲测有效]我们平时会写各种各样或简单或复杂的sql语句,提交后就会得到我们想要的结果集。比如sql语句,”select * from t_user where user_id > 10;”,意在从表t_user

    2023-06-09
    142
  • Python中的Zip函数

    Python中的Zip函数Python是一种高级编程语言,被广泛应用于各种领域。在Python编程中,经常会遇到一种情况,需要同时遍历两个或多个列表。Zip函数正是为此而生的!Zip函数可以将两个或多个列表中的元素逐个配对后返回一个元组构成的列表。Zip函数既简单又方便,可以大大减少编写代码的时间和复杂度。

    2024-05-16
    85
  • Python实现start-process参数传递

    Python实现start-process参数传递
    start-process是Linux操作系统中一个用于启动新进程的命令,它可以通过命令行传递参数和选项,让新进程在创建后可以使用这些参数和选项来执行特定的操作。在Python中,我们也可以使用subprocess模块来实现start-process的功能,传递参数和选项等信息。Python实现start-process参数传递的过程中,需要注意的一些问题,下面将逐一阐述。

    2024-02-23
    131
  • 基于Python的Numpy生成随机数组方法

    基于Python的Numpy生成随机数组方法随机数组在数据分析、模拟实验、机器学习等领域中经常被使用,而使用Python中的Numpy库可以方便地生成各种形状的随机数组,包括高斯分布、均匀分布等。本文将介绍使用Numpy库生成随机数组的方法,以及如何控制其形状、类型和分布。

    2024-01-07
    112
  • oracle性能优化(项目中的一个sql优化的简单记录)「建议收藏」

    oracle性能优化(项目中的一个sql优化的简单记录)「建议收藏」在项目中,写的sql主要以查询为主,但是数据量一大,就会突出sql性能优化的重要性。其实在数据量2000W以内,可以考虑索引,但超过2000W了,就要考虑分库分表这些了。本文主要记录在实际项目中,一个

    2022-12-23
    156
  • Mybatis实现数据上移、下移、置顶与置底

    Mybatis实现数据上移、下移、置顶与置底介绍 在一些管理系统中,经常有一些需求需要对产生的列表数据进行上移、下移、置顶与置底操作,此时需要一定的SQL功底,下面介绍一下在Mybatis技术下的使用。 具体实现 数据库表 首先设计的表需要有…

    2022-12-17
    154
  • python对csv的操作的简单介绍

    python对csv的操作的简单介绍Python读取CSV文件方法如下:

    2023-11-19
    141
  • Python List的高效应用实例

    Python List的高效应用实例Python中的列表(List)是一种灵活且强大的数据结构,它可以存储任何类型的数据,且支持多种操作和方法。在本文中,我们将通过实例展示Python List的高效应用。

    2024-02-05
    92

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注