0

My query had ordered hint. it was giving below cost and cardinality

enter image description here

When I removed ordered hint then it started giving below cost and cardinality.

enter image description here

in terms of performance which plan is better? I can put more details including query if required. I am not saying somebody to my work, but even smallest suggestion would be really helpful for me.

2 Answers 2

1

Impossible to say which is faster based on cost alone. Cost is only the amount of work the optimizer estimates it will take to execute a query a certain way. This will depend on your statistics and your query (and optimizer math). If your statistics don’t represent the data or your query has filters that it can’t estimate: you’re going to get a misleading cost calculation. What you need to remember is Garbage In - Garbage Out, ie bad stats will give you a bad plan.

If you’re putting hints in, generally that means the execution plan that the optimizer came up with wasn’t deemed good enough. In those cases, you’re essentially saying that Oracle’s cost calculation was wrong - so we definitely shouldn’t use it to see which query is faster.

Luckily, you have everything you need to determine which query is faster - you have your database and the queries, you just need to execute them and see.

I suspect neither is particularly fast, but if you want to improve them you’re going to need to look at where the work is really going in executing them. The final cost in those queries are very high so maybe it has correctly identified an unavoidable (based on how the query is written and what structures exist) high cost operation. Reading over the execution plan yourself and considering how much effort each step would be is always a good idea.

The easy way to begin tuning it would be to get out the Row Source Execution Statistics for a complete execution and target the parts of the plan that are responsible for the most actual time. See parts 3 and 4 of https://ctandrewsayer.wordpress.com/2017/03/21/4-easy-lessons-to-enhance-your-performance-diagnostics/ for how to do that - if anything it will give you something you can share that concrete advise can be given on (if you do share it then don’t forget to include the full query).

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks you for explanation. It is very helpful. Could you please tell me, is there any relation between cost and cardinality?
The optimizer estimates cardinality for inputs into its costing math. If it thinks it will access 20 scattered rows by an index (cardinality 20) then it will cost 20 times more than accessing 1 row (cardinality 1). Some things aren't such linear relations - full scanning a table and getting 100 rows will cost the same as full scanning the same table and getting a million rows. There has been decades worth of evolution in the optimizer so it is obviously not going to be simple. If you want to understand it further then get "Oracle Cost-Based Fundamentals" by Jonathan Lewis
1

Normally cost comparison is enough to say whether using hint makes sense. Usually hints make it worse when statistics is gathered properly.

So, the one with less query cost is better.

I always look on usage of cpu, logical reads (reads from RAM) and physical reads (reads from disk). The better option uses less resources.

1 Comment

The optimizer already chooses the plan with the least cost, that’s it’s job. For a hint to make it come up with a “cheaper” plan it would need to either come up with a plan that was never costed (unusual), or manipulate the costing of the query so something else is considered cheaper (at which point the two costs cannot be compared as they were made with different inputs),

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.