Select info about tables on skewed data slices

SELECT t.dsid,
t.ds_prnct_used,
t.ideal_ds_prnct_used,
t.ds_prnct_used_delta,
soss.allocated_bytes/1024/1024/1024 tot_tbl_allocated_gbytes,
soss.used_bytes/1024/1024/1024 tot_tbl_used_gbytes,
t.used_gbytes,
t.ideal_used_gbytes,
t.used_gbytes_delta,
t.dis_prcnt,
t.ideal_dis_prcnt,
t.dis_prcnt_delta,
sod.dbname,
sod.owner objownerusername,
sod.objname,
sod.objtype,
soss.used_min,
soss.used_max,
soss.used_avg,
soss.used_spread,
soss.skew,
sod.createdate,
t.tblid
FROM (
SELECT sodi.tblid,
sodi.dsid,
sodi.used_bytes,
sodi.used_bytes/1024/1024/1024 used_gbytes,
sodi.tot_tbl_used_bytes,
sodi.tot_tbl_used_bytes/(SELECT COUNT(*) FROM _t_dslice)/1024/1024/1024 ideal_used_gbytes,
dp.ds_prnct_used,
dp.ideal_ds_prnct_used,
dp.ds_prnct_used – dp.ideal_ds_prnct_used ds_prnct_used_delta,
(sodi.used_bytes – sodi.tot_tbl_used_bytes/(SELECT COUNT(*) FROM _t_dslice)) used_bytes_delta,
(sodi.used_bytes – sodi.tot_tbl_used_bytes/(SELECT COUNT(*) FROM _t_dslice))/1024/1024/1024 used_gbytes_delta,
sodi.used_bytes/sodi.tot_tbl_used_bytes*100 dis_prcnt,
1.0/(SELECT COUNT(*) FROM _t_dslice)*100 ideal_dis_prcnt,
sodi.used_bytes/sodi.tot_tbl_used_bytes*100 – 1.0/(SELECT COUNT(*) FROM _t_dslice)*100 dis_prcnt_delta
FROM (
SELECT tblid,
dsid,
SUM(used_bytes) used_bytes,
SUM(SUM(used_bytes))OVER(PARTITION BY tblid) tot_tbl_used_bytes
FROM _v_sys_object_dslice_info
GROUP BY tblid, dsid
) sodi
INNER JOIN
(
SELECT dsid,
SUM(extents_used)/SUM(extent_count)*100 ds_prnct_used,
SUM(SUM(extents_used))OVER(PARTITION BY NULL)/SUM(SUM(extent_count))OVER(PARTITION BY NULL)*100 ideal_ds_prnct_used
FROM _vt_disk_partition
WHERE isprimary
GROUP BY dsid
) dp ON sodi.dsid = dp.dsid AND sodi.used_bytes > 0
) t
INNER JOIN _v_sys_object_data sod ON t.tblid = sod.objid
INNER JOIN _v_sys_object_storage_size soss ON sod.objid = soss.tblid
INNER JOIN _v_sys_database d ON sod.dbname = d.objname
WHERE t.ds_prnct_used > 70
ORDER BY t.ds_prnct_used DESC, t.used_bytes_delta DESC
LIMIT 20;
SELECT tblid,
dsid,
ds_prnct_used,
ideal_ds_prnct_used,
ds_prnct_used_delta,
tot_tbl_used_gbytes,
used_gbytes,
ideal_used_gbytes,
used_gbytes_delta,
dis_prcnt,
ideal_dis_prcnt,
dis_prcnt_delta
FROM (
SELECT sodi.tblid,
sodi.dsid,
sodi.used_bytes,
sodi.used_bytes/1024/1024/1024 used_gbytes,
sodi.tot_tbl_used_bytes,
sodi.tot_tbl_used_bytes/(SELECT COUNT(*) FROM _t_dslice)/1024/1024/1024 ideal_used_gbytes,
sodi.tot_tbl_used_bytes/1024/1024/1024 tot_tbl_used_gbytes,
dp.ds_prnct_used,
dp.ideal_ds_prnct_used,
dp.ds_prnct_used – dp.ideal_ds_prnct_used ds_prnct_used_delta,
(sodi.used_bytes – sodi.tot_tbl_used_bytes/(SELECT COUNT(*) FROM _t_dslice)) used_bytes_delta,
(sodi.used_bytes – sodi.tot_tbl_used_bytes/(SELECT COUNT(*) FROM _t_dslice))/1024/1024/1024 used_gbytes_delta,
sodi.used_bytes/sodi.tot_tbl_used_bytes*100 dis_prcnt,
1.0/(SELECT COUNT(*) FROM _t_dslice)*100 ideal_dis_prcnt,
sodi.used_bytes/sodi.tot_tbl_used_bytes*100 – 1.0/(SELECT COUNT(*) FROM _t_dslice)*100 dis_prcnt_delta
FROM (
SELECT tblid,
dsid,
SUM(used_bytes) used_bytes,
SUM(SUM(used_bytes))OVER(PARTITION BY tblid) tot_tbl_used_bytes
FROM _v_sys_object_dslice_info
GROUP BY tblid, dsid
) sodi
INNER JOIN
(
SELECT dsid,
SUM(extents_used)/SUM(extent_count)*100 ds_prnct_used,
SUM(SUM(extents_used))OVER(PARTITION BY NULL)/SUM(SUM(extent_count))OVER(PARTITION BY NULL)*100 ideal_ds_prnct_used
FROM _vt_disk_partition
WHERE isprimary
GROUP BY dsid
) dp ON sodi.dsid = dp.dsid AND sodi.used_bytes > 0
) t
ORDER BY ds_prnct_used DESC, used_bytes_delta DESC
LIMIT 20;