less09 data - managing data and concurrency

Upload: jaseme1

Post on 04-Apr-2018

221 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/30/2019 Less09 Data - Managing Data and Concurrency

    1/33

    Copyright 2008, Oracle. All rights reserved.

    Managing Data and Concurrency

  • 7/30/2019 Less09 Data - Managing Data and Concurrency

    2/33

    Copyright 2008, Oracle. All rights reserved.9 - 2

    Objectives

    After completing this lesson, you should be able to: Manage data by using SQL Identify and administer PL/SQL objects

    Describe triggers and triggering events Monitor and resolve locking conflicts

  • 7/30/2019 Less09 Data - Managing Data and Concurrency

    3/33

    Copyright 2008, Oracle. All rights reserved.9 - 3

    Manipulating Data by Using SQL .

    SQL> INSERT INTO employees VALUES 2 (9999,'Bob','Builder','[email protected]',NULL,SYSDATE,

    3 'IT_PROG',NULL,NULL,100,90);1 row created.

    SQL> UPDATE employees SET SALARY=6000 2 WHERE EMPLOYEE_ID = 9999;

    1 row updated.

    SQL> DELETE from employees2 WHERE EMPLOYEE_ID = 9999;

    1 row deleted.

  • 7/30/2019 Less09 Data - Managing Data and Concurrency

    4/33

    Copyright 2008, Oracle. All rights reserved.9 - 4

    INSERT Command

    Create one row at a time. Insert many rows from another table.

    SQL> INSERT INTO emp VALUES (9999,'Bob','Builder',100,SYSDATE, 10000);

    SQL> INSERT INTO emp select employee_id , last_name,first_name,department_id, hire_date, salary FROM HR.employees where department_id =30;

    6 rows created.

    1

    2

  • 7/30/2019 Less09 Data - Managing Data and Concurrency

    5/33

    Copyright 2008, Oracle. All rights reserved.9 - 5

    UPDATE Command

    Use the UPDATE command to change zero or more rows of a table.

    SQL> UPDATE EMP SET SALARY= salary +500 Where emp_no =117;

    1 row updated.

    SQL> UPDATE EMP Set dept_no= (select dept_no from emp where emp_no =117);

    6 rows updated.

    1

    2

  • 7/30/2019 Less09 Data - Managing Data and Concurrency

    6/33

    Copyright 2008, Oracle. All rights reserved.9 - 6

    DELETE Command

    Use the DELETE command to remove zero or more rows

    SQL> DELETE FROM emp WHERE emp_no =9000;

    0 rows deleted.

    SQL> DELETE FROM emp;

    6 rows deleted.

    1

    2

  • 7/30/2019 Less09 Data - Managing Data and Concurrency

    7/33Copyright 2008, Oracle. All rights reserved.9 - 7

    MERGE Command

    Use the MERGE command to perform both INSERT and UPDATE in a single command.

    MERGE INTO EMP a USING

    (Select * FROM HR.employees) b ON (a.emp_no =b. employee_id )WHEN MATCHED THEN UPDATE SET a.salary =b.salary WHEN NOT MATCHED THEN INSERT

    (emp_no, last_name, first_name, dept_no, hire_date, salary)VALUES

    (employee_id,last_name, first_name, department_id,hire_date, salary);

  • 7/30/2019 Less09 Data - Managing Data and Concurrency

    8/33Copyright 2008, Oracle. All rights reserved.9 - 9

    COMMIT and ROLLBACK Commands

    To finish a transaction: COMMIT makes the change permanent ROLLBACK undoes the change

    SQL> COMMIT;Commit complete.

  • 7/30/2019 Less09 Data - Managing Data and Concurrency

    9/33Copyright 2008, Oracle. All rights reserved.9 - 10

    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

  • 7/30/2019 Less09 Data - Managing Data and Concurrency

    10/33Copyright 2008, Oracle. All rights reserved.9 - 11

    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 Use supplied packages for administrative and database

    maintenance tasks

  • 7/30/2019 Less09 Data - Managing Data and Concurrency

    11/33Copyright 2008, Oracle. All rights reserved.9 - 12

    PL/SQL Objects

    There are many types of PL/SQL database objects: Package Package body

    Type body Procedure Function Trigger

  • 7/30/2019 Less09 Data - Managing Data and Concurrency

    12/33Copyright 2008, Oracle. All rights reserved.9 - 13

    Functions

  • 7/30/2019 Less09 Data - Managing Data and Concurrency

    13/33Copyright 2008, Oracle. All rights reserved.9 - 14

    Procedures

    Are used to perform specific actions Pass values in and out by using an argument list Can be called from within other programs using the

    CALL command

  • 7/30/2019 Less09 Data - Managing Data and Concurrency

    14/33Copyright 2008, Oracle. All rights reserved.9 - 15

    Packages

    Packages are collections of functions and procedures.Each package should consist of two objects:

    Package specification Package body

    Package specification

  • 7/30/2019 Less09 Data - Managing Data and Concurrency

    15/33Copyright 2008, Oracle. All rights reserved.9 - 16

    Package Specification and Body

  • 7/30/2019 Less09 Data - Managing Data and Concurrency

    16/33Copyright 2008, Oracle. All rights reserved.9 - 17

    Built-in Packages

    Oracle Database comes with several built-in PL/SQLpackages that provide:

    Administration and maintenance utilities Extended functionality

    Use the DESCRIBE command to view subprograms.

    SQL>DESC DBMS_LOCK

  • 7/30/2019 Less09 Data - Managing Data and Concurrency

    17/33Copyright 2008, Oracle. All rights reserved.9 - 18

    Triggers

  • 7/30/2019 Less09 Data - Managing Data and Concurrency

    18/33Copyright 2008, Oracle. All rights reserved.9 - 19

    Triggering Events

    Event Type Examples of Events DML INSERT , UPDATE , DELETE

    DDL CREATE , DROP , ALTER , GRANT , REVOKE , RENAME

    Database LOGON , LOGOFF , STARTUP , SHUTDOWN , SERVERERROR , SUSPEND

  • 7/30/2019 Less09 Data - Managing Data and Concurrency

    19/33Copyright 2008, Oracle. All rights reserved.9 - 20

    Locks

    Prevent multiple sessions from changing the same data at the same time

    Are automatically obtained at the lowest possible level for a given statement

    Do not escalate

    Transaction 1

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

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

    Transaction 2

  • 7/30/2019 Less09 Data - Managing Data and Concurrency

    20/33Copyright 2008, Oracle. All rights reserved.9 - 21

    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

    or ROLLBACK operation)

    Transaction 1

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

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

    Transaction 2

  • 7/30/2019 Less09 Data - Managing Data and Concurrency

    21/33Copyright 2008, Oracle. All rights reserved.9 - 22

    Data Concurrency

    Time:

    09:00:00

    Transaction 1 UPDATE hr.employeesSET salary=salary+100 WHERE employee_id=100;

    Transaction 2 UPDATE hr.employeesSET salary=salary+100 WHERE employee_id=101;

    Transaction 3 UPDATE hr.employeesSET salary=salary+100 WHERE employee_id=102;

    ... ...

    Transaction x UPDATE hr.employeesSET salary=salary+100 WHERE employee_id =xxx;

  • 7/30/2019 Less09 Data - Managing Data and Concurrency

    22/33Copyright 2008, Oracle. All rights reserved.9 - 23

    Data and Concurrency../2

    Lock modes:ROW SHARE : permits concurrent access to the lockedtable but prohibits sessions from locking the entire table for exclusive access.ROW EXLUSIVE : is the same as ROW SHARE, but also

    prohibits locking in SHARE mode. The ROW EXCLUSIVElocks are automatically obtained when updating, inserting, or deleting data. ROW EXCLUSIVE locks allows multiplereaders and one writer.SHARE: permits concurrent queries but prohibits updates tothe locked table. A SHARE lock is required (andautomatically requested) to create an index on a table.However, online index creation requires a ROW SHARElock that is used when building the index.

  • 7/30/2019 Less09 Data - Managing Data and Concurrency

    23/33Copyright 2008, Oracle. All rights reserved.9 - 25

    Data and Concurrency../4

    Like any request for a lock, manual lock statements wait until all

    sessions that either already have locks or have previously requested locks release their locks.The LOCK command accepts a special argument that controls the waiting behaviour NOWAIT.

    NOWAIT returns control to you immediately if the specified table is already locked by another session.It is usually not necessary to manually lock objects.

    The automatic locking mechanism provides the data concurrency needed for most applications.

    Oracle recommends that you avoid using manual locks, especially when developing applications.Severe performance issues often occur from unnecessarily high locking levels

  • 7/30/2019 Less09 Data - Managing Data and Concurrency

    24/33Copyright 2008, Oracle. All rights reserved.9 - 26

    DML Locks

    Each DML transaction must acquire two locks: EXCLUSIVE row lock on the row or rows being updated ROW EXCLUSIVE table-level lock on the table containing

    the rows

    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 Transaction 1

  • 7/30/2019 Less09 Data - Managing Data and Concurrency

    25/33Copyright 2008, Oracle. All rights reserved.9 - 27

    Enqueue Mechanism

    The enqueue mechanism keeps track of: Sessions waiting for locks Requested lock mode

    Order in which sessions requested the lock

  • 7/30/2019 Less09 Data - Managing Data and Concurrency

    26/33Copyright 2008, Oracle. All rights reserved.9 - 28

    Lock Conflicts

    UPDATE employees SET salary=salary+100 WHERE employee_id=100;1 row updated.

    9:00:00 UPDATE employees SET salary=salary+100 WHERE employee_id=101;1 row updated.

    UPDATE employees SET COMMISION_PCT=2 WHERE employee_id=101;Session waits enqueued due to lock conflict.

    9:00:05 SELECT sum(salary) FROM employees;SUM(SALARY)-----------

    692634

    Session still waiting!

    16:30:00

    Many selects, inserts, updates,

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

    1 row updated.Session continues.

    16:30:01 commit;

    Transaction 1 Transaction 2 Time

  • 7/30/2019 Less09 Data - Managing Data and Concurrency

    27/33

    Copyright 2008, Oracle. All rights reserved.9 - 29

    Possible Causes of Lock Conflicts

    Uncommitted changes Long-running transactions Unnecessarily high locking levels

  • 7/30/2019 Less09 Data - Managing Data and Concurrency

    28/33

    Copyright 2008, Oracle. All rights reserved.9 - 30

    Detecting Lock Conflicts

    Select Blocking Sessions on the Performance page.

    Click the Session ID link to view information about the locking session, including the actual SQL statement.

  • 7/30/2019 Less09 Data - Managing Data and Concurrency

    29/33

    Copyright 2008, Oracle. All rights reserved.9 - 31

    Resolving Lock Conflicts

    To resolve a lock conflict: Have the session holding the lock commit or roll back Terminate the session holding the lock (in an

    emergency)

  • 7/30/2019 Less09 Data - Managing Data and Concurrency

    30/33

    Copyright 2008, Oracle. All rights reserved.9 - 32

    Resolving Lock Conflicts with 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

  • 7/30/2019 Less09 Data - Managing Data and Concurrency

    31/33

    Copyright 2008, Oracle. All rights reserved.9 - 33

    Deadlocks

    Transaction 2 Transaction 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 = 1342 WHERE employee_id = 2000;

    UPDATE employeesSET manager = 1342 WHERE employee_id = 1000;

    9:00

    9:15

    9:16

  • 7/30/2019 Less09 Data - Managing Data and Concurrency

    32/33

    Copyright 2008, Oracle. All rights reserved.9 - 34

    Summary

    In this lesson, you should have learned how to: Manage data by using SQL Identify and administer PL/SQL objects Describe triggers and triggering events Monitor and resolve locking conflicts

  • 7/30/2019 Less09 Data - Managing Data and Concurrency

    33/33

    Practice 9 Overview: Managing Data and Concurrency

    This practice covers the following topics: Identifying locking conflicts Resolving locking conflicts