Optimizing SQL Queries in Oracle

Embed Size (px)

Citation preview

  • 8/19/2019 Optimizing SQL Queries in Oracle

    1/69

    Optimizing SQL Queries in Oracle

    Introduction

    Hello and welcome to the Pluralsight course, Optimizing SQL Queries inOracle. My name is Scott Hecht and in this course I'll e discussing se!eralthings you can do to speed up your SQL "ueries. #e'll co!er e!erything $romrearranging your tale data, to modi$ying your SQL code, as well as how touse inde%es and partitions. Let's get started.

    &ourse O!er!iew

    e$ore we go o!er the course contents in detail, let's gi!e an o!er!iew o$ thecourse in general. #e start o( descriing how to speed up your SQL "ueriesy )rst optimizing your tale data. *hat is, we tal+ aout minimizing thecolumns and rows in your data using a !ariety o$ techni"ues, such as normal$orms, where to place nulls in a tale to sa!e space, and so on. #e thendescrie how to speed up your SQL "ueries y $ocusing on your SQL codeitsel$. or e%ample, using intersect instead o$ an inter-oin can speed up your"ueries tremendously in certain instances. e%t, we tal+ aout how tocreate and use inde%es on one or more columns in your tales. inally, wediscuss how to create and use partitions to rea+ up !ery large tales into

    smaller pieces called partitions, so that only the desired data, ased on yourSQL "ueries where clause, is accessed y Oracle. *he reason I ha!e orderedthis lecture as shown is to pre!ent you $rom -ust -umping to inde%es orpartitions as i$ they will e the cure all $or all o$ your SQL "uery prolems. /properly structured tale or set o$ tales, along with well written SQL code,can go a long way to speeding up your SQL "ueries and it is only at thatpoint that inde%es and partitions should e considered i$ your SQL "ueriesare still ta+ing too long to run. On the other hand, i$ you are a eginningdataase administrator tas+ed with creating a dataase you may want toconsider all o$ these things up$ront while still in the dataase design phase.

    &ourse &ontents

    e%t, let's go o!er the course contents in more detail. O$ course, we start o( with the course introduction, which is the su-ect o$ this module. #e discussasic optimizations as related to data, that as we tal+ aout how to shrin+down the size o$ your tales y arranging them in third normal $orm. #ediscuss how nulls are stored in the tale and how arranging your columnsappropriately can also shrin+ down the size o$ your tale. In the ne%t module

    we discuss how you can change your SQL code to get your "ueries to run$aster. o inde%es or partitions are created in this section, only simple

  • 8/19/2019 Optimizing SQL Queries in Oracle

    2/69

    changes to your SQL code is e%plored. In the ne%t $ew modules we discuss amore ad!anced SQL optimization techni"ue called inde%es. #e start o( withan o!er!iew o$ inde%es. I$ appropriately applied, inde%es can ma+e your SQL"ueries e%ecute much much $aster, ut with great power comes greatresponsiility. Inde%es ta+e up space in your dataase, so you ha!e to

    alance speed !ersus space usage. *here are se!eral types o$ inde%es wewill e discussing, such as 0*ree inde%es, which are the oeing 121 o$inde%es. *hat is, they are the inde% most o$ten used in a tales columns andare considered a general purpose inde%. itmap inde%es are used when thecolumn or columns you want to inde% ha!e a small or medium numer o$distinct !alues, as compared to the total numer o$ rows in the tale. ore%ample, in the data used throughout the course the responded gendercolumn contains two distinct !alues, m and $, as compared to the totalnumer o$ rows, which is )!e million rows. / itmap inde% only respondedgender column could e considered depending on the SQL "uery sumittedto the dataase. #e then mo!e on to discuss se!eral additional types o$

    inde%es, such as $unction ased inde%es, used when you're SQL "ueries$re"uently contain a speci)c $unction applied to a column. ow, normallyapplying the $unction to a column that is inde%ed will pre!ent Oracle $romusing that inde%, ut $unction ased inde%es )% that. #e also rie3y discussinde% organized tales, as well as itmap drain inde%es. In the remainingmodules we discuss partitions, which are used to rea+ up !ery large talesased on a speci)c column or columns, so that it appears to Oracle as i$ thetale is actually se!eral indi!idual tales. or e%ample, we could partitionour data into 45 partitions using the sur!ey year column and i$ your SQL"uery susets y say, sur!ey year 6552, Oracle +nows enough to s+ip o!erthe partitions containing the other years. *his can speed up your "ueries

    tremendously. Similar $or the modules on inde%es, we start o( this series o$modules with an o!er!iew o$ partitions. #e'll then discuss se!eral types o$partitions, such as list, range, and hash partitions. #e then mo!e on todiscuss composite partitions, which allow you to partition y one methodand then su0partition using another method. or e%ample, we can partition)rst y sur!ey year and then su0partition y responded gender withinthose partitions. inally, we discuss the interaction etween partitions andinde%es.

    Module &ontents

    Let's go o!er the contents o$ the remaining part o$ this particular module.#e discuss who will ene)t $rom this course. #e tal+ aout the dataase!ersion used to prepare this course. #e'll loo+ at the data used throughoutthe course. #e'll then tal+ rie3y aout *alespaces and how they are usedwith tales, inde%es, and partitions. #e'll also tal+ aout the Oracle "ueryoptimizer, which is used to determine how your "uery will e e%ecuted. #eremind you that se!eral great resources are Oracle's on Pluralsight'swesites themsel!es. e%t, we also remind you that your greatest resource

    is your company's dataase administrator, or 7/, and we end this modulewith a rie$ summary.

  • 8/19/2019 Optimizing SQL Queries in Oracle

    3/69

    #ho #ill ene)t8

    #ho will ene)t $rom this course8 irstly, SQL de!elopers will ene)t greatly$rom this course, especially i$ they'!e ne!er used or only casually usedinde%es and partitions e$ore. I$ you connect to Oracle !ia a third party toolsuch as S/S, SPSS, 9, and so on, you can ene)t $rom this course as well.It's not always necessary to pull all o$ the data ac+ $rom Oracle into say,S/S datasets, in order to then suset the data down. *hat is, i$ you're goingto suset the data, why pull it all ac+ into S/S8 #hy not -ust suset thedata in Oracle and then pull ac+ all o$ the susetted data8 / cleanseparation o$ tas+s etween your dataase and your statistical so$tware willmost li+ely wor+ in your $a!or. *hat is, let Oracle do the data manipulationand let S/S, SPSS, and 9 do what they're good at, namely the statistics and

    the graphics. inally, i$ you're a new dataase administrator, yes that's howyou actually appear to people, then you will de)nitely ene)t $rom thiscourse, although you will still ha!e to read through the manuals ecausewell, that's what good 7/s do.

    7ataase :ersion

    Let's tal+ aout the Oracle dataase !ersion used to prepare this course. *oprepare this course I used Oracle 44g release 6, although Oracle 46c is out,an introductory course, such as this, will su;ce with Oracle 44g release 6.

     *he client tool I'm using is Oracle SQL 7e!eloper, which is $reely a!ailaleon Oracle's wesite. inally, i$ you don't ha!e access to an Oracle dataasein wor+, that's sad, or you don't ha!e Oracle SQL 7e!eloper, you candownload oth $rom Oracle's wonder$ul wesite at www.oracle.com.

    7ata HIS*O9I&/L>7/*/ tales and the $act and dimension tales

    during the course o$ this lecture in order to show SQL runtime comparisons.ow, let's go o!er the columns in this tale. 9?SPO7?*>I7, this column

  • 8/19/2019 Optimizing SQL Queries in Oracle

    4/69

    contains a numer representing a uni"ue sur!ey respondent.9?SPO7?*>/M?, this column contains the respondent's $ull name.9?SPO7?*>/779, this column contains the respondent's address.9?SPO7?*>&I*=, the name o$ a respondent's city o$ course.9?SPO7?*>S*/*?, the respondent's two letter @IP&O7?, this column contains the respondent's )!e digit PHO?>I7, this column contains anumer representing a uni"ue candy ar. or e%ample, one represents the

     *hree Mus+eteers candy ar. &/7=/9>/M?, this is the $ull name o$ thecandy ar, $or e%ample *hree Mus+eteers. &/7=/9>M9>I7, this columnrepresents the manu$acturer o$ the associated candy ar, $or e%ample,

     *hree Mus+eteers is manu$actured y our manu$acturer I7 numer 2B.&/7=/9>M9>/M?, this is the $ull name o$ the manu$acturer o$ the

    associated candy ar. or e%ample, *hree Mus+eteers is manu$actured ythe )ne $ol+s at Mars. &/7=/9>#?IAH*>O@, this is the $a+e weight inounces o$ the $a+e candy ar, the $a+e ser!ing respondent was not reallysent. Let's )nish up our list o$ columns in the &/7=/9>HIS*O9I&/L>7/*/tale. S7/*?, this column is the date the sur!ey was gi!en to therespondent and is a date data type. S=?/9, this column is -ust theyear associated with the sur!ey date and is a $our digit numer.

     */S*?>9/*IA, this is the rating o$ the candy ar on the attriute taste andranges $rom 4045 where 4 means the candy ar tastes li+e a dead hamsterand 45 means the candy ar tastes amazoid. /PP?/9/&?>9/*IA, this isthe rating o$ the candy ar on the attriute appearance and ranges $rom 40

    45 where 4 means the candy ar loo+s aw$ul and 45 means the candy arloo+s wonder$ul. *?C*9/*IA, this is the rating o$ the candy ar on theattriute te%ture and ranges $rom 4045 where 4 means the candy ar has ate%ture o$ sand and 45 means the candy ar has a pleasing te%ture.O:?9/LL>9/*IA, this is the o!erall rating o$ the candy ar on a scale $rom4045. LID?LIHOO7>P/9S>&OS

  • 8/19/2019 Optimizing SQL Queries in Oracle

    5/69

    transparent to you since your dataase administrator has associated ade$ault talespace with your username or schema. /ny tale or inde% youcreate will, y de$ault, e placed on this *alespace, ut you mayoccasionally want to store tales, inde%es, and partitions in separatetalespaces in order to, among other things, reduce auto contention

    etween these o-ects. irst, let's tal+ aout Single0ile 7ataases. / Single0ile 7ataase contains all o$ your tales and inde%es within a single )lestored on dis+. I$ you'!e e!er used Microso$t /ccess, and I suspect you ha!e,you +now that /ccess stores your data, as well as inde%es, in a single .md)le. On the other hand, Oracle ma+es use o$ one or more physical )les tostore dataase o-ect such as tales, inde%es, and other good stu(. #henyour dataase administrator creates a new dataase he or she names oneor more )les on dis+ where the dataase will store your tales and so on.

     *hese )les on dis+ are physical )les, since they actually e%ist on dis+. /talespace is associated with one or more )les. *hat is, a talespace is alogical construct and is associated with se!eral physical )les on dis+. /

    single physical )le can only e associated with one talespace though. /smentioned, your tales and inde%es are stored within the talespace your7/ has setup $or your dataase. ?ach dataase can contain se!eraltalespaces, as well as associated physical )les on dis+, ut your 7/ cancreate more talespaces and )les, i$ the need arises. or e%ample, here's agraphical representation o$ )les, talespaces, tales, and inde%es. #e ha!etwo talespaces, *S>O? and *S>*#O. *a+e note that *S>O? onlycurrently has two physical )les associated with it, whereas *S>*#O hasthree. /lso, note that *S>O? currently houses two tales and an inde%,whereas *S>*#O contains -ust two tales. ow, when creating one or moretales, inde%es or partitions, you can speci$y a talespace other that the

    de$ault. ormally, SQL de!elopers don't do this, although dataaseadministrators may choose to do this. In any case, I show you how to do thisin se!eral places during the course, ut you can ignore it unless you reallyneed it.

    Oracle Query Optimizer

    Let's tal+ rie3y aout the Oracle Query Optimizer. #hat is the Oracle Query

    Optimizer8 Let me put it to you li+e this. #hen you ha!e a lot o$ errands torun what goes through your mind8

  • 8/19/2019 Optimizing SQL Queries in Oracle

    6/69

    rain assigns weights, or costs, to each o$ the routes and the route with thelowest total cost is the one you use. / similar thing is done when you sumita SQL "uery to Oracle. Oracle analyzes your "uery and decides the estpossile way to e%ecute your "uery gi!en serial cost o$ per$orming tas+s,such as $ull tale scans !ersus using an inde%. *he est way to e%ecute your

    "uery is called the e%ecution plan and, in this case, the optimizer is also+nown as the cost ased optimizer ecause, -ust li+e deciding the est wayto run errands, Oracle chooses the est e%ecution plan ased on the totalcost o$ running your "uery. ow, in order to roll out Oracle's "uery optimizerto determine the est e%ecution plan $or your "uery, you need to help italong y gathering dataase rele!ant in$ormation, such as the numer o$rows in the tales appearing in your "uery, along with their a!erage runlength, the numer o$ distincti!e !alues within the columns, the numer o$nulls in the columns, in$ormation aout inde%es, and so on. *hese are calledoptimizer statistics and are used y the "uery optimizer to create the estpossile e%ecution plan $or your "uery. #e'll tal+ aout how to easily gather

    optimizer statistics, i$ necessary, later on in the course. ow, I'm not sayingyour SQL "uery is ad or wrong, I'm saying that Oracle can determine theest possile plan o$ attac+ to e%ecute your "uery, -ust li+e you planning onthe est possile route to get your errands done. or e%ample, the OracleQuery Optimizer can decide which inde% to use or to use no inde%es at all.

     *he Oracle Query Optimizer can also decide the est possile way to -ointwo or more tales together. or most o$ us programmers out there, whenyou much two S/S datasets together what do you normally do8 #ell, usuallyyou do a sort, merge maneu!er. Oracle's Query Optimizer can decide toper$orm a sort and merge maneu!er as well or it can decide to use one o$the other merge techni"ues a!ailale to Oracle, nested loops, hash -oins,

    and so on. In later modules we'll show you how to determine i$ Oracle isactually using your inde%es or not y ta+ing a loo+ at the e%ecution planitsel$. #hile we won't go into too much detail, we will point out a $ew thingsthat should help you speed up your "ueries. One thing to watch out $orthough, is that the use o$ an inde% is not always the est plan o$ attac+ anda $ull tale scan may e more appropriate in certain instances.

    Oracle and Pluralsight 9esources

    Let's tal+ aout resources such as Oracle and Pluralsight. I$ you )nd yoursel$ still in need o$ more in$ormation in the topics presented in this course,you're )rst stop should e to Oracle's wesite, as well as to Pluralsight.com.On Oracle's wesite you can download their entire documentation undlecontaining all their documentation. I$ you pre$er not to download all o$ that,you should instead download the $ollowing. *he Oracle dataase SQLLanguage 9e$erence Manual, this manual contains all the SQL, 77L, and7ML you are li+ely to use. *he Oracle 7ataase 7ata #arehousing Auide,this manual goes into detail aout inde%es and partitions, as well as a lotmore. *he Oracle 7ataase PLESQL Pac+ages and *ypes 9e$erence. *his

    manual details all the pac+ages and procedures a!ailale to you, utspeci)cally, the 7MS stats pac+age, which is used to gather optimizer

  • 8/19/2019 Optimizing SQL Queries in Oracle

    7/69

    statistics. aturally, don't $orget aout Pluralsight $or more course.Speci)cally, Hugo Dornelis has created a !ery nice lecture called relationaldataase design on dataase design course, ut it also includes se!eralsections on the normal $orms. 7a!id erry has created a course entitledOracle Per$ormance *uning $or 7e!elopers, which is an ad!anced course on

    the topic I'm aout to present to you. Please chec+ out oth o$ theselectures when you ha!e a moment.

     =our 7ataase /dministrator

    Let's tal+ aout your dataase administrator. I$ you tried all the things I'!esuggested in this course, all the Oracle dataase related Pluralsight courses,the Oracle manuals, Aoogle, stac+o!er3ow.com, as+tom.com, and so on,and you still can't get your SQL "uery to e%ecute in a reasonale amount o$time, it's time to email your dataase administrator. He or she really is theest resource in your company. Include all the steps you'!e gone through toattempt to speed up your "uery and include all rele!ant SQL code. *he lastthing you should e!er do is send an email demanding your 7/ ma+e yourSQL code run $aster. *hat stu('s not going to wor+. *he approach outlinedao!e will most li+ely get you, not only a response to your email, ut somerespect $rom your dataase administrator, and that's a good thing.

    Summary

    In summary, although this was -ust the course contents, we did learn a $ewthings. #e went o!er the entire course contents. #e tal+ed aouttalespaces and how each talespace is associated with one or morephysical )les on dis+ and that each talespace could contain tales, inde%es,partitions, as well as other o-ects. #e tal+ed aout the Oracle QueryOptimizer, which ta+es your "uery and the optimizer statistics and createsthe est possile e%ecution plan $or your SQL code. #hen all else $ails, tal+to you dataase administrator. He may e schizophrenic, ut he's nicepeople. In the ne%t module we discuss SQL optimizations related to your

    data.

    asic SQL Optimization Part I

    Introduction

    Hello and welcome ac+ to the Pluralsight course, Optimizing SQL Queries in

    Oracle. My name is Scott Hecht and in this module we'll ta+e a loo+ at someasic methods to optimize your SQL "ueries with changes applied only to

  • 8/19/2019 Optimizing SQL Queries in Oracle

    8/69

    your data. *he in$ormation presented in this module usually should eper$ormed )rst e$ore mo!ing on to the other optimization techni"ues wetal+ aout later on in the course, so let's get started.

    Module &ontents

    irst though, let's go o!er the module contents. *his is the )rst o$ twomodules on asic optimizations. *he )rst part deals with optimizationsrelated to your data. In the ne%t module we'll tal+ aout asic optimizationsrelated to your SQL code. #e start o$ this module with a asic e%planation o$ Primary and oreign Deys. Later on in this module we tal+ aout these inmore detail. I assume that you'!e een e%posed to this e$ore, so it should

     -ust e a reminder. #e ne%t descrie )rst, second, and *hird ormal orms,which allow you to ta+e a !ery large tale, li+e our&/7=/9>HIS*O9I&/L>7/*/ tale, and slim it down into se!eral relatedtales. 7ataases lo!e slim. #e then re!isit Primary and oreign Deys ande%plain in the line and out o$ line constraints, although identi$ying yourtales Primary and oreign Deys won't necessarily speed up all o$ your"ueries, they will e !ery help$ul when -oining two or more tales togethery these +eys. #e tal+ a little it aout remo!ing in$re"uently accessedcolumns and we end this module with a chat aout how I7 in this )le. ow, +nowing this allows me to sa$ely code, saycan't count star on this tale, and I will e secure +nowing that the numerreturned is the numer o$ uni"ue candy ars in that tale. I$ this talerepeated candy ars, my count star would return an incorrect !alue. /sanother e%ample, here's a tale containing )!e columns. *he columns

     */S*?>9/*IA and O:?9/LL>9/*IA are ratings $rom a particular

  • 8/19/2019 Optimizing SQL Queries in Oracle

    9/69

    respondent $or a particular candy ar on a particular sur!ey date. ecauseo$ that, the three columns, 9?SPO7?*>I7, &/7=/9>I7, andS7/*? ta+en together, ma+e up the Primary Dey. Gust li+e $or the&/7=/9>I7 in the pre!ious e%ample, gi!en a where clause where say,9?SPO7?*>I7 is 4 and &/7=/9>I7 is 1B, and S7/*? is March

    64, 654, I will e sure that I will get ac+ only a single row o$ data. ow,occasionally tales contain an additional single column that acts as thePrimary Dey. or e%ample, i$ I create a column that contains a row numer$or each row in the tale, that could e the Primary Dey instead o$ the threecolumns shown. I$ that's the case, the three columns shown will e +nown asthe alternate +ey or alternate Primary Dey, whereas the row numer columnwould e the Primary Dey. *here's nothing wrong with this, ut as $ar asunderstanding the data contained within the tale, the three columns in redare more help$ul than the 9O#>I7 column. Dnowing this $act allows us toper$orm some complicated SQL "ueries such as where the a!erage o!erallrating $or each respondent $or each candy ar across all the sur!eys ta+en.

    /s you see here, I can do this y grouping y 9?SPO7?*>I7 and&/7=/9>I7 -ust ecause I +now what the Primary Dey is. ow that we'!elearned what a Primary Dey is, what's a oreign Dey8 / oreign Dey iscolumn that appears in one tale, ut is actually a Primary Dey in acompletely di(erent tale. *he tale shown is -ust the candy ar tale $romthe pre!ious slide with one additional column, the &/7=/9>M9>I7, whichis -ust the identi)cation numer o$ the manu$acturer o$ the candy ar. /syou see, this column is not the Primary Dey o$ this tale, &/7=/9>I7 stillis, ut &/7=/9>M9>I7 is the Primary Dey within its own tale, whichcontains one row $or each candy ar manu$acturer. Dnowing this allows youto sa$ely -oin these two tales together as shown without rows repeating

    accidentally.

    ormal orms 0 Part I

    Let's tal+ aout optimizations related speci)cally to data. #hat do I mean ythat8 #ell, without data you won't ha!e anything to "uery, so data is arather important component o$ your SQL "uery. ow, ignoring inde%es andpartitions, which are introduced later on in the course, i$ your tales are

    huge with dozens o$ columns and illions o$ rows, then you proaly e%pectyour "ueries to run slow, ut that doesn't mean there aren't ways o$speeding up your "ueries a little it. #hat can we do8 /lthough you'!eproaly seen this in a college course or read aout it in a SQL oo+,modi$ying your data to e in *hird ormal orm can decrease your SQL"uery runtime y eliminating redundant data or mo!ing columns to othertales. 9ecall that our tale, &/7=/9>HIS*O9I&/L>7/*/ has 66 columns,some o$ which are large te%t strings. 66 columns is a pretty large recordsize. &an we do etter8 ote )rst, that dataases lo!e s+inny tales, that is,tales with only a $ew columns. I'll e%plain why in -ust a moment. ow youmay ha!e thought that ormal orms were -ust something you learn in

    college and weren't really used in the real world. #ell, we're seeing themrun with the trans$orm &/7=/9>HIS*O9I&/L>7/*/ tale into *hird ormal

  • 8/19/2019 Optimizing SQL Queries in Oracle

    10/69

    orm signi)cantly reduces the numer o$ columns, which then has thepotential to reduce down our SQL "uery runtime. I'll go o!er the ormalorms in detail in -ust a moment. inally, -ust a reminder. 7ataases lo!es+inny tales, so a s+inny tale should e%ecute your "uery $aster than acorresponding $at tale. #hy is that8 Let's ta+e a loo+ at the inside o$ a

    typical hard dri!e. /s you can see, there is a spinning platter, which holdsthe data, and a readEwrite armature, which reads $rom and writes to thedis+. ?ach row o$ data in the tale is stored on the hard dri!e one row a$terthe ne%t. ow, in order to mo!e $rom one row to the ne%t the readEwritearmature has to wait $or the platter to spin around to the ne%t row. I$ eachrow is !ery large, as it is in our &/7=/9>HIS*O9I&/L>7/*/ tale, it ta+estime to mo!e $rom row to row. On the other hand, i$ each row is tiny thereadEwrite armature can scan through each row much "uic+er, and it's thegoal, or at least one o$ the goals, o$ *hird ormal orm to slim down a taleso that each row is as small as possile, ut still allows us to per$orm anyanalysis we need to do. Let's ta+e a loo+ at the ormal orms in more detail.

    Let's start o( with the irst ormal orm. In a nutshell, a tale is in irstormal orm i$ there are no repeating )elds. or e%ample, suppose you hada tale with two columns, 9?SPO7?*>I7 and PHO?>I7,and an additional column indicating the type o$ phone numer, say cell

    phone, wor+ phone, home phone, and so on. e%t, let's tal+ aout Secondormal orm. or a tale to e in Second ormal orm it must e in irstormal $orm and ha!e no attriutes that are associated with only part o$ thePrimary Dey. In the tale shown we ha!e )!e columns, 9?SPO7?*>I7,&/7=/9>I7, S7/*?, &/7=/9>#?IAH*>O@, andO:?9/LL>9/*IA. /s discussed in the pre!ious module, the )rst threecolumns are the Primary Dey o$ this tale. ow, ta+e a loo+ at the column&/7=/9>#?IAH*>O@. =ou'll note that the ounces displayed are all 2.6 $or&/7=/9>I7 e"uals 4. *his ma+es sense since a candy ar's weight isassociated with a candy ar and neither the respondent who )lled in thesur!ey nor the date o$ the sur!ey. *o put it another way, the column&/7=/9>#?IAH*>O@ is associated with the &/7=/9>I7 column onlyand not with the rest o$ the Primary Dey. / candy ar's weight has nothing todo with a respondent and nothing to do with a sur!ey date. ecause o$ this,the tale is not in Second ormal orm. *o )% this situation we mo!e thecolumn &/7=/9>#?IAH*>O@ into its own tale along with the&/7=/9>I7 column. #e ta+e a loo+ at this more closely in -ust a moment.Let's continue our discussion on the ormal orms y loo+ing up the *hirdormal orm. or a tale to e in the *hird ormal orm it must )rst passthe irst ormal orm test, as well as the Second ormal orm test, andadditionally, all o$ the non0+ey columns in the tale must e associated with

    the entire +ey and not -ust part o$ it. or e%ample, we already saw that&/7=/9>#?IAH*>O@ is only associated with the &/7=/9>I7 column,

  • 8/19/2019 Optimizing SQL Queries in Oracle

    11/69

    so we got rid o$ it and mo!ed it to a di(erent tale. e%t, the9?SPO7?*>/M? column is associated with the 9?SPO7?*>I7 columnonly, that is, a respondent's name doesn't change ecause o$ the candy arhe's een assigned to write, so 9?SPO7?*>/M? has to go. inally, let'sloo+ at the O:?9/LL>9/*IA. *his column is the response gi!en y a

    particular respondent, $or a particular candy ar, on a particular sur!eydate. *hus, O:?9/LL>9/*IA is associated with the entire Primary Dey andnot -ust part o$ it, so O:?9/LL>9/*IA can stay. #hat's the ig deal aoutall this ormal orm stu(8 /mong other things, placing a large tale in *hirdormal orm allows you to remo!e columns out to smaller tales, remo!ingduplicated data. On the ne%t slide we're going to ta+e the&/7=/9>HIS*O9I&/L>7/*/ tale and its 66 columns and put it in *hirdormal orm. /s you'll see, not only will this reduce down the total numero$ columns in our main tale, ut as a conse"uence the record size $or eachrow will e smaller and our "ueries should run $aster.

    ormal orms 0 Part II

    Our goal now is to trans$orm our &/7=/9>HIS*O9I&/L>7/*/ tale into asingle s+inny tale containing -ust those columns directly related to thePrimary Dey columns. y doing this we'll need to create one or more smallertales containing additional columns we'!e remo!ed $rom the larger tale.

     *he s+inny tale is +nown as the $act tale and the additional in$ormationtales are called dimension tales. *a+en together, the $act and dimension

    tales -ust recreate the large &/7=/9>HIS*O9I&/L>7/*/ tale, utwithout the repeated data and, as mentioned, s+inny tales should allow ourSQL "ueries to e%ecute $aster. ow, y rea+ing the large tale into $act anddimension tales you aren't losing in$ormation you're -ust ma+ing it moremanageale $or the dataase to process. /gain, thin+ ac+ to the image o$the hard dri!e shown a $ew slides ac+. Let's start o( y creating the $acttale. #ell, ased on the de)nition o$ *hird ormal orm, we need to +eepall o$ those columns that are directly associated with our Primary Dey, so)rst we need to identi$y the Primary Dey columns. /s stated earlier, the$ollowing columns are good candidates $or the Primary Dey,9?SPO7?*>I7, &/7=/9>I7, and S7/*?. e%t, i$ you ta+e a loo+

    at the list o$ columns, the $ollowing columns are associated completely withour Primary Dey columns, */S*?>9/*IA, /PP?/9/&?>9/*IA, *?C*9/*IA, O:?9/LL>9/*IA, LID?LIHOO7>P/9S>&OS9/*IA and so $orth, is completely associated with a speci)c9?SPO7?*>I7, &/7=/9>I7, and S7/*?. On the other hand, the9?SPO7?*>/M? is only associated with e!ery 9?SPO7?*>I7 and assuch, is remo!ed $rom the $act tale and placed into a dimension tale. Ourne%t goal is to create se!eral dimension tales holding the in$ormation wee%cluded $rom our $act tale. irst, we create a dimension tale associated

    with the respondents. *his tale will contain the 9?SPO7?*>I7 as thePrimary Dey and the 9?SPO7?*>/M?, 9?SPO7?*>/779, and so on

  • 8/19/2019 Optimizing SQL Queries in Oracle

    12/69

    as the attriutes. *he ne%t dimension tale contains all o$ those columnsrelated to the candy ar itsel$. It would contain the &/7=/9>I7 as the solePrimary Dey, as well as the &/7=/9>/M?, the &/7=/9>M9>I7, andthe &/7=/9>#?IAH*>O@, and so on $or the rest o$ the dimension tales.

    ormal orms 0 Part III

    /$ter drawing out our tales on paper, let's ta+e a loo+ at our )nal dataasedesign. In the center o$ e!erything is our $act tale called &/7=/9>/&*,which contains only the columns associated with the Primary Dey columns,9?SPO7?*>I7, &/7=/9>I7, and S7/*?, and are shown ao!ethe horizontal line. *his tale is in *hird ormal orm ecause the remainingcolumns, those appearing elow the horizontal line, rely directly on all threecolumns o$ the Primary Dey and not -ust part o$ it. /s you can see, we ha!esigni)cantly $ewer columns in this tale than in the&/7=/9>HIS*O9I&/L>7/*/ tale, B columns !ersus 66 columns. Here weha!e the &/7=/9>7IM tale, candy ar dimension tale, containing thename o$ the candy ar, its manu$acturer I7, its weight in ounces. #e alsoha!e the candy ar manu$acturer dim tale containing the name o$ themanu$acturer along with its I7. ote that i$ we +ept the column&/7=/9>M9>/M? in the &/7=/9>7IM tale, the name would ha!eeen associated with the &/7=/9>M9>I7 column, which is not part o$the Primary Dey. *his !iolates *hird ormal orm. esides that, the&/7=/9>M9>/M? would ha!e repeated se!eral times, since one

    manu$acturer can produce se!eral types o$ candy ars. or e%ample, theMars corporation produces the *wi% ar, Mil+y#ay ar, Mars ar, or course,and so on, thus Mars would ha!e repeated many times in the&/7=/9>7IM tale. e%t, we ha!e our 9?SPO7?*>7IM tale containingthe 9?SPO7?*>I7 as the Primary Dey, as well as e!erything associateddirectly with it, such as the 9?SPO7?*>/M?, the 9?SPO7?*>/779,and so on. inally, here's our dimension tale related to dates. /s you see,our Primary Dey is the S7/*? column and we ha!e two attriutesrelated to the S7/*? column, S=?/9, which is -ust a $our digityear associated with the S7/*?, and SMO*H, which is thenumer o$ the month, 4 is Ganuary, 6 is eruary, and so on. /t this point,

    we ha!e our &/7=/9>/&* tale, as well as se!eral dimension tales.ow, instead o$ each tale eing isolated, you can -oin them together asedon the +eys. or e%ample, we can -oin together the tales &/7=/9>7IMand &/7=/9>M9>7IM y the &/7=/9>M9>I7 column. *his isrepresented y the line as shown. #e can do a similar thing with the&/7=/9>/&* tale. I$ need e, you can -oin the three dimension tales,7/*?>7IM, &/7=/9>7IM, and 9?SPO7?*>7IM to the &/7=/9>/&*tale. *hese -oins are represented y the three lines pointing $rom thePrimary Deys o$ the dimension tales to the columns that ma+e up thePrimary Dey o$ the $act tale. On this slide let's per$orm a speed test usingthe same "uery on the &/7=/9>HIS*O9I&/L>7/*/ tale and the

    &/7=/9>/&* tale. Here we ha!e a !ery simple sum on the */S*?>9/*IA column, and note that I -ust sumitted the command, S?*

  • 8/19/2019 Optimizing SQL Queries in Oracle

    13/69

     *IMIA O, so that the elapsed time it ta+es the "uery to run will edisplayed in the output, which is always nice to see. Here are the results andthis "uery ran in -ust under 45 seconds. ow, here we ha!e the e"ui!alent"uery on the */S*?>9/*IA column, ut it uses &/7=/9>/&* instead o$&/7=/9>HIS*O9I&/L>7/*/ and the results are naturally the same, ut

    the e%ecution time is aout 6. seconds. *his "uery runs in a "uarter o$ thetime and this is solely due to the record size o$ the tale. *hat is, we'!e gone$rom a $at tale, &/7=/9>HIS*O9I&/L>7/*/ containing 66 columns, to as+inny tale, &/7=/9>/&* with only B columns, and I thin+ you nowunderstand why I mentioned that you shouldnJt -ust -ump into inde%es andpartitions. *his *hird ormal orm stu( is really nice.

    PrimaryEoreign Deys 9?7I7 column, which uni"uely identi)es each one o$ the 65rows o$ data within this tale. *hat is, a &/7=/9>I7 o$ 4 will only occuronce in this tale and only one row will e displayed i$ that &/7=/9>I7 isre"uested, similar $or the remaining 62B rows. /s another e%ample, thetale &/7=/9>/&* needs three columns, the 9?SPO7?*>I7, the&/7=/9>I7, and the S7/*? in order to distinguish each indi!idualrow o$ data uni"uely. *hat is, 9?SPO7?*>I7 o$ 4, &/7=/9>I7 o$ 4, and

    S7/*? o$ March 64, 654 will only result in a single row o$ data eingreturned $rom this tale. *his is true o$ all the respondents, candy ar I7s,and dates. =ou will only get ac+ a single row o$ data. *hese special columnsare called Primary Deys. In the case o$ the &/7=/9>7IM tale, there isonly one column, the &/7=/9>I7 and it is the Primary Dey. In the&/7=/9>/&* tale it ta+es three columns to ma+e the Primary Dey,9?SPO7?*>I7, &/7=/9>I7, and S7/*?. ow, i$ you loo+ at the&/7=/9>7IM tale, you'll see the &/7=/9>M9>I7 column. *hiscolumn is a oreign Dey ecause &/7=/9>M9>I7 is the Primary Dey in itsown tale. &/7=/9>M9>7IM. /s you see here, the column,&/7=/9>M9>I7 is the Primary Dey o$ the tale &/7=>M9>7IM. *a+e

    note that in our dataase design all Primary Deys appear ao!e thehorizontal lines. ?!erything elow this line are called attriutes or non0+eycolumns and are solely related to the Primary +ey. O+ay, so what does thisha!e to do with optimization8 #hen you create your $act and dimensiontales using the create tale statement you speci$y which column orcolumns act as the Primary Dey or oreign Dey, these are +nown asconstraints and Oracle will automatically put an inde% on the Primary Deycolumn allowing you to -oin $act and dimension tales together much"uic+er. Oracle does not automatically inde% the oreign Dey within the $acttale though. /gain, we tal+ aout inde%es later in the course, ut let's ta+ea loo+ at the code used to speci$y Primary and oreign Dey constraints. Let's

    loo+ at how to create a Primary Dey using the &9?/*? */L? Synta%. In thise%ample we're telling the Oracle that the column S7/*? is the

  • 8/19/2019 Optimizing SQL Queries in Oracle

    14/69

    Primary Dey o$ the 7/*?>7IM tale. ollowing the column data type, 7/*? inthis case, you'll speci$y the +eyword &OS*9/I* $ollowed y the name o$the constraint. In this case, I ha!e called it p+>sur!eydate. e%t, we $ollowup with the +eywords P9IM/9= D?=, indicating that this column is the solePrimary Dey $or this tale. /$ter the tale is created you can then insert data

    into it. *his type o$ Primary Dey synta% is called the inline constraint synta%ecause we're using the constraint +eyword on the same line as thede)nition o$ the column. ow, this synta% only allows you to de)ne a singlecolumn as the Primary Dey. On the other hand, you can use the out0o$0lineconstraint synta% in order to indicate that multiple columns act as thePrimary Dey. or e%ample, here we're creating the $act tale,&/7=/9>/&*. ote that a$ter the column 9>/9S>&OSM9>I7 in this tale is actually the Primary Deyin the tale de)ned ao!e, and it is this tale that is speci)ed a$ter there$erences +eyword, so let's go through it in more detail. irst, I speci$y thecolumn &/7=/9>M9>I7 and then I'll immediately $ollow up with the&OS*9/I* +eyword. ote that I do not speci$y the data type and I'll

    e%plain why in -ust a moment. /$ter the &OS*9/I* +eyword I gi!e a nameto the &OS*9/I*. Here it's $+>candyarm$rid. I then $ollow up with the9??9?&?S +eyword $ollowed y the name o$ the tale to re$erence, in thiscase, the tale &/7=M9>7IM as shown ao!e, and $ollowing parentheses Iplace the name o$ the column in the &/7=M9>7IM tale that is thePrimary Dey. It's the data type in this tale that's used as the data type inthe &/7=/9>7IM tale. /s I mentioned on the pre!ious slide, Oracle doesnot automatically place an inde% on the column &/7=/9>M9>I7 in the&/7=/9>7IM tale, unli+e $or Primary Deys. #e tal+ more aout Primaryand oreign +eys in the modules on inde%es later on in the course.

    In$re"uent &olumns

    Let's tal+ aout columns that will e used in$re"uently or not at all. =oualready +now this, ut one way to ma+e your SQL "ueries e%ecute $aster isto include only those columns on the S?L?&* clause that you'll e using inyour analysis. *his may seem o!ious, ut I'!e heard many programmerso!er the years say, well, I may need that column later on. I'm not dismissingthat, ut i$ you don't need a column don't include it on the S?L?&* clause

    and you should proaly a!oid using the asteris+ in $a!or o$ a list o$ desiredcolumns. *his not only ma+es you thin+ aout the columns you really need,

  • 8/19/2019 Optimizing SQL Queries in Oracle

    15/69

    ut helps out anyone who has to modi$y your SQL code later on. ow, i$ you+now you'll ne!er use a particular column, -ust don't load it into thedataase, sa!e the space and your SQL should run a tiny it $aster. On theother hand, you can create a separate tale containing the rele!ant PrimaryDey, as well as these in$re"uently accessed columns, thus the !ast ma-ority

    o$ your analysis will ta+e place on the $act tale containing the $re"uentlyaccessed columns, and i$ you do need the in$re"uently accessed columns,they're there on another tale. or e%ample, i$ our sur!ey data containedcolumns li+e the !ersion o$ the sur!ey so$tware used y the respondent, thetimestamp produced y that so$tware, the location o$ the sur!ey site, thee%piration date o$ each candy ar eaten y the respondent, and so on, thesecolumns may not necessarily e used on a day to day asis $or analysis, utmay e needed $or other reasons later on. *hese columns can e mo!ed outto a separate tale $rom the $act tale +eeping the $act tale s+inny.

  • 8/19/2019 Optimizing SQL Queries in Oracle

    16/69

  • 8/19/2019 Optimizing SQL Queries in Oracle

    17/69

    Let's tal+ aout optimizing on0Goins. irst, e sure to neaten up your SQLcode and I +now you all +now this. e sure to indent your SQL code. o,lining up your code does not ma+e the SQL e%ecute $aster. #ith that said,lining up your code will help you spend less time )guring out where thesu"ueries are, what parenthesis goes with what other parenthesis, and so

    on. I$ it ta+es you 45 minutes to understand a complicated piece o$ SQLcode that's not nice and neat, then you should add those 45 minutes to thee%ecution time o$ your "uery ecause you'!e -ust lown 45 minutes. I'msaying that hal$0-o+ingly o$ course. or e%ample, here's some SQL code and Ithin+ you get my point.

    9estrict &olumnsE9ows

    Let's tal+ aout how to restrict the numer o$ columns and rows you pullac+ $rom the dataase. Speci)cally, let's tal+ aout the S?L?&* and #H?9?clauses.

  • 8/19/2019 Optimizing SQL Queries in Oracle

    18/69

    tales. I*?9S?&* usually outper$orms the corresponding I?9 GOI. Aoingac+ to our e%ample, we could use an I?9 GOI etween the two tales todetermine a distinct list o$ common 9?SPO7?*>I7s, ut this is generallyslower than I*?9S?&*. ote that unli+e an I?9 GOI, I*?9S?&*per$orms its own 7IS*I&* without the need $or the 7IS*I&* +eyword. or

    e%ample, gi!en two tales, 9?SPO7?*S>4 and 9?SPO7?*S>6,containing thousands o$ respondent I7s, here is the I?9 GOI !ersion o$the code to return a distinct list o$ common 9?SPO7?*>I7s. *his "uery,on my system, too+ aout 6. seconds to e%ecute. ow, here is the samecode, ut using the I*?9S?&* +eyword. ote that one complete "ueryappears ao!e the I*?9S?&* operator and another complete "ueryappears elow. *he result set o$ oth "ueries is intersected and a distinct listo$ 9?SPO7?*>I7s is returned. *his "uery completed in 5.651 seconds, oraout 4E45 o$ the time as the I?9 GOI. /nother nice $eature o$ theI*?9S?&* is that you're not limited to two tales. /ll you ha!e to do is addanother I*?9S?&* operator $ollowed y another $ull "uery and so on. *he

    corresponding I?9 GOI code would re"uire an additional tale and amodi)ed O clause that is much more complicated.

    MI

  • 8/19/2019 Optimizing SQL Queries in Oracle

    19/69

    Let's tal+ rie3y aout correlated su"ueries. In a correlated su"uery asu"uery contains one or more columns $rom the outer "uery. *hisnecessitates the dataase per$orm loops as the columns $rom each row inthe outer "uery has to e $ed into the su"uery. &orrelated su"ueriesgenerally per$orm poorly, ut there may e times when you can't a!oidthem. *ry to rewrite the "uery, i$ possile. or e%ample, here's a correlatedsu"uery. /s you can see, the su"uery contains a mention o$/.9?SPO7?*>I7, which is $rom the outer "uery. *his code ta+es a littlemore than seconds to run on my system. Let's complete our tal+ aoutcorrelated su"ueries. Here is the rewritten !ersion o$ the SQL code on thepre!ious slide. *a+e note that I ha!e eliminated the correlated su"uery in$a!or o$ an I?9 GOI and the "uery e%ecutes at aout 6. seconds, oraout hal$ the time, so the ta+eaway $rom this is try to a!oid correlatedsu"ueries i$ you can.

    I !s. ?CIS*S

    Let's tal+ aout the I condition !ersus the ?CIS*S condition. 9ecall that theI condition is e%pecting a comma delimited list o$ !alues or su"uery.

     *hese !alues will e compared to the column appearing to the le$t o$ the Icondition. *he ?CIS*S condition, on the other hand, tests $or the e%istenceo$ at least one row in the su"uery and will return true the moment a row is$ound. ote that the "uery within the ?CIS*S $unction needs to e correlated

    with the outer "uery $or it to e use$ul. In oth cases, when there's a matchin the su"uery the outer "uery uses that data. In general, i$ the inner "ueryis small as compared to the outer "uery, then try to use the I condition. Onthe other hand, i$ the inner "uery is large, as compared to the outer "uery,then try to use the ?CIS*S condition. In one test I did I tried the $ourpermutations trying to compute the numer o$ distinct $emale respondentsusing the &/7=/9>HIS*O9I&/L>7/*/, which is a large tale, !ersus the9?SPO7?*>7IM tale, which is a tiny tale. /s you can see in our chart,our general rule actually doesn't wor+ out. or the I condition the lue!ertical ar is smaller than the red ar indicating that the "uery ran $aster.

     *his means that the ig outer "uery, indicated y the lael O, paired with asmall inner "uery, indicated y the lael SI, ran $aster. or the ?CIS*Scondition a small outer "uery with a ig inner "uery ran $aster.

    Multi0&olumn I !s. /7sEO9s

    Let's tal+ aout the I condition !ersus using /7s and O9s. Many SQLprogrammers use a series o$ /7s and O9s in order to pull data ased onlists o$ !alues, ut there is an Oracle speci)c synta% $or the I condition that

    allows you to pro!ide !ectors and !alues. *his is re$erred to as the multi0column I &ondition and we'll show an e%ample o$ this in -ust a moment.

  • 8/19/2019 Optimizing SQL Queries in Oracle

    20/69

    Aenerally, the multi0column I &ondition per$orms slightly etter than aseries o$ /7s and O9s. or e%ample, here's a typical SQL "uery using aseries o$ /7s and O9s to pull a speci)c list o$ 9?SPO7?*>I7s,&/7=/9>I7s, and S7/*?s. /s you can see, this code iscumersome and di;cult to read. *his "uery runs in -ust under 45 seconds

    on my system. Let's seen an e%ample o$ the multi0column I &ondition. /syou can see, our #H?9? clause starts o( with a comma delimited list o$columns and parentheses. *his -ust lists all the columns we will esusetting y using the multi0column I &ondition. #e ne%t $ollow up withthe I condition and its eginning L?*P/9/M. e%t, we ha!e a series o$comma delimited list o$ !alues, each in its own set o$ params. ?ach list is$ollowed y a comma e%cept $or the last list. #e end the multi0column I&ondition with a right parenthesis, ending the I $unction. ow, this is readli+e this, where 9?SPO7?*>I7 e"uals 2 and &/7=/9>I7 e"uals 15 andS7/*? e"uals 65405046 or 9?SPO7?*>I7 e"uals and&/7=/9>I7 e"uals 15 and S7/*? e"uals 65405046 or and so on

    and so on. *his code runs in B.6 seconds or -ust slowly $aster than thecode on the pre!ious slide. 7espite not running that much $aster, there isanother ene)t o$ using the multi0column I &ondition, and that's how cleanthis synta% appears on the page. #hich would you rather loo+ at, the codeshowing on this slide or the /7s and O9s code on the pre!ious slide8 orme, it's the code on this slide.

     *he #I*H &lause

    In this section let's $ocus on the #I*H clause, etter +nown in the Oracleworld as the Su"uery actoring &lause. ow, $or those o$ you who ha!ene!er seen this e$ore, I discuss it in my Pluralsight lecture, /d!anced SQLQueries in Oracle and SQL Ser!er. Please ta+e the time to !iew that lecturewhen you ha!e a chance. It'll change your li$e. #hat are the ene)ts o$using the #I*H clause8 irst, the #I*H clause neatens up your codeespecially i$ you ha!e !ery large and complicated su"ueries, as we shallsee in -ust a moment. Second, and more important, i$ you ha!e the samerepeated SQL code in su"ueries, the #I*H clause allows Oracle to e%ecutethe #I*H clause once and use those results multiple times without

    rerunning the su"uery. I$ you do not put repeated su"ueries in a #I*Hclause, then Oracle may e%ecute the su"uery o!er and o!er again, urningdaylight. or e%ample, here's a large "uery that does not use the #I*Hclause. /s you can see, the su"uery pulling the male 9?SPO7?*>I7s,displayed in old $ont, is repeated se!eral times. *his "uery runs at a littlemore than 4 minutes. Let's conclude our tal+ aout the #I*H clause. In thisSQL code I ha!e pulled out the repeated su"uery and placed it in a single#I*H clause called !mM/L?9?SP. I'!e had access !mM/L?9?SP where theoriginal "uery was, shown in red now. elie!e it or not, this "uery runs inaout 22 seconds, as compared to 4 minutes $or the SQL code on thepre!ious slide. I didn't actually elie!e this, so I shutdown and restarted my

    Oracle instance and ran this code again, with a similar runtime, so the

  • 8/19/2019 Optimizing SQL Queries in Oracle

    21/69

    ta+eaway $rom this, use the #I*H clause i$ you ha!e a lot o$ repeatedsu"ueries, especially large ones.

     *he /PP?7 Hint

    Let's tal+ aout the /PP?7 Hint to speed up IS?9*s into a tale. ow,when the little rows $rom your tale Oracle doesn't collapse the tale tos"ueeze out those lan+ rows, ut when you insert new data into that taleOracle will attempt to )nd those deleted rows and )ll them in with the newrows eing inserted. /s you can imagine, this does ta+e time. ow, in orderto speed up IS?9*s you can use the /PP?7 Hint, a dataase hint tellsOracle to per$orm some action ased on in$ormation it may not +now, utyou do. or e%ample, using the /PP?7 Hint as shown, a$ter the IS?9*+eyword, will pre!ent Oracle $rom searching $or the deleted rows, utinstead -ust slap the new rows o$ data to the end o$ the tale. ow, this mayloo+ li+e an Oracle comment, ut the addition o$ the plus sign a$ter the )rstasteris+ indicates to Oracle that what's to $ollow is an Oracle hint. Ma+e surethough, that you spell this correctly otherwise, Oracle will ignore it withoutso much as a worry. y using the /PP?7 Hint you may signi)cantly reduceyour o!erall insertion times. or e%ample, let's insert one million rows intothe tale &/7=HIS*>4. Here we are not using the /PP?7 Hint. *his too+

     -ust under 6B seconds on my machine. ow, here is the same insertstatement, ut I'!e added the /PP?7 Hint a$ter the IS?9* +eyword. *histoo+ -ust under B seconds on my machine. ote that I do suggest you use

    the /PP?7 Hint e!en i$ the tale has not had 7?L?*?s per$ormed there,since I'!e seen insertion times greatly reduced in this case as well.

    O &lause !s. #H?9? &lause

    Let's tal+ aout how to optimize SQL "ueries in!ol!ing -oins. In this sectionlet's tal+ rie3y aout the O &lause !ersus the #H?9? &lause. ow, thereis no signi)cant reduction in runtime o$ one o!er the other, ut please useO &lause to indicate -oin criteria and use the #H?9? &lause to indicate

    tale susetting criteria. /gain, there is no signi)cant runtime sa!ingsetween the two, ut you will at least gi!e Oracle a )ghting chance tooptimize your "uery when you use the O and #H?9? clauses properly. ore%ample, here is an I?9 GOI in the old style speci$ying the GOI criteriawithin the #H?9? clause. ote that the tales ha!e a comma etween theminstead o$ the +eywords I?9 GOI or L?* GOI or 9IAH* GOI, and so on.Here is the more modern !ersion o$ the same "uery with the GOI criterianeatly speci)ed on the O &lause and the susetting criteria neatlyspeci)ed on the #H?9? &lause and. as I'!e mentioned e$ore, i$ it ta+es youlonger to understand the old style "uery !ersus the more modern style, thenyou should add that to your runtime ecause you -ust lew that e%tra time.

  • 8/19/2019 Optimizing SQL Queries in Oracle

    22/69

    Order o$ *ales on 9OM &lause

    In this section let's $ocus on the order o$ the tales appearing on the 9OM&lause. *he Oracle documentation recommends that you place tales with

    the $ewest rows )rst, $ollowed y the ne%t $ewest, and so on whenper$orming GOIS. *he numer o$ rows though, is ased on the susettingcriteria eing applied to it, and not -ust the total numer o$ rows in eachtale indi!idually. ased on my test, placing the smaller tale )rst on the9OM &lause signi)cantly reduces down the runtime. or e%ample, here is asimple &9?/*? */L? statement -oining the &/7=/9>HIS*O9I&/L>7/*/tale against the tale 9?SP>SI7s and, as you see, this runs in -ust under 64 seconds on mysystem. Here we ha!e the same code, ut the smaller tale, 9?SP>S

  • 8/19/2019 Optimizing SQL Queries in Oracle

    23/69

    inner "uery is smallest compared to the outer "uery, try the I condition. I$the inner "uery is large, as compared to the outer "uery, try the ?CIS*Scondition. #e saw some modest gains when using the Multi0&olumn I&ondition !ersus the traditional /7s and O9s construct, not only in termso$ runtime, ut in readaility.

  • 8/19/2019 Optimizing SQL Queries in Oracle

    24/69

    tal+ a little it aout how inde%es and partitions interact. #e then show youa simple e%ample in!ol!ing inde%es and then we end on a summary.

    #hat is an Inde%8

    #hat is an inde%8 In this section we go o!er the asics o$ inde%es. =ou canthin+ o$ an inde% as similar to the inde% in the ac+ o$ a oo+. =ou dorememer what a oo+ is8 I$ you want to locate in$ormation "uic+ly within aoo+ you can either scan e!ery single page se"uentially one a$ter the otherto locate the desired in$ormation, which, as you can imagine, would ta+esome time or you can "uic+ly loo+ it up in the inde% and then 3ip directly tothe appropriate page, which is much much $aster. *ell me more I can hearyou say. Inde%es allow you to potentially a!oid $ull tale scans. *hat is, your"uery a!oids !isiting e!ery single row in your tale. Inde%es allow you to

     -ump to your desired row or rows -ust li+e the inde% at the ac+ o$ the oo+.Inde%es may also help speed up -oins etween tales. Inde%es can alsospeed up &O

  • 8/19/2019 Optimizing SQL Queries in Oracle

    25/69

    corresponding inde%es as well. inally, o!er the years hard dri!e dis+s ha!edecreased their access time signi)cantly. or e%ample, your laptop harddri!e proaly spins at 255 or 1655 9PMs, whereas the hard dri!es in thedata center at wor+ spin at 4,555 9PMs. 9ecently, Solid0State dri!es ha!ecome on the scene decreasing access times e!en more. /s dri!es ecome

    $aster at accessing data the need $or inde%es to suset your talesdecreases and this is not hypothetical. I'!e een in companies where someinde%es were dropped ecause the comination o$ $aster hard dri!es, alongwith an appropriate partitioning scheme, allowed $ull tale scans to occur$aster than when using an inde%. *his doesn't mean though that inde%es willgo the way o$ the dinosaur, since they ha!e other attriutes, as mentionedon the pre!ious slide.

    0*ree Inde%es

    Let's tal+ aout some o$ the !arieties o$ inde%es a!ailale to you. In thissection let's tal+ aout the 0*ree inde%. *he 0*ree inde% is a generalpurpose inde% that can e used in a !ariety o$ columns, whether they arenumeric, date or character. I call this type o$ inde% the 121 o$ inde%esecause it is the most general wor+horse inde% a!ailale. 0*ree inde%es cane thought o$ as an upside down representation o$ a tree whose lea$ nodescontain the row I7s, which point to the rows within the tale itsel$. Let's seewhat a 0*ree inde% loo+s li+e, at least conceptually, when placed on the&/7=/9>I7 column. 9ecall that there are 65 di(erent candy ars

    a!ailale in the &/7=/9>/&* tale. Here's the top o$ our 0*ree, the&/7=/9>I7 Inde%. ow, our )rst node represents &/7=/9>I7 numer4. Hanging o( o$ this node is a series o$ lea!es, each indicating a speci)crow numer or 9O#I7 in the tale. *hat is, these lea!es list all those rowsassociated with &/7=/9>I7 numer 4. Mo!ing on, here is the same thing$or &/7=/9>I7 numer 6. /s you can see, the lea!es ha!e di(erent rownumers associated with them ecause each row in the tale can only eassociated with one candy ar, and so on. Here is &/7=/9>I7 numer62B, and )nally, &/7=/9>I7 numer 65, the last candy ar. #hathappens when your SQL "uery has where &/7=/9>I7F4 in it8 /ssumingthat you'll e pulling ac+ aout 4 or less o$ the data in the tale, ased

    on the #H?9? &lause where &/7=/9>I7F4, Oracle will consider using theinde% to gather the row numers associated with the &/7=/9>I7 numer4, and then "uic+ly pull only those rows $rom the tale. *his can e muchmuch "uic+er than doing the $ull tale scan. Let's continue our tal+ aoutthe 0*ree inde%. In this e%ample let's see what a 0*ree inde% would loo+li+e, conceptually, i$ we inde%ed oth the 9?SPO7?*>I7 and the&/7=/9>I7 columns together. *hat is, as a composite inde% rather thantwo separate inde%es. Here we ha!e the root o$ our composite inde% and $or9?SPO7?*>I7 numer 4 we ha!e all o$ the rele!ant &/7=/9>I7s alongwith the appropriate row numers. or 9?SPO7?*>I7 numer 6 we ha!eall o$ the rele!ant &/7=/9>I7s along with the appropriate row numers,

    and so on and so $orth $or all o$ the respondents. /s you can see, the inde%is small as compared to the entire tale, so when searching $or say,

  • 8/19/2019 Optimizing SQL Queries in Oracle

    26/69

    9?SPO7?*>I7F6 or &/7=/9>I7F62B, the dataase can spin throughthe inde% much $aster, gathering together the 9O#I7s and then pulling thedata $rom the tale.

    itmap Inde%es

    Let's tal+ aout some more !arieties o$ inde%es a!ailale to the SQLprogrammer. In this section let's tal+ aout the itmap inde%. / itmapinde% is similar to a 0*ree inde% in $unctionality, e%cept a itmap inde% isstored di(erently than a 0*ree inde%. &onceptually, a itmap inde% uses acontinuous series o$ 4s and 5s where a 4 indicates row inclusion and the 5indicates row e%clusion with one itmap $or each !alue in the column einginde%ed. itmap inde%es are usually placed on a column whose total numero$ distinct !alues is much less than the total numer o$ rows on the taleitsel$. or e%ample, 9?SPO7?*>A?7?9 has two distinct !alues, ascompared to the )!e million rows in our $act tale. *wo, is o!iously muchless than )!e million. itmap inde%es are $re"uently used in datawarehouses where the tales are not modi)ed that o$ten. /s an e%ample,let's see how Oracle would, at least conceptually, create a itmap inde% onthe &/7=/9>I7 column. 9ecall that there are 65 distinct I7s in the&/7=/9>I7 column, while the &/7=/9>/&* tale has million rows,so 65 is much less than million, so you are -usti)ed in using a itmapinde%. Here we ha!e the top o$ the itmap inde% tree. or &/7=/9>I7 1we ha!e a list o$ 5s and 4s, million 5s and 4s to e e%act. ow, where!er

    you see a 4 it indicates that that particular row has &/7=/9>I7 set to 1.otice that the )rst two its in the itmap are set to 4. *his means that othrow 4, the )rst it, and row 6, the second it, ha!e &/7=/9>I7F1.&ontinuing on, row and row 2 are set to 5 indicating that those two rowsdo not ha!e &/7=/9>IF1. /gain, these 4s and 5s are indicators o$ whichrows are or are not a part o$ the &/7=/9>I7F1 data. &ontinuing on, $or a&/7=/9>I7 that was we ha!e another series o$ 4s and 5s and again,where!er there's a 4 it indicates that that row contains &/7=/9>I7 ,and so on all the way up to &/7=/9>I7 65. ow, some o$ you may ethin+ing that storing million its $or each o$ the 65 candy ars will ta+eup a lot o$ space, ut million its is 6,555 ytes, or aout 45D. or all

    o$ those 65 &/7=/9>I7s we'll add 42BM total and this gi!es you anindication as to why Oracle recommends the total numer o$ distinct column!alues e much less than the total numer o$ rows or the storage space wille !ery large indeed. inally, i$ your #H?9? &lause contains se!eralcolumns eing /7ed or O9ed together, the use o$ itmap inde%es canma+e the susetting !ery $ast, since Oracle can per$orm inary /7s orinary O9s etween the itmaps. *his is !ery $ast, as compared to a similarmaneu!er when using 0*ree inde%es. It's ecause o$ this that Oracle'sdocumentation states, Ritmap inde%es are primarily designed $or datawarehousing or en!ironments in which "ueries re$erence many columns inan ad hoc $ashion.R

  • 8/19/2019 Optimizing SQL Queries in Oracle

    27/69

    unction0ased Inde%es

    Let's tal+ aout some more !arieties o$ inde%es a!ailale to the SQLprogrammer. In this section let's tal+ aout unction0ased inde%es. Suppose

    you went through a lot o$ troule de)ning inde%es on the column say,&/7=/9>/M?. I$ your SQL code $re"uently uses the /M?, the inde% will e s+ipped. *his is due tothe $act that when the inde% is created the actual !alues are stored in theinde% and not the uppercased !alues. *he solution is to create an inde% thatta+es into account the $unction that you will e using say, the

  • 8/19/2019 Optimizing SQL Queries in Oracle

    28/69

    on the #H?9? &lause. or e%ample, we can create a itmap -oin inde%etween the &/7=/9>/&* and 9?SPO7?*>7IM tales -oining y the9?SPO7?*>I7, ut our #H?9? &lause will use the 9?SPO7?*>S*/*?column in the 9?SPO7?*>7IM tale as a susetting criteria. /s you see inthe graphic, $or each state there is a itmap associated with the

    &/7=/9>/&* tales 9?SPO7?*>I7, as well as a itmap associatedwith the 9?SPO7?*>7IMs 9?SPO7?*>I7. #hen re"uesting #H?9?9?SPO7?*>S*/*? e"uals /L, Oracle can use oth sets o$ row I7s withoutha!ing to -oin the tales when the "uery is e%ecuted. Since you area(ecti!ely storing the resulting -oins, "ueries that ta+e ad!antage o$ itmap

     -oin inde%es can e !ery $ast.

    Aathering Statistics

    Let's tal+ aout the importance o$ gathering statistics on your inde%es. /swe'!e mentioned earlier, Oracle's Query Optimizer uses optimizer statisticsto decide the est e%ecution plan $or your SQL "uery. Let's tal+ more aoutthese statistics. /$ter you go through all the troule o$ creating inde%es youmust gather statistics on the inde%es otherwise, Oracle's Query Optimizerwon't consider them and you would ha!e wasted your time and space in thedataase. *hese statistics help Oracle create the est e%ecution plan $romyour SQL "uery, so i$ Oracle thin+s an inde% is the way to go, Oracle will usethe inde%. I$ Oracle doesn't thin+ an inde% is the way to go, Oracle willignore the inde%, ut i$ Oracle has not optimizer statistics aout the inde%es

    in your tale, then Oracle won't e ale to determine whether using aninde% is the way to go or not. Once you gather stats on the inde%es on yourtale you should see a mar+ed impro!ement in your SQL "ueries. aturally,this usually, although not always, goes ac+ to the #H?9? &lause. ow, inorder to gather statistics you use the procedures pro!ided in the Oracle7MS>S*/*S pac+age. #e'll see an e%ample o$ this in -ust a moment. Pleasedon't use the older //L=@? command. It gathers in$erior statistics ascompared to 7MS>S*/*S causing Oracle's optimizer to create a less thanoptimal e%ecution plan. ote that since !ersion 45g, Oracle automaticallygathers statistics when you create or reuild an inde%, ut this is only true i$the tale is not empty. /lso, since !ersion 45g, Oracle has an automated

    tas+ that runs nightly to gather updated optimizer statistics i$ your tale haschanged signi)cantly, rendering the current statistics stale. Since I don't+now what !ersion o$ Oracle you're using, I will show you how to use the7MS>S*/*S pac+age -ust in case and we'll show an e%ample o$ how togather statistics later on in the module.

    Inde%EPartition Interaction

    Let's tal+ rie3y aout inde%es and partitions. *here is an interactionetween inde%es and partitions. Partitions can e thought o$ as ta+ing a!ery large tale and slicing it up into smaller tales. or e%ample, we could

  • 8/19/2019 Optimizing SQL Queries in Oracle

    29/69

    rea+ up our &/7=/9>/&* tale into separate tales y say, sur!eyyear, gi!ing us 45 smaller tales, one $or each year. I$ your "ueries pull data$rom within a speci)c year, you would use only that speci)c tale. In Oraclepartitions are similar to this, ut are managed y the dataase, so you don'tactually ha!e to rea+ up the tale into pieces although, you do ha!e to

    speci$y what these pieces are when you code your &9?/*? */L?statement. I$ you partition your tale y say, sur!ey year, you can stillcreate inde%es on one or more o$ the columns. In one case, you can createinde%es within each partition. *hese inde%es are called Local PartitionedInde%es ecause they are local, or within, each indi!idual partition. =ou canthin+ aout this again, as i$ you had 45 separate tales, one $or each year.?ach one o$ those tales could e gi!en a separate inde% on it. LocalPartitioned Inde%es are similar to that. On the other hand, when usingpartitions you can create inde%es that span across all the other partitions,these are called Aloal on0Partitioned Inde%es. inally, we can createinde%es that ha!e a di(erent partitioning scheme to that o$ the underlying

    tale. *hese types o$ inde%es are called Aloal Partitioned Inde%es. #e tal+more aout the interaction etween inde%es and partitions in the lastmodule in the course.

    ?%ample

    Let's see a asic e%ample o$ how to create inde%es on two tales, gatherstatistics on those tales, as well as see i$ the inde% ma+es a di(erence in

    the runtimes o$ our SQL "uery. In this e%ample we want to pull data $rom the&/7=/9>/&* tale y inner -oining against the small tale containingse!eral 9?SPO7?*>I7s. ow, I ran this e%ample three times, once withonly 45 9?SPO7?*>I7s, again with 455, and then again with 4,555. Hereis the code to create a tale called 9?SP>SI7s and I changed this $or the run o$ 455 respondents andthen again, $or the 4,555 respondents. Here is a simple I?9 GOI etweenthe &/7=/9>/&* and the respondent suset tale. Here are the resultso$ the three runs without any inde%es. /s you can see, the runtimes ma+esense and range $rom o!er 6 seconds to o!er seconds, ut can we doetter8 ow, in the code that $ollows we create inde%es on the

    9?SPO7?*>I7 column and gather statistics e$ore running the I?9 GOIcode $rom the pre!ious slide. irst, let's create an inde% on the9?SPO7?*>I7 column o$ the 9?SP>S $ollowed y a shortened !ersion o$ thetale name, $ollowed y an underscore, and $ollowed y a shortened !ersiono$ the column or columns I'm inde%ing. Here my inde% is namedIC>9?SPS9?SPI7. *hen, you $ollow up with the +eyword O, $ollowedy the name o$ the tale, a le$t perim, the name o$ the column you want toinde%, $ollowed y the closing param. Once this &9?/*? I7?C code runs,your inde% has een created. e%t, let's gather stats on the inde%. Since we

    are running the procedure, A/*H?9>*/L?>S*/*S within the 7MS>S*/*Spac+age, we start o( with the e%act +eyword $ollowed y a lan+. e%t,

  • 8/19/2019 Optimizing SQL Queries in Oracle

    30/69

    7MS>S*/*S.A/*H?9>*/L?>S*/*S and a parentheses, the name o$ theschema, called the owner here, the name o$ the tale, 9?SP>SSI@? constant located within the

    7MS>S*/*S pac+age. ote that attempting to gather stats on 455 o$ thetale could ta+e "uite a while i$ the tale is !ery large. Oracle recommendsusing the /S/MPL?>SI@? constant as shown. Once this e%ecutes, thestatistics $or the tale 9?SP>SI7 o$ the &/7=/9>/&* taleand gather statistics on the tale &/7=/9>/&*. inally, we can run ourcode. /gain, note that I ran this with 45, 455, and 4,555 9?SPO7?*>I7s inthe 9?SP>S*/L?>S*/*S procedure. #e saw that you can get a reductionin the runtime o$ your SQL "uery when using inde%es appropriately. e%t, wetal+ aout how to use 0*ree inde%es in more detail.

    0*ree Inde%es

    Introduction

  • 8/19/2019 Optimizing SQL Queries in Oracle

    31/69

    Hello and welcome ac+ to the Pluralsight course, Optimizing SQL Queries inOracle. My name is Scott Hecht and in this module I'd li+e to go o!er the 0

     *ree inde% in a little more detail than what was presented in the o!er!iewmodule, so let's get started.

    Module &ontents

    irst though, let's go o!er the module contents. #e'll tal+ aout what a 0 *ree Inde% is. #e'll then tal+ aout when you should use a 0*ree Inde%.e%t, we'll show you how to create a 0*ree Inde% on one or more columns.#e'll then show you how to list all the inde%es on a tale, as well as thecolumns that are inde%ed. e%t, in the e!ent that you don't want the inde%

    anymore, we'll show you how to delete the inde%. /s mentioned in thepre!ious section, gathering statistics on your inde%es is a !ital stepotherwise, Oracle's Query Optimizer may not consider your inde%es whencreating an e%ecution plan. In this section we show you how to gatherstatistics on the inde%es on your tale. #ith all o$ this tal+ aout inde%es,how do you +now Oracle is using the inde% when it runs your "uery8 In thissection we gi!e a high le!el o!er!iew o$ auto trace and e%pand plan, whichare two ways to determine i$ Oracle is using your inde%es. inally, we endwith a summary.

    #hat is a 0*ree Inde%8

    #hat is a 0*ree Inde%8 9ecall that we tal+ed a little it aout 0*ree Inde%esin the o!er!iew. *here, we said that the 0*ree inde% is a general purposeinde% and can e placed on numeric, character, and date data types. *hisma+es the 0*ree inde% a !ery good general purpose inde% and is the inde%a!ailale in most, i$ not all, the dataases a!ailale out there, such as SQLSer!er, SQLite, MySQL, and so on. Let's go o!er the &/7=/9>I7 columne%ample. 9ecall that a 0*ree inde% is li+e an in!erted tree. Here we ha!e a

    root node indicating the top o$ our inde% on the &/7=/9>I7 column. e%t,all the 9O#I7s $or each !alue o$ the &/7=/9>I7 are gathered and placedin the inde%. #hy is this use$ul8 #ell, the amount o$ space used up y theinde%, at least in this case, is much smaller than the total space ta+en up ysay, &/7=/9>/&*, also, the row length is smaller since we are retaining$ewer pieces o$ in$ormation. *hus, the hard dis+ readEwrite armature canspin through the inde% much $aster than the entire tale itsel$. 9ecall that inour asic e%ample in the pre!ious module we placed an inde% on a singlecolumn, the 9?SPO7?*>I7, ut you're not limited to inde%ing a singlecolumn. =ou can place an inde% on two or more columns. /n inde%, on asingle column, is called a simple inde%, whereas an inde% on two or more

    columns together is called a composite inde%. 9ememer that you shouldloo+ at your #H?9? clauses in order to see which columns are eing

  • 8/19/2019 Optimizing SQL Queries in Oracle

    32/69

    re$erenced. I$ your #H?9? &lause only re$erences a single column, then asimple inde% on that column should e considered. I$ your #H?9? &lausesre$erence two columns, then a composite inde% on those two columnsshould e considered, and so on. 9ecall $rom the o!er!iew that we showedwhat a composite 0*ree inde% loo+s li+e on the two columns,

    9?SPO7?*>I7 and &/7=/9>I7. /s you see, each 9?SPO7?*>I7 islisted, as well as each &/7=/9>I7 within each 9?SPO7?*>I7, and thelea!es are o$ course, the 9O#I7s associated with each speci)c9?SPO7?*>I7, &/7=/9>I7 pair.

    #hen do you use a 0*ree Inde%8

    Let's tal+ aout when to use a 0*ree Inde%. Let's tal+ in a little it moredetail aout 0*ree Inde%es, namely, when do you use them8 /ll inde%es are,

     GOIs aside, twinned with your SQL #H?9? &lauses. #hat does that mean8#hen you create a tale or tales you most li+ely +now what SQL "ueriesyou will e per$orming on these tales. #hat you should do is gathertogether all o$ the word clauses you are li+ely to issue $or the tale anddetermine i$ there are some common columns etween them. or e%ample,we may need to suset our &/7=/9>/&* tale y say, theLID?LIHOO7>PP

  • 8/19/2019 Optimizing SQL Queries in Oracle

    33/69

    you a clue as to what inde%es to create on what columns. I$ you create acomposite inde% on columns /, , and & you don't need to then create acomposite inde% on /, and you don't need to create a simple inde% oncolumn /. Oracle will still ma+e use o$ the composite inde% on columns /, ,and & when you re$erence columns / and , in a #H?9? &lause, or -ust

    column / in a #H?9? &lause. ote that columns /, as well as columns /and , are called leading columns. ow, Oracle's optimizer may also use thecomposite inde% on columns /, , and & when your #H?9? clausere$erences and & or -ust &. *hat is, Oracle may use the composite inde% oncolumns other than the leading columns. I'!e heard them read o!er theyears a !ariety o$ opinions as to the order o$ the columns in a compositeinde%. ow, some gurus say that you should place the column with thesmallest numer o$ distinct !alues )rst $ollowed y the ne%t most distinct,and so on. Other gurus say that the order doesn't matter, and still, othergurus say that it matters i$ your SQL "uery is accessing a range o$ !alues,such as where O:?9/LL>9/*IA is greater than or e"ual to se!en, and order

    doesn't matter i$ you're accessing a speci)c !alue, such as whereO:?9/LL>9/*IA e"uals )!e. #ith all o$ that said, my ta+e on this is thatyou should order your columns in the inde% such that you co!er the most#H?9? &lause predicates you can. *hat is, gi!en the composite inde% oncolumns /, , and & I can get three #H?9? &lause predicates out o$ it $or$ree, /, , and &, as well as /, , and / due to them eing the leadingcolumns. inally, 0*ree inde%es are considered $or use y the OracleOptimizer when used with the "uality and range conditions. #hen using thenot e"uals or is not conditions 0*ree inde%es won't e considered. *o put itanother way, inde%es are used to )nd data that's there and not data thatisn't there.

    &reating a 0*ree Inde%

    Let's tal+ aout how to create an inde%. Let's ta+e a closer loo+ at the&9?/*? I7?C Synta% speci)cally $or the 0*ree inde%. 9ecall $rom our asice%ample that we used a &9?/*? I7?C synta% to create an inde% on the9?SPO7?*>I7. *his asic synta% creates a 0*ree inde% y de$ault andplaces the inde% itsel$ in the de$ault talespace associated with your

    schema. *his talespace is setup y the dataase administrator and wasassociated with your schema when it was created. /s we'!e seen, youcreate an inde% y starting with the &9?/*? I7?C +eywords. In the chartyou can also speci$y the

  • 8/19/2019 Optimizing SQL Queries in Oracle

    34/69

    happen. 9ecall that i$ you speci$y the Primary Dey constraint on9?SPO7?*>I7 in the 9?SPO7?*>7IM tale, then a uni"ue 0*ree inde%is created $or you and you don't ha!e to use the &9?/*? I7?C synta%.e%t, you $ollow up with the name o$ the inde%. /s I'!e mentioned, I usuallyname my inde%es IC> $ollowed y an are!iation o$ the tale name,

    $ollowed y an underscore, $ollowed y an are!iation o$ the column orcolumns I'm inde%ing. ote that you are limited to 5 characters $or thename o$ an inde%. e%t, you $ollow with the O +eyword. /$ter that you$ollow with the tale>inde%>clause shown on your screen. Here you pro!idethe name o$ the tale you're going to inde% $ollowed y a le$t parenthesis, alist o$ one or more comma delimited columns to e inde%ed, $ollowed y theending right >>>>>paren, and a semi colon ending the &9?/*? I7?C synta%.ow, on the ne%t slide I would li+e to show you what the inde%>propertiesloo+ li+e. Let's )nish loo+ing at the synta%. Shown is the inde%>properties.#e tal+ aout local and gloal partition inde%es later on in the course, solet's concentrate on the inde%>attriutes part. Here are the inde%>attriutes.

    #e'll concentrate on two attriutes, the )rst is the talespace attriute. yde$ault, the inde% is created in your own schema, as I ha!e mentioned, utas I ha!e mentioned earlier, you can place your inde% in another talespace,i$ need e, y using the talespace +eyword $ollowed y the name o$ thetalespace. *he ne%t use$ul attriute we'll loo+ at is the parallel>clause. /syou +now, today's super0duper power$ul computers can contain manypower$ul processers. /ll this means is that more programs can e run inparallel allowing $or $aster runtimes. *his parallelism can e per$ormed ythe &9?/*? I7?C command i$ the parallel>clause is included in the synta%.y de$ault, &9?/*? I7?C is not per$ormed in parallel. /s you can see in thesynta%, you can speci$y an integer a$ter the parallel +eyword. ?ither lea!e

    this o( or contact your dataase administrator $or ad!ice on this !alue. #e'llsee an e%ample o$ this in -ust a it. Let's see some e%amples. /t its simplest,we can create a 0*ree inde% on a single column. Here we're creating aninde% named I7?C>9?SP7IM>9?SPI7 on the 9?SPO7?*>I7 columnwithin the 9?SPO7?*>7IM tale. *a+e note that the column you areinde%ing appears in the parentheses to the right o$ the tale name. On theother hand, since we +now that the 9?SPO7?*>I7 is uni"ue across the9?SPO7?*>7IM tale, we can include the

  • 8/19/2019 Optimizing SQL Queries in Oracle

    35/69

    Listing the Inde%es on a *ale

    7uring the course o$ creating tales and inde%es you will occasionally $orgetthe name o$ the inde%es you created or when starting a new -o as a SQL

    programmer you'd li+e to +now the inde%es that are on some o$ the talesyou'll e using. In this section we'll loo+ at some o$ Oracle's data dictionary!iews, which you can use to list your tales, inde%es, as well as whatcolumns are inde%ed. Let's )rst remind you o$ the dictionary !iews,/LL>*/L?S and */L?S. *he )rst is the !iew /LL>*/L?S, whichlists all o$ the tales accessile to you. #hen using this !iew you normallysuset y selecting where the owner column is your schema name. On theother hand, you can use the !iew, */L?S and s+ip the ownersusetting criteria. or e%ample, here is a SQL "uery that lists all the talesin my own S&O** schema. /s you see, I am susetting #H?9? O#?9FS&O** in tic+ mar+s, which will return my own tales. In this "uery I'm

    pulling ac+ the O#?9, */L?>/M?, and */L?SP/&?>/M? columns,although there are many many more columns in oth /LL>*/L?S and*/L?S. Here are the results. =ou will no dout recognize those tales.?"ui!alently, you can use the */L?S !iew and s+ip the #H?9?clause. *his SQL code returns the same list o$ tales as the SQL code ao!e.Let's tal+ aout the dictionary !iews, /LL>*/>&OL&OL*/L?S -ust lists the tales, you can listthe names o$ the columns associated with those tales along with their datatype and nullaility y "uerying /LL>*/>&OL&OL*/L? statement. /s you see, you aregi!en the column name, the data type, the length o$ the data type, thenullale column, which is = i$ the column can contain /M?, */L?>/M?,and */L?SP/&?>/M?. *he results are as $ollows. /s you can see, we ha!ean inde% called I7?C>&/7=/9>/&*>9I7&I7S7* on the tale&/7=/9>/&*, shown in red, among others. Here is the e"ui!alent codeusing the I7?C?S dictionary !iew and s+ipping the #H?9? clause.On this slide let's concentrate on /LL>I7>&OL&OL

  • 8/19/2019 Optimizing SQL Queries in Oracle

    36/69

     */L?>/M?, I7?C>/M?, and &OLPOSI*IO. It is the&OLPOSI*IO column which allows you to determine the order o$ thecolumns as they appear within the parentheses o$ the &9?/*? I7?Csynta%. Here are the results. or the &/7=/9>/&* tale we ha!e theinde% I7?C>&/7=/9>/&*9I7&I7S7*, which was created y speci$ying

    the columns 9?SPO7?*>I7, &/7=/9>I7, and S7/*? in thatorder, and are displayed across three lines in the output. inally, here's thee"ui!alent code using the I7>&OLS*/*S procedure, A/*H?9>*/L?>S*/*S,and we showed an e%ample o$ how to use it. In this section we'll go into alittle more detail on this particular procedure. ow, you can )nd thein$ormation presented in this section in the Oracle 7ataase manualentitled, PLESQL Pac+ages and *ypes 9e$erence. 9ememer to download theP7 )le o$ the !ersion, as well as the release $or the Oracle dataase youare using. ow, there are se!eral procedures a!ailale in the 7MS>S*/*Spac+age, most o$ which only a dataase administrator would use, ut thereare two you should e aware o$. A/*H?9>S&H?M/>S*/*S will gatherstatistics on all the tales in speci)c schema. #e won't go into this

    procedure, ut I -ust wanted you to e aware o$ it. *heA/*H?9>*/L?>S*/*S procedure gathers statistics on a speci)c tale. orthe tale itsel$, these procedures gather statistics such as, numer o$ rowson the a!erage row length, $or the columns within the tale, the numer o$distinct !alues, the numer o$ S*/*IS*I&S, /LL or */>&OL>S*/*IS*I&S, and /LL or I7>S*/*IS*I&S. Here is the synta%

  • 8/19/2019 Optimizing SQL Queries in Oracle

    37/69

    $or this procedure. ow, most o$ these parameters ha!e reasonalede$aults, so you'll e ignoring most o$ them, and really only need to speci$ythe )rst two. ote that you must use the e"ual sign, greater than symolnotation i$ you s+ip o!er one or more parameters listed. #e showed thisnotation in the e%amples earlier. ow, there are some parameters I do want

    to point out though. ownname is the name o$ the schema where the tale islocated. or my test dataase the owner is S&O**. or you, it should e thename you use when logging into the dataase. *his entry, y the way,should e in uppercase. taname is the name o$ the tale, in uppercase,that you want to gather stats on, partname is the name o$ the partition youwant to gather stats on, estimate>percent is the percentage o$ the tale youwant Oracle to spin through in order to gather statistics. /s e%plained in theo!er!iew, use the constant, 7MS>S*/*S./S/MPL?>SI@? to allowOracle to decide how much o$ the tale to spin through. ow, in Oracle!ersions B and 45 the statistics gathered with /S/MPL?>SI@? weresuoptimal, ut this has een impro!ed upon in !ersions 44 and on and this

    is proaly the way to go. *he degree parameter tells Oracle i$ you want togather the stats in P/9?L?LL. y de$ault, Oracle does not gather stats inP/9?LL?L, ut you can let Oracle decide y speci$ying the constant,7MS>S*/*S.7?/7?A9??. /gain, you may want to ha!e acon!ersation with your dataase administrator aout this particularparameter. inally, the cascade parameter determines i$ gathering statisticsis necessary $or all o$ the inde%es o$ the tale. Prior to 45g this was set to$alse, indicating no inde% stats are gathered, ut $rom 45g on $orward thisparameter is set to true y de$ault. */L?>S*/*S, let's see some e%amples. irst, let's issue a "uerythat )nds the a!erage O:?9/LL>9/*IA -ust $or the 9?SPO7?*>I7

    numer 4. ote that the tale &/7=/9>HIS*O9I&/L>7/*/ does not ha!ean inde% on the 9?SPO7?*>I7 column and we'll ha!e to spin through theentire tale. /s you can see, this "uery too+ under 44 seconds to run on mysystem. ow, let's create an inde% on the 9?SPO7?*>I7 column andgather statistics on the tale and columns using the /S/MPL?>SI@?constant. *his will let Oracle decide how much o$ the tale to spin throughto gather a reasonale set o$ statistics. /s you see, the creation o$ the inde%too+ 6ish seconds and the A/*H?9>*/L?>S*/*S procedure too+ aout seconds to run. ow, let's rerun the "uery at the top o$ the slide and, as youcan see, it now runs in 5.5 seconds, ut can we do etter8 Let's continueour e%ample. *his time let's remo!e the /S/MPL?>SI@? constant and$orce in 455 to indicate that Oracle should scan the entire tale, all millionrows o$ it, in order to gather the est possile statistics. /s you can see, thisprocedure ran in 6 minutes and 4 seconds, which is consideraly longerthan the 2 seconds $or the /S/MPL?>SI@?. 9eissuing that "uery now,you can see it did run $aster at 5.55 !ersus 5.5. e%t, let's replace the455 with a 4 to scan 4 o$ the tale. *he procedure ran in aout 66 secondsand our "uery runs worse than oth pre!ious "ueries at 5.65B seconds. *heta+eaway, stic+ with the /S/MPL?>SI@? and, as I'!e mentioned manytimes in the lecture, don't hesitate to tal+ to your dataase administrator $orad!ice.

  • 8/19/2019 Optimizing SQL Queries in Oracle

    38/69

    Is my Inde% eing HIS*O9I&/L>7/*/tale, and here is the output o$ /I7 columnecause it appears in the #H?9? clause, as well as the predicatein$ormation section elow. *a+e note o$ the column 9ows. /s you see, Oracle

    elie!es that it was pulling ac+ 65,655 rows $rom the&/7=/9>HIS*O9I&/L>7/*/ tale, where &/7=/9>I7F46. ow, theactual !alue is 65,555 and not 65,655. It is ecause we are estimating thestatistics that Oracle elie!es this is the numer or rows. *his isn't a adthing, ut ta+e note that i$ your 9ows column estimates are way o(, youmay need to gather statistics again. inally, when you're done with the/

  • 8/19/2019 Optimizing SQL Queries in Oracle

    39/69

    used in the PL/>*/L? and identi)es the rows associated with each run o$the ?CPL/I PL/. e%t, we $ollow with the +eywords O9 and your SQL"uery. *here is no output $rom this statement ecause ?CPL/I PL/ data isactually inserted into the PL/>*/L?. *he ne%t step is to issue this "uery,which pulls out the plan in$ormation associated with your statement I7,

    here, *?S*4. *he results are displayed on the ne%t slide. O+ay, let's see theoutput $rom the PL/>*/L?. 7espite the $ormatting di(erences, this issimilar to the /I7 is *?S*4. ote that there areother methods o$ otaining the ?CPL/I PL/, so please chec+ out the

    Oracle manuals $or more on this topic.

    Summary

    In summary, what did we -ust learn8 #e learned aout the 0*ree inde% andwhen it can e used. #e saw how to create a 0*ree inde% using the &9?/*?I7?C statement. #e e%plored a !ariety o$ dictionary !iews, speci)cally,/LL>*/L?S, /LL>*/>&OLI7?C?S, /LL>I7>&OL*/>S*/*S,/LL>*/>&OL>S*/*S, and /LL>I7>S*/*S. #e loo+ed into the 7MS>S*/*Spac+age, speci)cally the A/*H?9>*/L?>S*/*S procedure. Gust as a note,rememer to transition o!er to 7MS>S*/*S i$ you are using the older//L=@? command, as A/*H?9>*/L?>S*/*S does a much etter -o and,as I ha!e mentioned e$ore, $rom Oracle 45g on $orward the &9?/*? I7?Csynta% gathers stats i$ the tale is not empty and i$ your tales ha!echanged signi)cantly, the automated Oracle tas+ should update your statsnightly, i$ your dataase administrator has +ept this tas+ running. /gain,please ha!e a con!ersation with your dataase administrator. #e

    determined i$ an inde% is eing used y loo+ing at the output $rom oth/

  • 8/19/2019 Optimizing SQL Queries in Oracle

    40/69

    itmap Inde% in a little more detail than what was presented in the o!er!iewmodule, so let's get started.

    Module &ontents

    irst though, let's go o!er the module contents. #e'll tal+ aout what aitmap Inde% is. #e'll then tal+ aout when you should use a itmap inde%.e%t, we'll show you how to create a itmap Inde% on one more columns, aswell as show you se!eral e%amples. e%t, in the e!ent that you don't wantthe inde% anymore, we'll show you how to delete the inde%. In the pre!iousmodule we introduced /HIS*O9I&/L>7/*/ tale.

    9ecall $or 0*ree inde%es you can inde% a single column or multiple columns./ single column inde% is called a simple inde%, whereas a multicolumn inde%is called a composite inde%. *he same is true $or itmap inde%es and thesynta%, which we'll see in a moment, is similar to 0*ree Inde%es. /s we sawin the o!er!iew, a itmap Inde% is represented a it di(erently $rom a 0*reeInde%. Let's ta+e a loo+ at a itmap Inde% $or the &/7=/9>I7 column. /syou can see, $or each indi!idual &/7=/9>I7 !alue, there are 65 o$ themranging $rom 4065, a itmap is created with each it representing whichrows ha!e or don't ha!e that particular &/7=/9>I7 !alue. Internally,Oracle doesn't store itmap Inde%es "uite li+e this. ow, let's loo+ at howOracle handles two itmaps when used within one "uery. 9ecall $rom school

    that you learned how to per$orm computations on inary numers such as/7ing two inary numers together and O9ing two inary numerstogether. ow, /7ing two inary numers together is per$ormed itwise,that is each it in the )rst inary numer is /7ed with the correspondingit in the second inary numer, and so on. Similar $or the O9 operator. /similar computation is per$ormed etween itmap Inde%es on two or morecolumns in a single SQL "uery. /gain, let's ta+e a loo+ at the representationo$ a itmap Inde%. In this graphic we are displaying the case where&/7=/9>I7 is 1 and in this graphic we're displaying where respondentgender is . oth o$ these itmap Inde%es, y the way, are on the tale,&/7=/9>HIS*O9I&/L>7/*/. ow, gi!en the $ollowing #H?9? &lausepredicate, where &/7=/9>I7 is 1 and 9?SPO7?*>A?7?9Function, i$ Oracle's Optimizer decides to use oth itmap Inde%es the /7 operator will

  • 8/19/2019 Optimizing SQL Queries in Oracle

    41/69

    cause Oracle to per$orm a inary /7 etween the two itmaps. Here is theresult a$ter I /7ed the two itmaps, shown ao!e, together. ow,anywhere you see 4 indicates a row where &/7=/9>I7 is 1 and9?SPO7?*>A?7?9 is . *his type o$ inary computation can ee%tremely $ast. #e show a more detailed e%ample o$ this, as well as the

    /I7 column is65. *he numer o$ !alues in the 9?SPO7?*>A?7?9 column is 6 and soon. In $act, in one Oracle document it mentions that a column with 45,555distinct !alues is a candidate, e!en $or a tale containing 4 million rows or4 o$ the rows. #ith that said, you should not use itmap Inde%es oncolumns that are distinct or ha!e a !ery large numer o$ distinct !alues.Instead, consider using a 0*ree inde%. One great thing aout itmapInde%es is that

  • 8/19/2019 Optimizing SQL Queries in Oracle

    42/