Less08 Data TB3

download Less08 Data TB3

of 34

Transcript of Less08 Data TB3

  • 8/14/2019 Less08 Data TB3

    1/34

    8Copyright 2005, Oracle. All rights reserved.

    Managing Data and Concurrency

  • 8/14/2019 Less08 Data TB3

    2/34

    8-2 Copyright 2005, Oracle. All rights reserved.

    Objectives

    After completing this lesson, you should be able to do

    the following:

    Manage data through the use of SQL

    Identify and administer PL/SQL objects Describe triggers and triggering events

    Monitor and resolve locking conflicts

  • 8/14/2019 Less08 Data TB3

    3/34

    8-3 Copyright 2005, Oracle. All rights reserved.

    Manipulating Data Through SQL .

    SQL> INSERT INTO employees VALUES2 (9999,'Bob','Builder','[email protected]',NULL,SYSDATE,3 'IT_PROG',NULL,NULL,100,90);

    1 row created.

    SQL> UPDATE employees SET SALARY=60002 WHERE EMPLOYEE_ID = 9999;

    1 row updated.

    SQL> DELETE from employees2 WHERE EMPLOYEE_ID = 9999;

    1 row deleted.

    > SQL

    PL/SQL

    Locks

  • 8/14/2019 Less08 Data TB3

    4/34

    8-4 Copyright 2005, Oracle. All rights reserved.

    The INSERT Command

    Create one row at a time.

    Insert many rows from another table.

  • 8/14/2019 Less08 Data TB3

    5/34

  • 8/14/2019 Less08 Data TB3

    6/34

    8-6 Copyright 2005, Oracle. All rights reserved.

    The DELETE Command

    Use the DELETE command to remove zero or morerows from a table.

  • 8/14/2019 Less08 Data TB3

    7/348-7 Copyright 2005, Oracle. All rights reserved.

    TheMERGE Command

    Use theMERGE command to perform both INSERT andUPDATE in a single command.

  • 8/14/2019 Less08 Data TB3

    8/348-8 Copyright 2005, Oracle. All rights reserved.

    TheMERGE CommandFull Notes Page

  • 8/14/2019 Less08 Data TB3

    9/34

  • 8/14/2019 Less08 Data TB3

    10/348-10 Copyright 2005, Oracle. All rights reserved.

    PL/SQL

    Oracles Procedural Language extension to SQL

    (PL/SQL) is a fourth-generation programming language

    (4GL). It provides:

    Procedural extensions to SQL

    Portability across platforms and products

    Higher level of security and data integrity

    protection

    Support for object-oriented programming

    SQL

    > PL/SQL

    Locks

  • 8/14/2019 Less08 Data TB3

    11/348-11 Copyright 2005, Oracle. All rights reserved.

    PL/SQL

    Full Notes Page

  • 8/14/2019 Less08 Data TB3

    12/348-12 Copyright 2005, Oracle. All rights reserved.

    Administering PL/SQL Objects

    Database administrators should be able to:

    Identify problem PL/SQL objects

    Recommend the appropriate use of PL/SQL

    Load PL/SQL objects into the database Assist PL/SQL developers in troubleshooting

  • 8/14/2019 Less08 Data TB3

    13/348-13 Copyright 2005, Oracle. All rights reserved.

    PL/SQL Objects

    There are many types of PL/SQL database objects:

    Package

    Package body

    Type body Procedure

    Function

    Trigger

  • 8/14/2019 Less08 Data TB3

    14/348-14 Copyright 2005, Oracle. All rights reserved.

    Functions

  • 8/14/2019 Less08 Data TB3

    15/348-15 Copyright 2005, Oracle. All rights reserved.

    Procedures

    Procedures are used to perform a specific action.

    They:

    Pass values in and out by using an argument list

    Are called using the CALLcommand

  • 8/14/2019 Less08 Data TB3

    16/348-16 Copyright 2005, Oracle. All rights reserved.

    Packages

    Packages are collections of functions and procedures.

    Each package should consist of two objects:

    Package specification

    Package body

    Package specification

  • 8/14/2019 Less08 Data TB3

    17/348-17 Copyright 2005, Oracle. All rights reserved.

    Package Specification and Body

  • 8/14/2019 Less08 Data TB3

    18/34

  • 8/14/2019 Less08 Data TB3

    19/348-19 Copyright 2005, Oracle. All rights reserved.

    Triggers

  • 8/14/2019 Less08 Data TB3

    20/348-20 Copyright 2005, Oracle. All rights reserved.

    Triggering Events

    Examples of EventsEvent Type

    LOGON, LOGOFF, STARTUP, SHUTDOWN,

    SERVERERROR

    Database

    CREATE, DROP, ALTER, GRANT, REVOKE,

    RENAME

    DDL

    INSERT, UPDATE, DELETEDML

  • 8/14/2019 Less08 Data TB3

    21/348-21 Copyright 2005, Oracle. All rights reserved.

    Locks

    Locks prevent multiple sessions from changing

    the same data at the same time.

    They are automatically obtained at the lowest

    possible level for a given statement.

    They do not escalate.

    Transaction 1

    SQL> UPDATE employees2 SET salary=salary*1.13 WHERE employee_id=100;

    SQL> UPDATE employees2 SET salary=salary+1003 WHERE employee_id=100;

    Transaction 2

    SQL

    PL/SQL

    > Locks

  • 8/14/2019 Less08 Data TB3

    22/348-22 Copyright 2005, Oracle. All rights reserved.

    Locking Mechanism

    High level of data concurrency:

    Row-level locks for inserts, updates, and deletes

    No locks required for queries

    Automatic queue management Locks held until the transaction ends (with the

    COMMIT orROLLBACKoperation)

    Transaction 1

    SQL> UPDATE employees2 SET salary=salary*1.13 WHERE employee_id=101;

    SQL> UPDATE employees2 SET salary=salary+1003 WHERE employee_id=100;

    Transaction 2

  • 8/14/2019 Less08 Data TB3

    23/348-23 Copyright 2005, Oracle. All rights reserved.

    Data Concurrency

    UPDATE hr.employees

    SET salary=salary+100

    WHERE employee_id=xxx;

    Transaction x

    ......

    UPDATE hr.employees

    SET salary=salary+100

    WHERE employee_id=102;

    Transaction 3

    UPDATE hr.employees

    SET salary=salary+100

    WHERE employee_id=101;

    Transaction 2

    UPDATE hr.employees

    SET salary=salary+100

    WHERE employee_id=100;

    Transaction 1Time:

    09:00:00

  • 8/14/2019 Less08 Data TB3

    24/348-24 Copyright 2005, Oracle. All rights reserved.

    Data Concurrency

    Full Notes Page

  • 8/14/2019 Less08 Data TB3

    25/348-25 Copyright 2005, Oracle. All rights reserved.

    DML Locks

    Transaction 1

    SQL> UPDATE employees2 SET salary=salary*1.13 WHERE employee_id= 106;

    1 row updated.

    SQL> UPDATE employees2 SET salary=salary*1.13 WHERE employee_id= 107;

    1 row updated.

    Transaction 2

    Each DML transaction must acquire two locks:

    EXCLUSIVE row lock for the row or rows being

    updated

    ROWEXCLUSIVE table-level lock for the table

    containing the rows

  • 8/14/2019 Less08 Data TB3

    26/34

    8-26 Copyright 2005, Oracle. All rights reserved.

    Enqueue Mechanism

    The enqueue mechanism keeps track of:

    Sessions waiting for locks

    The requested lock mode

    The order in which sessions requested the lock

  • 8/14/2019 Less08 Data TB3

    27/34

    8-27 Copyright 2005, Oracle. All rights reserved.

    Lock Conflicts

    commit;16:30:011 row updated.

    Session continues.

    Many selects, inserts, updates,

    and deletes during the last 7.5hours, but no commits or

    rollbacks!

    16:30:00

    Session still waiting!

    SELECT sum(salary) FROMemployees;

    SUM(SALARY)

    -----------

    692634

    9:00:05UPDATE employees SETCOMMISION_PCT=2 WHERE

    employee_id=101;

    Session waits enqueued due to

    lock conflict.

    UPDATE employees SET

    salary=salary+100 WHERE

    employee_id=101;

    1 row updated.

    9:00:00UPDATE employees SET

    salary=salary+100 WHERE

    employee_id=100;

    1 row updated.

    Transaction 1 Transaction 2Time

  • 8/14/2019 Less08 Data TB3

    28/34

    8-28 Copyright 2005, Oracle. All rights reserved.

    Possible Causes of Lock Conflicts

    Uncommitted changes

    Long-running transactions

    Unnecessarily high locking levels

  • 8/14/2019 Less08 Data TB3

    29/34

  • 8/14/2019 Less08 Data TB3

    30/34

  • 8/14/2019 Less08 Data TB3

    31/34

    8-31 Copyright 2005, Oracle. All rights reserved.

    Resolving Lock Conflicts Using SQL

    SQL statements can be used to determine the blocking

    session and kill it.

    SQL> alter system kill session '144,8982' immediate;

    SQL> select sid, serial#, username

    from v$session where sid in(select blocking_session from v$session)

    Result:

    1

    2

  • 8/14/2019 Less08 Data TB3

    32/34

    8-32 Copyright 2005, Oracle. All rights reserved.

    Deadlocks

    Transaction 2Transaction 1

    UPDATE employees

    SET salary = salary x 1.1WHERE employee_id = 1000;

    UPDATE employeesSET salary = salary x 1.1WHERE employee_id = 2000;

    ORA-00060:Deadlock detected whilewaiting for resource

    UPDATE employees

    SET manager = 1342WHERE employee_id = 2000;

    UPDATE employeesSET manager = 1342WHERE employee_id = 1000;

    9:00

    9:15

    9:16

  • 8/14/2019 Less08 Data TB3

    33/34

    8-33 Copyright 2005, Oracle. All rights reserved.

    Summary

    In this lesson, you should have learned how to:

    Manage data through the use of SQL

    Identify and administer PL/SQL objects

    Describe triggers and triggering events Monitor and resolve locking conflicts

  • 8/14/2019 Less08 Data TB3

    34/34

    Practice Overview:

    Managing Data and Concurrency

    This practice covers the following topics:

    Identifying locking conflicts

    Resolving locking conflicts