集锦系列:Oracle日常工作SQL集锦 (持续更新)

场景

日常工作中,经常会使用Oracle,在这个过程中,需要查询某些数据,而有些SQL比较特殊,或者比较常用。所以本篇博文主要是记录自己工作中会使用的Oracle脚本。

正文

表空间相关

  1. 查看表空间的使用状况

    SELECT
    UPPER(f.tablespace_name) 表空间名, d.Tot_grootte_Mb "表空间大小(M)", d.Tot_grootte_Mb - f.total_bytes "已使用空间(M)", round((d.Tot_grootte_Mb - f.total_bytes) / d.Tot_grootte_Mb * 100,2) "使用比", f.total_bytes "空闲空间(M)", f.max_bytes "最大块(M)" 
    FROM
    	(	SELECT
    			tablespace_name, round(SUM(bytes)/(1024*1024),2) total_bytes, round(MAX(bytes)/(1024*1024),2) max_bytes 
    		FROM
    			sys.dba_free_space 
    		GROUP BY
    			tablespace_name) f, (	SELECT
    										dd.tablespace_name, round(SUM(dd.bytes)/(1024*1024),2) Tot_grootte_Mb 
    									FROM
    										sys.dba_data_files dd 
    									GROUP BY
    										dd.tablespace_name) d 
    WHERE
    	d.tablespace_name = f.tablespace_name 
    ORDER BY
    	4 DESC 
    
  2. 查看无法扩展的段

    SELECT   segment_name,  
                    segment_type,  
                    owner,  
                    a.tablespace_name   "tablespacename",  
                    initial_extent/1024   "inital_extent(K)",  
                    next_extent/1024   "next_extent(K)",  
                    pct_increase,  
                    b.bytes/1024   "tablespace   max   free   space(K)",  
                    b.sum_bytes/1024   "tablespace   total   free   space(K)"  
          FROM   dba_segments   a,  
                    (SELECT   tablespace_name,MAX(bytes)   bytes,SUM(bytes)   sum_bytes   FROM   dba_free_space   GROUP   BY   tablespace_name)   b  
        WHERE   a.tablespace_name=b.tablespace_name    
            AND   next_extent>b.bytes  
        ORDER   BY   4,3,1      
    
  3. 查看所有表空间对应的数据文件:

    select tablespace_name,file_name from dba_data_files;
    
  4. 查看所有表空间及表空间大小:

    select tablespace_name ,sum(bytes) / 1024 / 1024 as MB from dba_data_files group by tablespace_name;
    
  5. 查看各个表的行数

    select t.table_name,t.num_rows from user_tables t where num_rows is not null ORDER BY NUM_ROWS DESC;
    

总结

因为是日常记录,所以本篇博文会持续更新的。如果有不错的命令推荐,也可以留言。我会视情况添加的。也推荐大家进行收藏,毕竟本文是持续更新当中。

更新记录

时间 更新日志
2020-08-19 14:10:26 初版

随缘求赞

如果我的文章对大家产生了帮忙,可以在文章底部点个赞或者收藏;
如果有好的讨论,可以留言;
如果想继续查看我以后的文章,可以点击关注
可以扫描以下二维码,关注我的公众号:枫夜之求索阁,查看我最新的分享!
在这里插入图片描述
拜拜

猜你喜欢

转载自blog.csdn.net/u013084266/article/details/113995104