Sep 28, 2015

Query for Table size along with Index and lobs






Query to get the above output:

SELECT Sub."Table_Name",
  sub."Number of Rows",
  sub."Number of Indexes",
  sub."Number of LOB Columns",
  sub."Index_Size" "Index_Size",
  sub."Table_Size" "Table_Size",
  sub."LOB_Size" "LOB_Size",
  sub."Index_Size"+sub."Table_Size"+sub."LOB_Size" "Total_Size_MB"
FROM
  ( WITH index_cnt AS
  (SELECT b.table_name,
    COUNT(b.index_name) "index_count"
  FROM user_indexes b
  GROUP BY b.table_name
  ),
  lob_cnt AS
  (SELECT c.table_name,
    COUNT(c.column_name) "lob_count"
  FROM user_lobs c
  GROUP BY c.table_name
  ),
  ind_sz AS
  (SELECT e.table_name,
    SUM(Bytes/1024/1024) "index_size"
  FROM user_segments d,
    user_indexes e
  WHERE d.segment_name = e.index_name
  AND d.segment_type LIKE 'INDEX%'
  GROUP BY e.table_name
  ),
  tab_sz AS
  (SELECT d.segment_name,
    SUM(Bytes/1024/1024) "table_size"
  FROM user_segments d
  WHERE d.segment_type LIKE 'TABLE%'
  GROUP BY d.segment_name
  ),
  lob_sz AS
  (SELECT e.table_name,
    SUM(Bytes/1024/1024) "lob_size"
  FROM user_segments d,
    user_lobs e
  WHERE d.segment_name = e.segment_name
  GROUP BY e.table_name
  )
SELECT a.table_name "Table_Name",
  NVL(a.num_rows,0) "Number of Rows",
  NVL(index_cnt."index_count",0) "Number of Indexes",
  NVL(lob_cnt."lob_count",0) "Number of LOB Columns",
  NVL(ind_sz."index_size",0) "Index_Size",
  NVL(tab_sz."table_size",0) "Table_Size",
  NVL(lob_sz."lob_size",0) "LOB_Size"
FROM user_tables a ,
  index_cnt,
  lob_cnt,
  ind_sz,
  tab_sz,
  lob_sz
WHERE a.table_name=index_cnt.table_name(+)
AND a.table_name  =lob_cnt.table_name(+)
AND a.table_name  =ind_sz.table_name(+)
AND a.TABLE_NAME  =tab_sz.segment_name(+)
AND a.table_name  =lob_sz.table_name(+)
  ) sub
ORDER BY 2 DESC nulls last; 

*** Courtesy to my friend Hari Krishna for writing this above query ***

No comments:

Post a Comment