Wednesday, August 7, 2013

A sample from the Hotsos SQL Optimization course

Hey everyone!  I'm in the big league now, I've got my first video out there! (OK you can't see me, but you can hear me!)  It's a sample from the 5 day Hotsos SQL Optimization course.  Great stuff! 

Click HERE to see it.

Have a great day!

Saturday, July 27, 2013

Some Rambles

I'm currently flying at some 36000 feet on my way home and was just thinking about some of the myths that seem to have a very strong life about Oracle.  Here are just a few that I tend to bump into as I teach my classes.

Full Table scans are bad - This one seems to be mostly dead but sometimes I still run across folks that somehow believe that if you see a full table scan in a query, that is the problem.  Hold on there cowboy.  It might be but personally I've seen INDEX scan turn out to be worse then full table scans.  To find one row a full table scan is really always a bad idea but there are plenty of times a full table scan is good.  In a very broad view, if you're going to get touch more then about 20% of the blocks of the table, you need to do a full table scan.

COUNT(1) is better then COUNT(*) - Nope. They do the exact same thing.  This one is so easy to test I can't believe the smart folks I've come across that haven't tested this to see that there is no difference.

You can Pin buffers in the buffer cache - No you can't.  The CACHE option at the table level only changes the behavior of a full table scan.  And this should be used with caution.  For a table of reasonable size Oracle reads the table into the LRU (Least Recently Used) of the LRU list.  This means that a table wouldn't stay in cache very long, likely it will just get cycled out as the scan goes.  Setting the cache option caused it to move the full scan to the MRU (Most Recently Used) end of the list.  This will cause the table to stay in cache LONGER, but not pinned.  Even the old KEEP pool didn't "keep" buffers, it was managed slightly differently but buffers will age out there too.

When you see X step in a plan it's always bad.  Really?  Do you really think the guys and gals that write the code for the optimizer are that stupid?  Seriously they are not going to put an option in to the optimizer that is "always" bad.  Sure some steps might seem to be bad in your environment more then not.  For example the SKIP SCAN on indexes isn't always good, but there are times when it rocks.  Any step in a plan can be "good" or "bad".  It's hard to just look at a plan and say with absolute certainty that a particular step is BAD.  To know for sure you need some run time stats to tell you what happened.  Yes if you know your tables and indexes very well (which I hope you do) you have a much better chance of looking at a plan and gaging the "goodness" or "badness" of a step.



Tuesday, June 25, 2013

Saturday, April 27, 2013

2013 Great Lakes Oracle Conference (GLOC)


Hey everyone!  Come on over to Cleveland Ohio and see lots of great oracle folks at the Great Lakes Oracle Conference on the 14th and 15th of May.  The line up of speakers are fantastic. There is even a per-conference workshop on Monday the 13th.

Go here for more details and the registration link.  Use this code to get a discount ATTENDGLOC. 

See you there!!

Monday, March 11, 2013

Hotsos Education Forums!!

We have created forums for our courses for you to use to ask questions and have a dialog about the topics covered in each course.  They are brand new and just announced at the Hotsos Symposium this past week.

I look forward to hearing from you there! Click HERE to check them out. 

http://support.hotsos.com/forums/

And I hope to see you at the Symposium next year!  (Save the date: first week of March as always.)


Friday, March 1, 2013

Thursday, January 24, 2013

Not In vs Not Exists

My last post was about IN and EXISTS but what about the NOT versions of these?  Are they the same also?  Nothing bets a test so let's take a look.

This time around I want to find out if there are any departments without any employees assigned.  First let's give it a go with the NOT IN.

select * from dept where deptno not in (select deptno from emp ) ;

This seems like a good idea, basicly we are saying give me all the dapartments that are in the DEPT but not in a list of departmets from the EMP table.  We get this results:

no rows selected

Which looks fine, we think there aren't any.   But what if I change it to a NOT EXISTS.

select * from dept d where not exists (select deptno from emp where deptno = d.deptno);

now I get:

         DEPTNO DNAME          LOC
--------------- -------------- -------------
             40 OPERATIONS     BOSTON

1 row selected.


Well that's not very nice!  It looks like I got wrong results here somewhere.  And I did.  The first one was incorrect, why?  Let's take a look at the plans for each, the NOT IN gives me this plan:

--------------------------------------------------------
| Id  | Operation                     | Name           |
--------------------------------------------------------
|   0 | SELECT STATEMENT              |                |
|   1 |  MERGE JOIN ANTI NA           |                |
|   2 |   SORT JOIN                   |                |
|   3 |    TABLE ACCESS BY INDEX ROWID| DEPT           |
|   4 |     INDEX FULL SCAN           | DEPT_DEPTNO_PK |
|*  5 |   SORT UNIQUE                 |                |
|   6 |    TABLE ACCESS FULL          | EMP            |
--------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   5 - access("DEPTNO"="DEPTNO")
       filter("DEPTNO"="DEPTNO")


The NOT EXISTS gives me this plan:

-------------------------------------------
| Id  | Operation          | Name         |
-------------------------------------------
|   0 | SELECT STATEMENT   |              |
|   1 |  NESTED LOOPS ANTI |              |
|   2 |   TABLE ACCESS FULL| DEPT         |
|*  3 |   INDEX RANGE SCAN | EMP_DEPT_IDX |
-------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   3 - access("DEPTNO"="D"."DEPTNO")
       filter("DEPTNO" IS NOT NULL)




What gives?  Why these to different plans and what is that "ANTI NA" thing in the NOT IN plan.  It's a logic thing that is the culprit. With the NOT IN if there is a null in the inner set the entire set is nullified.  In the EMP table, the employee King (the president, what a great name for the company president don't ya think?) is not in a department so his DEPTNO is NULL that ends up nullifying the entire set and we get no results. 

The "NA" in the join step stands for "Null Aware", which kicks in this logical rule that once there is a null in there, it's over.


So what to do?  Well if you want to use NOT IN, then make sure you do something about the nulls. Like this:


select * from dept 
where deptno 
not in (select deptno from emp where deptno is not null) ;


Now you get the same results and teh exact same plan as NOT EXISTS.  Try and see for yourself.

So IN and EXISTS are really the same, and NOT IN and NOT EXISTS can be, so long as you do something about those nulls.


One question that this query is able to answer is a question that has plagued many Oracle professions for many years, why is it that is company has never made any money?  We now know, there is no one in the operations department!