I am looking for a script/process to check the amount of CPU (in terms of percentage) each query consumes at any point of time in my oracle database.
My Oracle version is : 12.1.0.2 Database is hosted on Exadata X7_2 It is a shared server, and many databases also running on the same server.
SELECT AVG(value/vp.cpu_count) FROM v$sysmetric_history,(SELECT value cpu_count FROM v$parameter WHERE name LIKE 'cpu_count') vp WHERE metric_name = 'CPU Usage Per Sec' AND group_id = 2 AND end_time > SYSDATE - 10/(60*24) AND end_time <= SYSDATE;
I am using this script to calculate the instance CPU utilization at a given time. Now, i would like to find out how much each SQL query is consuming in given CPU.
Example: at a given time if my CPU utilization is 80%, i would like to see how much CPU each sql query is contributing in that 80% of instance CPU.
To answer this question, you need historic performance metrics. The easiest way to get these is from the AWR view DBA_HIST_SQLSTAT [license required] (can also use StatsPack). As far as getting a percentage you’d need to calculate that from the total available CPU, but probably just ordering on CPU consumption would be enough. The query I use from the AWR tables: