If your customers shared across different Oracle container databases and you want to charge them based on they Utilizations then below queries will help you.

(CPU)

SELECT 
    s.con_id, 
    p.pdb_name, 
    ROUND((AVG(s.cpu_consumed_time) / SUM(AVG(s.cpu_consumed_time)) OVER()) * 100, 2) AS cpu_used_percent
FROM V$RSRCMGRMETRIC s
JOIN CDB_PDBS p ON s.con_id = p.con_id
GROUP BY s.con_id, p.pdb_name
HAVING AVG(s.cpu_consumed_time) > 0 -- Exclude zero CPU usage
ORDER BY cpu_used_percent DESC;

(CPU + Storage in GB)

WITH cpu_usage AS (
    SELECT 
        s.con_id, 
        p.pdb_name, 
        ROUND((AVG(s.cpu_consumed_time) / SUM(AVG(s.cpu_consumed_time)) OVER()) * 100, 2) AS cpu_used_percent
    FROM V$RSRCMGRMETRIC s
    JOIN CDB_PDBS p ON s.con_id = p.con_id
    GROUP BY s.con_id, p.pdb_name
    HAVING AVG(s.cpu_consumed_time) > 0
), 
storage_usage AS (
    SELECT 
        a.con_id, 
        b.pdb_name, 
        ROUND(SUM(a.used_space) / 1024 / 1024, 2) AS used_gb,
        ROUND(SUM(a.tablespace_size) / 1024 / 1024, 2) AS allocated_gb
    FROM CDB_TABLESPACE_USAGE_METRICS a
    JOIN CDB_PDBS b ON a.con_id = b.con_id
    GROUP BY a.con_id, b.pdb_name
)
SELECT 
    c.con_id, 
    c.pdb_name, 
    c.cpu_used_percent, 
    s.used_gb, 
    s.allocated_gb 
FROM cpu_usage c
LEFT JOIN storage_usage s ON c.con_id = s.con_id
ORDER BY cpu_used_percent DESC;

Expected Output

CON_IDPDB_NAMECPU_USED_PERCENTUSED_GBALLOCATED_GB
5P_DB151.3614.6517.58
6P_SAMCO39.0711.7214.65
9P_DBACO6.657.819.77

Leave a comment