dba and developers

Upload: marc-jayson-p-papp

Post on 10-Apr-2018

222 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/8/2019 dba and Developers

    1/36

    7 DBA Skills for H ighly Effective Developers

    rton Consulting - April 2002

    7 DBA Skills for HighlyEffective Developers

    Presented by:

    Karen Morton

    Morton Consulting

    There is often a great divide between the Oracle DBA andDevelopers. Bridging this gap can often mean the differencebetween an efficient, well-received application and a bomb! Thispaper discusses skills that are common to DBAs but often left out of

    the developers repertoire. While it is not necessary for thedeveloper to learn all the tricks of the DBAs trade, a basicunderstanding of these seven concepts will dramatically enhancethe ability of the developer to design and write better applications.

  • 8/8/2019 dba and Developers

    2/36

    7 DBA Skills for H ighly Effective Developers

    rton Consulting - April 2002

    2

    He that is good with a hammer

    tends to think everything is a nail.

    - Abraham Maslow

  • 8/8/2019 dba and Developers

    3/36

    7 DBA Skills for H ighly Effective Developers

    rton Consulting - April 2002

    3

    Who Does What?

    Database Administrator

    Systems and performance

    Developer

    Applications

    Who does what when it comes to your Oracle database? In mostorganizations, one set of distinct duties falls to the databaseadministrator and another set falls to the developer. Typically, theDBA is concerned with the black box of Oracle: how it is installed,

    configured, and maintained for most efficient operation. Thedeveloper, on the other hand, is usually focused on thedevelopment of applications that will use the Oracle database.

    While this division of duties is natural and necessary, it can alsocreate a divide between the DBA and developer. The developertypically relies heavily on the DBA to create objects and providepermissions for their applications. So, what happens when thedeveloper needs something now and the DBA has other pressing

    tasks that take priority? What happens when the developer writescode that degrades system performance and the DBA has to figureout where the problem is and fix it? Have you ever found yourselfsaying just add more RAM when the DBA tells you your query istaking up too much memory?

  • 8/8/2019 dba and Developers

    4/36

    7 DBA Skills for H ighly Effective Developers

    rton Consulting - April 2002

    4

    The DBA

    Systemperformance

    Backup &recovery

    Security / accesspermissions

    Object creation

  • 8/8/2019 dba and Developers

    5/36

    7 DBA Skills for H ighly Effective Developers

    rton Consulting - April 2002

    5

    The Developer

    User interface

    Data acquisition

    Reporting

    End user support

  • 8/8/2019 dba and Developers

    6/36

    7 DBA Skills for H ighly Effective Developers

    rton Consulting - April 2002

    6

    Team Work

    DBA

    Developer

    Developer

    Developer

    What werethey thinking?

    All we need ismore memory!

    It works;what elsematters?!

    Does he thinkIm dumb?!

    Particularly when it comes to application performance, DBAs anddevelopers are often pitted against each other. Managementtypically expects DBAs to be responsible for how the system runsand developers to be responsible for what it runs. In other words,

    once an application is launched and it looks good and provides theuser with an interface they like and that does the job, its the job ofthe DBA to keep it running smoothly and efficiently. Many times,when performance issues arise, the DBA points the finger at thedeveloper and vice-versa. So, how do we stop the finger-pointingand really work as a team?

  • 8/8/2019 dba and Developers

    7/36

    7 DBA Skills for H ighly Effective Developers

    rton Consulting - April 2002

    7

    Oracle: The black box

    What exactly isthe database?

    Why/how do SQLstatements work?

    Is there a rightway?

    Most developers learn Oracle SQL and PL/SQL but often dont taketheir knowledge any further. But, only learning about how to writebasic SQL statements and code PL/SQL procedures and functionsleaves a whole world of important information about the database

    as a big unknown. The black box that is the Oracle database canbe daunting to go inside. But the key to becoming the mosteffective developer you can be likely resides within that black box.

    In his book, Expert One-on-One Oracle, Thomas Kyte sums up theblackbox approach as follows:

    The single most common reason for failure is a lack of practicalknowledge of the database a basic lack of understanding of thefundamental tool that is being used. The blackbox approach

    involves a conscious decision to protect the developers from thedatabase. They are actually encouraged not to learn anythingabout it! In many cases, they are prevented from exploiting it. Thereasons for this approach appear to be FUD (Fear, Uncertainty, andDoubt). They have heard that databases are hard, that SQL,transactions and data integrity are hard. The solution dontmake anyone do anything hard. They treat the database as ablack box and have some software tool generate all of the code.They try to insulate themselves with many layers of protection sothat they do no have to touch this hard database.

  • 8/8/2019 dba and Developers

    8/36

    7 DBA Skills for H ighly Effective Developers

    rton Consulting - April 2002

    8

    7 DBA Skills for Developers

    Understand basicsof Oraclearchitecture

    Learn to leveragethe documentation

    Learn how toleverage peer

    support

    Understand locking& concurrency

    Learn to use thedata dictionary

    Learn to writeeffective SQL

    Learn how to

    communicate withthe DBA

    A little knowledge will go along way towards making the working relationshipbetween the DBA and developer more interactive and productive. We willfocus here on the knowledge that can be acquired by the developer.

    7 DBA Skills for Highly Effective Developers

    1) Learn the basics of Oracle architecture

    2) Learn about locking and concurrency issues

    3) Learn how to use the Oracle data dictionary

    4) Learn how to effectively write and tune queries

    5) Learn how to use the Oracle documentation

    6) Learn how to leverage peer support

    7) Learn how to talk with the DBA (not at)

  • 8/8/2019 dba and Developers

    9/36

    7 DBA Skills for H ighly Effective Developers

    rton Consulting - April 2002

    9

    Oracle ArchitectureBasic Concepts & Terms

    Database vs. Instance

    System Global Area (SGA)

    Program Global Area (PGA)

    Files

    Processes

    Learning even a small bit about its architecture can help demystifyOracle. While I dont believe it is necessary to learn all theintricacies of Oracle architecture, I do think a basic understanding ofhow Oracle is put together can help you make better application

    design and development decisions.

  • 8/8/2019 dba and Developers

    10/36

    7 DBA Skills for H ighly Effective Developers

    rton Consulting - April 2002

    10

    Oracle Architecture

    Log

    Buffer

    SharedPool

    BufferCache

    Client

    USER

    DBWR

    LGWR

    ARCH

    Online Redo Logs

    Archived Redo Logs Data Files

    System Global Area (SGA)

    An Oracle system really is simply a system that consists ofspecialized processes that manipulate database blocks. The clientattaches to the database via a USER process which handles mostof the workload by parsing, executing and fetching SQL. Oracle

    background processes perform specialized tasks on behalf of theuser processes.

    The System Global Area is the great communicator of Oracle. Itshares its resources with all users. It is made of several sections:the database buffer cache, the shared pool and the log buffer cache.

    For more information on Oracle architecture, see the Introduction tothe Oracle Server chapter of your Oracle Concepts documentationguide.

    Writes redo log files to archived locationARCH

    Writes transactions to redo log filesLGWR

    Responsible for database writingDBWR

    PurposeBackground Process

  • 8/8/2019 dba and Developers

    11/36

    7 DBA Skills for H ighly Effective Developers

    rton Consulting - April 2002

    11

    Using Documentation

    Become familiar with the Oracle

    documentation set

    Use Oracles Technical Resource

    web site (technet.oracle.com)

    Get a couple of good referencebooks

    While the complete set of Oracle documentation may appear too bigto be helpful, it is a key source of information. If you dont haveaccess to the documentation CD that came with your companysOracle version, you can access the entire documentation set on

    Oracles Technology Network site at technet.oracle.com.Membership to this site is free and once obtained gives you accessto the full documentation set for your Oracle version. You canaccess the different guides in either HTML or PDF formats. PDFformatted guides can even be downloaded if desired.

    If youre not sure which guide to look in for the information youwant, the online documentation has a search engine that can help.

    There are a ton of Oracle reference books out there. OReilly,Oracle Press and Wrox are all publishers with good books on themarket.

  • 8/8/2019 dba and Developers

    12/36

    7 DBA Skills for H ighly Effective Developers

    rton Consulting - April 2002

    12

    Support Resources

    User Groups

    On-line Discussion Groups/Forums

    Mail Lists

    Technical Journals

    Oracle-related Web Sites

    Ask Tom http://asktom.oracle.com

    DBAToolBox http://www.dbatoolbox.com

    Evergreen Database Technologies http://www.evdbt.com

    Great Oracle Sites to Visit http://www.onwe.co.za/frank/faqlink.htm

    Hot Oracle http://www.hot-oracle.com

    IOUG-A http://www.ioug.org

    ITtoolbox Portal for Oracle http://oracle.ittoolbox.comIxora - Steve Adams http://www.ixora.com.au/home.htm

    Jonathan Lewis Consultancy http://www.jlcomp.demon.co.uk

    Material Dreams http://www.materialdreams.com

    MetaLink (Oracle) http://metalink.oracle.com

    Oracle Certified Professional Program http://www.oracle.com/education/certification

    OLN - Oracle Learning Network http://oracle.com/education/oln

    Oracle DBA Scripts and Tips http://www.think-forward.com/sqltips.htm

    Oracle Forms Developer's Handbook http://www.belacorp.com/OracleForms

    Oracle Power http://www.oraclepower.com

    Oracle Professional Newsletter http://www.pinnaclepublishing.com/OP

    Oracle Resource Stop http://www.orsweb.com

    Oracle Technical Network http://technet.oracle.com

    Oracle Techniques http://www.geocities.com/samoracle

    OracleTuning.com http://www.oracletuning.comOracleZone http://www.oraclezone.com

    Oracle Documentation http://www.oradoc.com

    Orafans http://www.orafans.com

    OraGeeks http://www.interealm.com/orageeks

    OraPerf http://www.oraperf.com

    OraPub, Inc. http://www.orapub.com/cgi/genesis.cgi

    Oriole Corp - Tools and SQL scripts http://www.oriolecorp.com

    Prometric Testing (OCP) http://www.2test.com/index.jsp

    Rama K. Nalam - Good Oracle Links http://members.spree.com/rnalam/html/oracle/oralinks.html

    RevealNet Oracle DBA Pipeline - Home http://128.121.241.221/pipelines/dba/index.htm

    RevealNet Oracle DBA Pipeline - Papers http://128.121.241.221/pipelines/dba/archives.htm

    SearchDatabase.com http://www.searchdatabase.com

    SQL Course - SQL Tutorial http://www.sqlcourse.comTUSC-The Ultimate Software Consultants http://www.tusc.com

    Verge Technologies - PowerExplain http://verge-tech.com/verge

  • 8/8/2019 dba and Developers

    13/36

    7 DBA Skills for H ighly Effective Developers

    rton Consulting - April 2002

    13

    Locking & Concurrency

    Oracle locks ONLY at the row level

    Data is never locked for read

    Data being written doesnt block

    data being read

    Data being written is only blockedby another data writer that has

    already locked the row

    One of the most differentiating factors between databases is howthey handle locking and concurrency. One database is likely verydifferent from another. It is critical for the developer to understandhow Oracle handles locking in order to successfully write their

    applications. If you dont understand how Oracle accesses data forreads and locks data for writes, you can possibly make very seriousmistakes in your application that will cause your application to notfunction properly as more users are added to your system.

    These are some basic rules of thumb about how Oracle handleslocking and concurrency. To get a more complete overview of thistopic, see the Oracle Concepts guide.

  • 8/8/2019 dba and Developers

    14/36

    7 DBA Skills for H ighly Effective Developers

    rton Consulting - April 2002

    14

    Read Committed Isolation

    Read committed (default)

    An executed query sees only datathat was committed before the query(not the transaction) began

    SET TRANSACTION ISOLATION LEVEL

    READ COMMITTED ;

    This level of consistency is the default and is the minimum levelenforced by Oracle. It basically guarantees that when a singlequery executes, it returns data from the point in time at which thequery began. Therefore, a query will never see any changes made

    by transactions that commit while the query is executing. This levelof isolation is appropriate for applications where few conflicts arelikely.

    This form of isolation does not keep other transactions frommodifying the data read by a query. So, if a transaction executesthe same query multiple times, the query may return differentresults each time.

    This level also would allow two sessions to modify the same row atbasically the same time and not be notified. In this manner, twotransactions could apply an update to a row one following the otherand, in effect, duplicate the update.

    Instead of setting the isolation level at the beginning of eachtransaction, it can be set for all transactions within a given session:

    ALTER SESSION SET ISOLATION_LEVEL READ COMMITTED ;

  • 8/8/2019 dba and Developers

    15/36

    7 DBA Skills for H ighly Effective Developers

    rton Consulting - April 2002

    15

    Serializable Isolation

    Serializable transactions

    Transactions that see only changesthat were committed at the start timeof the transaction plus any changesmade by the transaction itself

    SET TRANSACTION ISOLATION LEVEL

    SERIALIZABLE ;

    Serializable isolation enforces read consistency at the transactionlevel. All data access during a single transaction reflect the state ofthe database at the time the transaction began. The exception tothis is that the transaction does see changes made by itself since

    the time it started.

    Using this isolation level, multiple transactions attempting to accessthe same data would be notified of changes to the data made sinceit initially retrieved it. This level effectively prohibits duplicatemodifications without notifying the transaction that data hadchanged. Oracle would generate an error notifying that anothertransaction modified its data after it began:

    ORA-08177: Cannot serialize access for this transaction

    Instead of setting the isolation level at the beginning of eachtransaction, it can be set for all transactions within a given session:

    ALTER SESSION SET ISOLATION_LEVEL SERIALIZABLE ;

  • 8/8/2019 dba and Developers

    16/36

    7 DBA Skills for H ighly Effective Developers

    rton Consulting - April 2002

    16

    Read-Only Isolation

    Read-only

    Transactions that see only changesthat were committed at the start timeof the transaction and do not allowany DML statements (INS,UPD,DEL)

    SET TRANSACTION ISOLATION LEVEL

    READ ONLY;

    Read-only isolation ensures that transactions are consistent fromthe point in time they begin and do not allow any DML to beexecuted during the transaction that would cause changes.

  • 8/8/2019 dba and Developers

    17/36

    7 DBA Skills for H ighly Effective Developers

    rton Consulting - April 2002

    17

    Locking Mechanisms

    Locks prevent destructive

    interaction between transactionsaccessing the same resource

    Resources include two generaltypes of objects

    Structures and data (tables and rows)

    System objects (memory and datadictionary rows)

    Oracle will automatically acquire the locks it needs when executingSQL statements. Locking is tightly integrated with transactioncontrol. Therefore, application developers only need to define thetransactions properly and Oracle will handle the locking

    automatically.

    Locking can be manually manipulated if you wish to overrideOracles default locking mechanisms.

    To get a more complete overview of Oracles locking mechanisms,see the Oracle Concepts guide.

  • 8/8/2019 dba and Developers

    18/36

    7 DBA Skills for H ighly Effective Developers

    rton Consulting - April 2002

    18

    The Data Dictionary

    Read only set of tables and views thathouse information about the database

    All object definitions (tables, etc)

    Space allocations

    Default values

    Integrity constraints

    Users and privileges

    Auditing information (who used what & when) Lots of other stuff. . .

    The data dictionary is the central information resource for yourOracle database. Oracle itself uses the data dictionary to find outinformation about objects being accessed, users and their privilegesand records information about all objects as they are created and

    modified. As a developer, you can leverage the data dictionary as awonderful reference to find out just about anything youd like.

    What if you want to know how big a particular table is or whatindexes or integrity constraints are on a given table? Use the datadictionary. All users have access to see at least part of theinformation stored in the data dictionary. You can see any datadictionary object that is named beginning with ALL_ or USER_.Other objects can be made available to you by specific permission.

    Specifically, the SELECT_CATALOG_ROLE, when granted to you, willallow you to see system performance tables (V$) and more.

  • 8/8/2019 dba and Developers

    19/36

    7 DBA Skills for H ighly Effective Developers

    rton Consulting - April 2002

    19

    The Data Dictionary

    To gain access to additional data

    dictionary information, request fromyour DBA to be granted the role

    called:

    SELECT_CATALOG_ROLE

    Other objects can be made available to you by specific permission.Specifically, the SELECT_CATALOG_ROLE, when granted to you, willallow you to see system performance views (V$) and more. Youllneed access to many of these views if you want to do more

    advanced tuning of your application code.

  • 8/8/2019 dba and Developers

    20/36

  • 8/8/2019 dba and Developers

    21/36

    7 DBA Skills for H ighly Effective Developers

    rton Consulting - April 2002

    21

    The Data Dictionary

    chk_dict.sql

    column comments format a40 word_wrapped

    set verify off

    clear screen

    accept search_phrase char prompt "Enter a search

    phrase: "

    SELECT * FROM dict

    WHERE UPPER(comments) LIKE UPPER('%&search_phrase%')

    ORDER BY table_name

    /

    This script provides a simple way to query the data dictionary to lookfor a particular word or phrase. It looks in the comments column tofind a match and displays the results.

  • 8/8/2019 dba and Developers

    22/36

  • 8/8/2019 dba and Developers

    23/36

    7 DBA Skills for H ighly Effective Developers

    rton Consulting - April 2002

    23

    EXPLAIN PLAN

    EXPLAIN PLAN displays the

    execution plan for a SQL statement

    The execution plan shows the exact

    sequence of steps that the Oracleoptimizer chose to process the SQL

    The plan is stored in a table called

    PLAN_TABLE

  • 8/8/2019 dba and Developers

    24/36

    7 DBA Skills for H ighly Effective Developers

    rton Consulting - April 2002

    24

    PLAN_TABLE Creation

    The plan_table must be created

    prior to executing the EXPLAINPLAN command

    The DDL statement to create theplan_table can normally be found

    in a script named utlxplan.sql

  • 8/8/2019 dba and Developers

    25/36

    7 DBA Skills for H ighly Effective Developers

    rton Consulting - April 2002

    25

    Important Columns

    The optimizers access path isidentified in the OPERATION andOPTION columns

    The ID column identifies the

    execution order of the steps in the

    plan

  • 8/8/2019 dba and Developers

    26/36

    7 DBA Skills for H ighly Effective Developers

    rton Consulting - April 2002

    26

    The EXPLAIN PLAN Command

    EXPLAIN PLAN statement has the

    following syntax:

    EXPLAIN PLAN

    [SET STATEMENT_ID = string literal]

    [INTO table_name]

    FOR sql statement

    SET STATEMENT_ID is the parameter that allows you to assign alabel to a given execution plan. If you will be sharing a singleplan_table with others, using the statement_id is necessary to beable to tell the difference between your plans and those of other

    developers. You can set the statement_id to any string you desirebut if not included, the column will contain NULL.

    INTO is a clause that allows you to put explain plan results into aplan_table that you create. You must create this other table as anexact duplicate of the official plan_table.

    FOR is the keyword that indicates the SQL statement you wish tohave explained. You may explain any query or DML statement

    (SELECT, INSERT, UPDATE or DELETE).

  • 8/8/2019 dba and Developers

    27/36

    7 DBA Skills for H ighly Effective Developers

    rton Consulting - April 2002

    27

    Explaining a Simple Query

    EXPLAIN PLAN FORSELECT * FROM emp;

    SELECT operation, options,

    object_name, id, parent_id,

    cardinality, cost

    FROMplan_table

    ORDER BY id;

    OPERATION OPTIONS OBJECT_NAME ID PARENT_ID CARD COST

    --------------- ---------- ------------ ---- --------- ------- -------SELECT STATEMENT 0 94,362 292

    TABLE ACCESS FULL EMP 1 0 94,362 292

    Since the is no WHERE clause on the explained query (all the rowsare being selected), a full table scan is the most efficient accesspath and is chosen by the optimizer.

    To save space, the example on the slide was shortened. Thecomplete example is shown below.

    DELETE FROM PLAN_TABLE;

    EXPLAIN PLAN

    FOR SELECT * FROM emp;

    COL operation FORMAT A20 WORD_WRAPPED

    COL options FORMAT A15

    COL object_name FORMAT A15

    COL id FORMAT 999

    COL cardinality FORMAT 999,999 HEADING CARD

    SELECT operation, options, object_name, id,

    parent_id, cardinality, cost

    FROM plan_table

    ORDER BY id;

  • 8/8/2019 dba and Developers

    28/36

  • 8/8/2019 dba and Developers

    29/36

    7 DBA Skills for H ighly Effective Developers

    rton Consulting - April 2002

    29

    SQL*Plus AUTOTRACE

    The statement has the following

    syntax:

    SET AUTOTRACE [ON | OFF |

    TRACEONLY] [EXPLAIN]

    [STATISTICS]

    Automating Explain Plans

    By turning the AUTOTRACE SQL*Plus environment variable on, youcan automate the generation of explain plans. When attempting touse the AUTOTRACE facility, you will need to have been granted thePLUSTRACE role by the DBA and must also have access to aPLAN_TABLE.

    The settings for AUTOTRACE are as follows:

    SET AUTOTRACE ON enables automatic generation of the explainplan and execution statistics.

    SET AUTOTRACE ON EXPLAIN enables the display of only theexplain plan.

    SET AUTOTRACE ON STATISTICS enables the display of only theexecution statistics.

    SET AUTOTRACE TRACEONLY enables both the display of theexplain plan and the execution statistics but suppresses the displayof the data from the SQL statement.

    SET AUTOTRACE OFF disables automatic generation of theexplain plan.

  • 8/8/2019 dba and Developers

    30/36

    7 DBA Skills for H ighly Effective Developers

    rton Consulting - April 2002

    30

    AUTOTRACE Example

    SET AUTOTRACE ON provides EXPLAINoutput and statistics on queryexecution

    By default, autotrace runs thequery

    SQL> SET AUTOTRACE TRACEONLY EXPLAIN STATISTICS

    SQL> SELECT * FROM EMP;97 rows selected.

    TRACEONLY suppresses query output.

  • 8/8/2019 dba and Developers

    31/36

  • 8/8/2019 dba and Developers

    32/36

    7 DBA Skills for H ighly Effective Developers

    rton Consulting - April 2002

    32

    tkprof and SQL_TRACE

    To cause a trace of SQL execution to begenerated for a session:ALTER SESSION SET SQL_TRACE = TRUE ;

    The tkprof utility formats the resultingoutput

    Tkprof output breaks down executionstatistics, the execution plan and rows

    returned for each step.

    Tkprof provides statistics that are unavailable from any othersource. It can take a trace file (generated by turning onSQL_TRACE for a session) and format it to display executionstatistics, the execution plan and rows returned for each step.

    While tkprof is a powerful tool, it does require a significant learningcurve to be able to interpret the output effectively.

  • 8/8/2019 dba and Developers

    33/36

    7 DBA Skills for H ighly Effective Developers

    rton Consulting - April 2002

    33

    Interacting with the DBA

    Schedule a time to talk

    Show youve done your homework

    Be specific

    Ask questions about how/why

    things are working as they are

    Listen

    When you need to talk with your DBA, the first thing to do isschedule a time to talk. By doing this, you show that you respectthe DBAs time and priorities and will likely be able to have their fullattention when you do talk with them.

    Before going to your DBA, be sure that youve done yourhomework. Provide example code and discuss the results of testsyouve performed. Be specific about what youve tried and whatyou need to accomplish. The more you know, the more able youwill be to ask pointed questions that will get you the informationyou need.

    Most importantly, listen. Your efforts to learn about the database

    can be served greatly by listening to someone who knows itintimately, as most DBAs do.

  • 8/8/2019 dba and Developers

    34/36

    7 DBA Skills for H ighly Effective Developers

    rton Consulting - April 2002

    34

    Benefits of New Knowledge

    Better applications

    Better relationships

    Less application performance

    issues

    Less time re-writing code orchanging application design

    More confidence in your abilities

  • 8/8/2019 dba and Developers

    35/36

    7 DBA Skills for H ighly Effective Developers

    rton Consulting - April 2002

    35

    Questions&

    Answers

  • 8/8/2019 dba and Developers

    36/36

    7 DBA Skills for H ighly Effective Developers

    36

    Thanks!

    Send questions to:[email protected]

    Visit us on the web at:www.morton-consulting.com