AWS Community Day CPH - Three problems of Terraform
[INSIGHT OUT 2011] A21 why why is probably the right answer(tom kyte)
1. <Insert Picture Here>
Why “why” (or it depends) is probably the right
answer
Thomas Kyte
http://asktom.oracle.com/
2. Who am I
• Been with Oracle since 1993
• User of Oracle since 1987
• The “Tom” behind AskTom in
Oracle Magazine
www.oracle.com/oramag
• Expert Oracle Database
Architecture
• Effective Oracle by Design
• Expert One on One Oracle
• Beginning Oracle
What we know, shapes how we do things...
3. Why the right first answer is probably
“Why, what is your goal”
(and the second one is “it depends”)
Copyright Kyte Inc. 2005
4. What prompted me to think about this
QUOTE
its funny how you sometimes have to explain the question in order to
get the the answer.
It's not "funny", IMHO. It's rude and unproductive for people to
assume that you don't know what you are doing. For example, this
question is none of XXX’s business, and it assumes that you don't
have enough skill to follow standard DBA procedures and best-
practices
.
This person came for answers and suggestions, not to play 20-
questions with some anonymous person.
If you want to "ask" questions, please feel free to start a thread. . .
Copyright Kyte Inc. 2005
5. So, what is wrong with that
• Just about everything
• Let’s ask some questions
Copyright Kyte Inc. 2005
6. How do I reorg a table?
• You could answer with any of the following:
– Alter table T move, rebuild indexes
– Export/Import
– Create table as select, index, grant, constrain
– DBMS_REDEFINITION
– Alter table shrink (10g)
– And maybe more.
• So, why is “why, what is the goal” the only right
answer?
Copyright Kyte Inc. 2005
7. How do I reorg a table?
• You Alter table T move, rebuild indexes
– And say they did that, what did they get?
• Took 2 hours of downtime (big table, lots of indexes)
• Cost $$$, they could have been doing something truly
productive
• Did they achieve their goal? Not in this case, their perceived
problem was chained rows (say 1,000 of them)
• After the reorg, they still had 1,000 chained rows!
• We need to ask “WHY”, what is the goal.
Copyright Kyte Inc. 2005
8. How do I create a table in a stored procedure
• You could answer
– Get CREATE TABLE granted directly to you, not via a
role. Use EXECUTE IMMEDIATE ‘create table ’
– Or, you could answer “why, what is the goal?”
Copyright Kyte Inc. 2005
9. How do I create a table in a stored procedure
• Why is Why the right answer?
– They were porting from SQL Server.
– They had years of database experience.
– Just not with Oracle.
– Dropping and Creating tables in PL/SQL is not the
correct approach in Oracle.
– We need to tell them How in Oracle.
– They are not stupid, they just don’t know something yet
– We are not presumptuous or rude, just responsible
Copyright Kyte Inc. 2005
10. How do I reorg a 50gig database
At a given customer site, I must reorganize a 50 Gigs Prod DB in a
single day.
Note that I do not know the environment yet (I am replacing somebody
leaving on vacation without providing any analysis report. This guy only
re-
told this DB is bad, we must re-organize it and left on vacation...).
I only know the DB is running on a Windows server, the 50 Gigs DB is
mission-critical (hosting the PeopleSoft Financials suite), the DB is said
to be highly-fragmented at the tablespace-levels and highly chained at
the table-levels but no bad-perf issue has been apparently reported.
I do not have disk space to create a new db aside the current one, I only
have One Day to successfully carry out the whole thing. What would be
the right approach ? Keeping in mind, not to loose anything on the
way ... (i.e., low risk). What would be the best strategy ?
Copyright Kyte Inc. 2005
11. How do I reorg a 50gig database
• I asked “why”, “what is the goal”
• Answer:
– Tablespace fragmented as reported by Toad
– Chained rows, must get rid of chained rows
• Turns out peoplesoft uses lots of longs, won’t matter how
many times you rebuild will it.
• Fragmented tablespace – so what? 1 extent or 500 extents.
So what? They don’t drop/truncate, so “so what”
• Only answer is “don’t even think about doing this”. What
would have happened had I just answered the question!
Copyright Kyte Inc. 2005
12. It makes it clear
• People asking questions have a lot of inside
information
• People receiving the question don’t
• We need to communicate from person A to person
B
• And if we ass-u-me the person asking the question
has some in depth knowledge – and they don’t,
we’ve just really ‘hosed’ them.
• It would be irresponsible to not justify their
questions
Copyright Kyte Inc. 2005
13. Why ask Why?
• Because most of the time you won’t have to
actually answer after that!
– When you find out why, the answer becomes “oh, you
don’t need to do that”
• Because it is the only responsible thing to do
– You don’t know how much the person on the other end
of the ethernet cable actually knows
• Because it is the only way to get the best answer
Copyright Kyte Inc. 2005
14. It
depends
If Why isn’t the right answer, then this
one probably is
What is the best way questions
Copyright Kyte Inc. 2005
15. ops$tkyte%ORA11GR2> with
2 players as
3 ( select cast( 'P'||rownum as varchar2(2) ) username
4 from all_objects
5 where rownum <= 8),
6 weeks as
7 ( select rownum week
8 from all_objects
9 where rownum <= 7 ),
10 data as
11 ( select username,
12 week,
13 row_number() over (partition by week order by rnd) rn
14 from ( select username, week, dbms_random.random rnd
15 from players, weeks
16 )
17 )
18 select *
19 from data
20 pivot( max(username) for rn in (1,2,3,4,5,6,7,8) )
21 order by week
22 /
17. Best Practices defined –
Consensus of expert opinions, based <Insert Picture Here>
on actual customer experiences in
practice.
Lessons learned.
Proven practices associated with a
particular usage profile.
Baseline configuration rules -
prerequisite to tuning.
Sounds all good
18. Best Practices – It is easy with Best
Practices to forget that once a <Insert Picture Here>
practice has been branded as "Best",
that it may represent certain tradeoffs
and may involve noteworthy
downside potential. It is also easy to
forget the context for which any
given practice was promoted as
"Best", and therefore apply it in some
inappropriate context.
- Bob Sneed, Sun Microsystems
19. Bryn Llewellyn on Best Practices
Prescribing best practice principles for programming
any 3GL is phenomenally difficult. One of the hardest
challenges is the safety of the assumption that the
reader starts out with these qualities
• Has chosen common from education.(Howwell- can
Knows Oracle toinsidesenseseveral others. else
Can natural thefirst technical prose.excellent takes
Requiresclassanegotiating mechanical systems.
easy accessvisualize self
received Database orskills. (Good code
first excellenceclassinside out.
an PL/SQL right parents. and with
write excellent one
ability to out. coupled
mentors...
you write the requirements for your
longer to write and test than bad code; managers
developed verbal reasoning skills. code, write the
test specifications, and discuss problems that arise
want code delivered in aggressive timeframes.)
along the way?)
20. <Insert Picture Here>
“What is the best way..?” –
Questions that begin and end
with that can drive you nuts.
If there was a universal best way to do something,
we would not have implemented the other ways
AskTom “What is The best way?”
21.
22. <Insert Picture Here> Is there
a best way
to do something – every
time?
select *
from t1, t2
where t1. id = t2. id
and t1.small_distinct = :x
• T1 is large, where small_distinct = :x returns much of the table
• T2 is large
23. <Insert Picture Here> Is there
a best way
to do something – every
time?
select * from t1, t2
where t1. id = t2. Id and t1.small_distinct = :x
HASH JOIN SELECT STATEMENT
TABLE ACCESS FULL T1 NESTED LOOPS
TABLE ACCESS FULL T2 TABLE ACCESS BY INDEX ROWID(T1)
INDEX RANGE SCAN T1_IDX
TABLE ACCESS BY INDEX ROWID(T2)
INDEX UNIQUE SCAN T2_PK
24. <Insert Picture Here> Is there
a best way
to do something – every
time?
HASH JOIN SELECT STATEMENT
TABLE ACCESS FULL T1 NESTED LOOPS
TABLE ACCESS FULL T2 TABLE ACCESS BY INDEX ROWID(T1)
INDEX RANGE SCAN T1_IDX
TABLE ACCESS BY INDEX ROWID(T2)
INDEX UNIQUE SCAN T2_PK
call count cpu elapsed disk query
Fetch 35227 5.63 9.32 23380 59350
Fetch 35227 912.07 3440.70 1154555 121367981
25. <Insert Picture Here> Is there
a best way
to do something – every
time?
HASH JOIN SELECT STATEMENT
TABLE ACCESS FULL T1 NESTED LOOPS
TABLE ACCESS FULL T2 TABLE ACCESS BY INDEX ROWID(T1)
INDEX RANGE SCAN T1_IDX
TABLE ACCESS BY INDEX ROWID(T2)
INDEX UNIQUE SCAN T2_PK
call count cpu elapsed disk query
Fetch 1 4.55 5.16 12152 12456
Fetch 1 0.05 0.09 12 15
26. <Insert Picture Here>
It takes a context – It is also
easy to forget the context for
which any given practice was
promoted as "Best", and
therefore apply it in some
inappropriate context
Indexes are ‘best’, everyone knows that
27. <Insert Picture Here>
It takes understanding too –
You need to take the facts,
coupled with your knowledge,
How do I tune with tkprof
28. <Insert Picture Here>
What can
you do with this
information?
select count(subobject_name) from big_table.big_table
COUNT(SUBOBJECT_NAME)
---------------------
688256
call count cpu elapsed disk query
total 4 99.36 262.11 1840758 1840800
Rows Row Source Operation
1 SORT AGGREGATE (cr=1840797 pr=1840758 pw=0 time=262104893 us)
128000000 TABLE ACCESS FULL BIG_TABLE (cr=1840797 pr=1840758 pw=0 time=384004887 us)
Event waited on Times Max. Wait Total Waited
db file scattered read 14425 0.22 195.87
29. <Insert Picture Here>
First, there
are a bunch of
facts
• Query took a long time – if we make it fast
• We did a ton of physical IO – and that is slow
• We did a ton of logical IO – and that is not ‘free’
• There is a big difference between elapsed and cpu –
we were waiting for something
• We can see our query and plan – we know the
answer to the query
30. <Insert Picture Here>
There are
things we have
knowledge of
• We know the data (it is ours after all)
• How Oracle works (hopefully!)
31. <Insert Picture Here> What are
some obvious
things to think
about here?
• We needed a very small subset of the rows – 700k
out of 128m
• The table looks well packed – simple math, divide
IO’s (cr=1,840,797) by rows (128,000,000), about 70
rows/block and given we know the average row width
(it is our data after all ) that sounds nicely packed
• What can we rule out now? Shrink and Rebuild
32. <Insert Picture Here>
What are
Some possible
options?
• Make full scan faster
• Maybe by compressing the table
• Maybe by including subobject_name in some index (to avoid
the table)
• Remove Full Scan
• We are interested in only 0.6% of the data
• Maybe a new index would help
• Don’t do it or do it differently
33. <Insert Picture Here> In Real
Life it
Will be more
Complex
• It will be more complex in general
• But the process is the same
• Get facts
• Infer more facts
• Build your context!
• Rule things out
• Ruling something out is as good as ruling something in
• Many best practices will fall by the wayside here
34. Educated Incapacity – A <Insert Picture Here>
barrier to creative ideas can be
experience, ‘the best way’
"This 'telephone' has too many shortcomings to be seriously
considered as a means of communication. The device is inherently of
no value to us.“ – Western Union internal memo, 1876.
" The concept is interesting and well-formed, but in order to earn better
than a 'C,' the idea must be feasible.“ – Yale University management
professor in response to Fred Smith's paper proposing reliable
overnight delivery service. Smith went on to found Federal Express
Corp.
Although experience is often valuable, it can be a liability in a search
for creative ideas.
38. <Insert Picture Here>
Learn a new language – else
everything will look like a nail.
C, C++, PL/I, Rexx, Exec, JCL, SAS, Pascal,
Cobol, Java, Ada, PL/SQL, T-SQL, Prolog,
Lisp, Scheme, Various Assemblers, many SQL
dialects, many scripting languages,
39. <Insert Picture Here>
Don’t tune a query – tune a
process, an algorithm, the
entire approach.
Don’t fall into the sunk cost theory
41. <Insert Picture Here>
Collaborate – Participate,
Network, Exchange ideas.
I learn something new every day about
Oracle – from the questions I get about
Oracle
42. <Insert Picture Here>
The Best Way
Thomas Kyte
http://asktom.oracle.com/
What we know, shapes how we do things...