Quantcast
Channel: Oracle
Viewing all articles
Browse latest Browse all 1814

Blog Post: Overuse of Group by/Having Clause

$
0
0
Hi, I found Nimish’s information via Linkedin…if you are a part of Linkedin…please ask me to add you to my group. Nimish goes into great detail on a query he tuned by using more analytical functions and where clauses for row elimination than the original query that used group by/having clauses. He got this query from a cost of over 5000 to just 20…of course it ran much better. The cost number in an Oracle explain plan is ROUGLY the number of physical IO operations the CBO thinks it will have to do to resolve the query request. When I say ‘thinks’…this number is based on math, statistics, educated guesses, along with IO device speed, processor speed, and a few other things. Nimish Garg’s entire blog is at: http://nimishgarg.blogspot.com/2016/06/view-pushed-predicate-powerful.html SELECT ... ... FROM geo_source_city b, ( SELECT cc_iso, full_name_city, COUNT (1) FROM geo_source_city GROUP BY cc_iso, full_name_city HAVING COUNT (1) = 1) d WHERE b.cc_iso = d.cc_iso AND b.full_name_city = d.full_name_city Part of the original query that had a cost of over 5000. SELECT * FROM (SELECT cc_iso, full_name_city, full_name, adm_module, latitude, longitude, COUNT (1) OVER (PARTITION BY cc_iso, full_name, full_name_city) cnt FROM geo_source_city) WHERE cnt = 1 Part of the query rewritten using analytical SQL with a cost of just 20! Nice job Nimish. I may have blogged on this before but this blog makes a good point on something I always point out in my SQL performance tuning class…use WHERE clauses to do row elimination, not the HAVING clause. GROUP BY processes all the rows then the HAVING clause tosses a bunch of them out…after the database has done the work to pull in the rows. Using a WHERE clause does row elimination during query processing, not performing extra work and tossing a bunch of it out. Nimish makes a good point here using analytical functions to gather the data so that a WHERE clause can be used instead of a GROUP BY and HAVING clause. There are 10+ ways to code most any SQL. The better analyst or programmer knows a variety of ways to produce the same result. Analytical functions are great for a lot of SQL processing and it is very performant, as you can see here. I did a blog series on analytical functions a while back…review my examples and see if you can use them in your day to day use of the Oracle RDBMS. Dan Hotka Oracle ACE Director Author/Instructor/CEO

Viewing all articles
Browse latest Browse all 1814

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>