How to calculate query that access system view cost in Oracle?
-
22-10-2019 - |
Question
For a query that returns if there are any active transactions within current session ,
SELECT COUNT(*)
FROM
v$transaction t
INNER JOIN v$session s ON (t.ses_addr = s.saddr )
INNER JOIN v$mystat m ON (s.sid = m.sid )
WHERE ROWNUM = 1;
EXPLAIN
shows 0 cost. However, that doesn't seem to be true. Periodically running this query in high load environment causes server to waste almost all its resources on it.
What is the right way to estimate impact of such queries (I believe the same problem occurs not for just this particular query, but anything that involves system views)?
Thanks.
Solution
Some (all?) dynamic V$
views are based not on dictionary tables but on memory structures, so the traditinoal stats are not collected and therefore the optimizer can't compute the cost of queries on those views.
However, the explain plan cost is only a rough approximation of the expected work so it shouldn't be your only way to evaluate the efficiency of a query. The true cost of a query can be revealed with a trace. Trace your actual query and all alternative queries and the tkprof will reveal which one is the most efficient.
For your query in particular, it seems you want to determine if your current session has uncommited work. Alternate queries are described in the following SO questions :
- How can I tell if I have uncommitted work in an Oracle transaction?
- How to find out if there is a transaction pending?
Can you try the queries in these questions and tell us which one is the most efficient (on your high load environment)?