1. Trang chủ
  2. » Công Nghệ Thông Tin

Sybex OCA Oracle 10g Administration I Study Guide phần 8 pdf

60 388 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 60
Dung lượng 2,96 MB

Nội dung

418 Chapter 8  Managing Consistency and Concurrency In the following example, you lock the EMPLOYEES and DEPARTMENTS tables at the highest possible level, EXCLUSIVE: SQL> lock table hr.employees, hr.departments 2 in exclusive mode; Table(s) Locked. Until the transaction with the LOCK statement either commits or rolls back, only queries are allowed on the EMPLOYEES or DEPARTMENTS tables. In the sections that follow, we will review the lock modes, as well as show you how to avoid the lock enqueue process and terminate the command if the requested resource is already locked. Lock Modes Lock modes provide a way for you to specify how much and what kinds of access other users have on tables that you are using in DML commands. In Table 8.2, you can see the types of locks that can be obtained at the table level. Manual lock requests wait in the same queue as implicit locks and are satisfied in a first in, first out (FIFO) manner as each request releases the lock with either an implicit or explicit COMMIT or ROLLBACK. TABLE 8.2 Table Lock Modes Table Lock Mode Description ROW SHARE Permits concurrent access to the locked table, but prohibits other users from locking the entire table for exclusive access. ROW EXCLUSIVE Same as ROW SHARE, but also prohibits locking in SHARE mode. This type of lock is obtained automatically with standard DML commands such as UPDATE, INSERT, or DELETE. SHARE Permits concurrent queries but prohibits updates to the table; this mode is required to create an index on a table and is automatically obtained when using the CREATE INDEX statement. SHARE ROW EXCLUSIVE Used to query a whole table and to allow other users to query the table, but to prevent other users from locking the table in SHARE mode or updating rows. EXCLUSIVE The most restrictive locking mode; permits queries on the locked table but prohibits any DML by any other users. This mode is required to drop the table and is automatically obtained when using the DROP TABLE statement. 4367.book Page 418 Monday, October 4, 2004 2:19 PM Monitoring Locking and Resolving Lock Conflicts 419 You can explicitly obtain locks on individual rows by using the SELECT … FOR UPDATE state- ment, as you can see in the following example: SQL> select * from hr.employees 2 where manager_id = 100 3 for update; This query not only shows the rows that satisfy the query conditions, it also locks the selected rows and prevents other transactions from locking or updating these rows until a COMMIT or a ROLLBACK occurs. NOWAIT Mode Using NOWAIT in a LOCK TABLE statement returns control to the user immediately if any locks already exist on the requested resource, as you can see in the following example: SQL> lock table hr.employees 2 in share row exclusive mode 3 nowait; lock table hr.employees * ERROR at line 1: ORA-00054: resource busy and acquire with NOWAIT specified SQL> This is especially useful in a PL/SQL application if an alternate execution path can be fol- lowed if the requested resource is not yet available. NOWAIT can also be used in the SELECT … FOR UPDATE statement. Detecting and Resolving Lock Conflicts Although locks are a common and sometimes unavoidable occurrence in many databases, they are usually resolved by waiting in the queue. In some cases, you may need to resolve the lock problem manually (for example, if a user makes an update at 4:59 P.M. and does not perform a COMMIT before leaving for the day). In the next few sections, we will describe in more detail some of the reasons that lock con- flicts occur and how to detect lock conflicts and discuss a more specific and serious type of lock conflict: a deadlock. Understanding Lock Conflicts In addition to the proverbial user who makes a change at 4:59 P.M. and forgets to perform a COMMIT before leaving for the day, other more typical lock conflicts are caused by long-running transactions that perform hundreds, thousands, or even hundreds of thousands of DML commands in the 4367.book Page 419 Monday, October 4, 2004 2:19 PM 420 Chapter 8  Managing Consistency and Concurrency overnight batch run but are not finished updating the tables when the normal business day starts. The uncommitted transactions from the overnight batch jobs may lock tables that need to be updated by clerical staff during the business day, causing a lock conflict. Another typical cause of lock conflicts is using unnecessarily high locking levels. In the side- bar “Packaged Applications and Locking” earlier in this chapter, we described a third-party application that routinely locked resources at the table level instead of at the row level to be compatible with every SQL-based database on the market. Developers may unnecessarily code updates to tables with higher locking levels than required by Oracle 10g. Detecting Lock Conflicts Detecting locks in Oracle 10g using the EM Database Control makes your job easy; no need to query against V$SESSION, V$TRANSACTION, V$LOCK, and V$LOCKED_OBJECT to see who is lock- ing what resource. In Figure 8.5, you can see the tables locked by the user SCOTT after executing the following statement: SQL> lock table hr.employees, hr.departments 2 in exclusive mode; Table(s) Locked. FIGURE 8.5 The Database Locks screen in EM Database Control 4367.book Page 420 Monday, October 4, 2004 2:19 PM Monitoring Locking and Resolving Lock Conflicts 421 SCOTT has an EXCLUSIVE lock on both the EMPLOYEES and DEPARTMENTS table. You can drill down on the locked object by clicking one of the links in the Object Name column; similarly, you can review other information about SCOTT’s session by clicking one of the links in the Ses- sion ID column. Understanding and Resolving Deadlocks Resolving a lock conflict, the user can either COMMIT or ROLLBACK the current transaction. If you cannot contact the user and it is an emergency, you can select the session holding the lock, and click the Kill Session button in the Database Locks screen of the EM Database Control (refer to Figure 8.5, earlier in this chapter). The next time the user whose session has been killed tries to execute a command, the error message ORA-00028: Your session has been killed is returned. Again, this is an option of last resort: all the statements executed in the session since the last COMMIT are lost. User Education, Locking, and Error Messages Some of our users who updated their tables using the SQL> command prompt instead of the appli- cation would come back from lunch, try to continue their work, and find that they had received an ORA-00028: Your session has been killed error message, which usually initiated a heated dis- cussion with the DBA about lost work due to their session being canceled without notice. At first, the users thought that the DBA group was either cleaning up unused connections man- ually or that a new automatic resource management policy was in place, because the details for this error message did not explain why the session was cancelled: Cause A privileged user has killed your session and you are no longer logged on to the database. Action Log in again if you want to continue working. As it turns out, the users were not always performing a COMMIT before they left for lunch; the other users who were trying to finish their work could not complete their updates because the rows of the tables were still locked in a transaction that had not yet been committed. They called the DBA, who identified the locking sessions and canceled them, generating the ORA-0002 message for the canceled session. Oracle error messages are not always clear, and the detailed description of the error message doesn’t always help, but at least it provides a starting point for investigating a problem. Make sure that the users can access the Oracle error messages, either via the Internet at www.oracle.com or via an internal shared directory containing all the Oracle documentation for the installation options at your site. 4367.book Page 421 Monday, October 4, 2004 2:19 PM 422 Chapter 8  Managing Consistency and Concurrency A more serious type of lock conflict is a deadlock. A deadlock is a special type of lock con- flict in which two or more users are waiting for a resource locked by the other users. As a result, neither transaction can complete without some kind of intervention: the session that first detects a deadlock rolls back the statement waiting on the resource with the error mes- sage ORA-00060: Deadlock detected while waiting for resource. In Table 8.3, two sessions are attempting to update a row locked by the other session. After the error message is issued at 11:45, the second UPDATE for Session 1 does not succeed; however, the second UPDATE for Session 2 completes, and the user in Session 2 can now submit another DML statement or issue a COMMIT or ROLLBACK. The user in Session 1 will have to re- issue the second UPDATE. Summary In this chapter, we presented the undo tablespace and its importance for the two types of database users: those who want to query a table and receive consistent results, and those who want to make changes to a table and have the option to roll back the data to its state when the transaction started. The undo tablespace provides undo information, or the value of rows in a table before changes were made, for both classes of users. More specifically, undo data facilitates rollback operations, read consistency, certain database recovery operations, and several types of flashback features, some of which were introduced in Oracle9i and greatly expanded in Oracle 10g. An undo tablespace can be configured with a handful of initialization parameters: UNDO_ MANAGEMENT to define the mode in which undo is managed, with values of either MANUAL or AUTO. The UNDO_TABLESPACE parameter identifies the current undo tablespace, which can be switched while the database is open to users; however, only one undo tablespace can be active at a time. You can use the EM Database Control to both proactively monitor and resize the undo tablespace, before you get the phone call from the user whose transactions are failing or SELECT statements are not completing. For databases whose long-running queries have priority over successful DML transactions, you can specify that an undo tablespace retain expired undo information at the expense of failed transactions. TABLE 8.3 Deadlock Scenario Session 1 Time Session 2 update employees set salary = salary * 1.2 where employee_id = 102; 11:29 update employees set manager = 100 where employee_id = 109; update employees set salary = salary * 1.2 where employee_id = 109; 11:44 update employees set manager = 100 where employee_id = 102; ORA-00060: Deadlock detected while waiting for resource 11:45 Control returns to user 4367.book Page 422 Monday, October 4, 2004 2:19 PM Exam Essentials 423 In the second part of the chapter, we showed you how to monitor resource locks within a transaction, both at the row level and the table level. Although Oracle usually manages locks at the minimum level to ensure that two sessions do not try to simultaneously update the same row in a table, you can explicitly lock a table at a number of levels. In addition, you can lock a subset of rows in a table to prevent updates or locks from other transactions with the FOR UPDATE clause in the SELECT statement. Finally, we presented some reasons that lock conflicts occur and how to resolve them; a spe- cial kind of lock conflict, called a deadlock, occurs when two users are waiting on a resource locked by the other user. Deadlocks, unlike other types of lock conflicts, are resolved quickly and automatically by Oracle long before any manual lock resolution is attempted. Exam Essentials Know the purpose of the Undo Advisor. Optimize the UNDO_RETENTION parameter as well as the size of the undo tablespace by using Undo Advisor. Use the graph on the Undo Advisor screen to perform what-if analyses given the undo retention requirements. Be able to monitor locking and resolve lock conflicts. Identify the reasons for database lock conflicts, and explain how to resolve them. Show an example of a more serious type of lock con- flict, a deadlock. List the features supported by undo data in an undo tablespace. Enumerate the four primary uses for undo data: rollback, read consistency, database recovery, and flashback operations. Show how the rollback requirements for users that perform long transactions can interfere with read consistency required for query users. Be able to identify and use the method to preserve expired undo at the expense of transactions. Summarize the steps for monitoring, configuring, and administering the undo tablespace. Set the initialization parameters required to use an undo tablespace. Be able to review the status of the undo tablespace using EM Database Control, and use the Undo Advisor to resize the undo tablespace when conditions warrant it. Alter the initialization parameter UNDO_RETENTION to configure how long undo information needs to be retained for long-running queries. List the types of lock modes available when locking a table. Identify the locks available, from least restrictive to most restrictive. Be able to request a lock with either a LOCK or SELECT statement and return immediately if the lock is not available. 4367.book Page 423 Monday, October 4, 2004 2:19 PM 424 Chapter 8  Managing Consistency and Concurrency Review Questions 1. What will be the salary of employee number 189 at the completion of the following SQL statements? update emp set salary = 1000 where employee_num = 189; savepoint save_1; update emp set salary = salary * 1.1 where employee_num = 189; savepoint save_2; update emp set salary = salary * 1.1 where employee_num = 189; savepoint save_3; rollback to savepoint save_2; commit; update emp set salary = 1500 where employee_num = 189; savepoint save_4; rollback to save_4; commit; A. 1000 B. 1100 C. 1111 D. 1500 2. Which of the following commands returns an error if the transaction starts with SET TRANSACTION READ ONLY? A. ALTER SYSTEM B. SELECT C. ALTER USER D. SET ROLE 3. Which of the following commands is most likely to generate an error message? (Choose two.) A. ALTER SYSTEM SET UNDO_MANAGEMENT=AUTO SCOPE=MEMORY; B. ALTER SYSTEM SET UNDO_MANAGEMENT=AUTO SCOPE=SPFILE; C. ALTER SYSTEM SET UNDO_MANAGEMENT=MANUAL SCOPE=MEMORY; D. ALTER SYSTEM SET UNDO_MANAGEMENT=MANUAL SCOPE=SPFILE; E. ALTER SYSTEM SET UNDO_TABLESPACE=RBS1 SCOPE=BOTH; 4. Guaranteed undo retention can be specified for which of the following objects? A. A tablespace B. A table C. The database D. A transaction E. The instance 4367.book Page 424 Monday, October 4, 2004 2:19 PM Review Questions 425 5. Which dynamic performance view can help you adjust the size of an undo tablespace? A. V$UNDOSTAT B. V$ROLLSTAT C. V$SESSION D. V$ROLLNAME 6. Which of the following lock modes permits concurrent queries on a table but prohibits updates to the locked table? A. ROW SHARE B. ROW EXCLUSIVE C. EXCLUSIVE D. SHARE ROW EXCLUSIVE E. SHARE 7. The highest level at which a user can request a lock is the ________ level. A. Schema B. Table C. Row D. Block 8. In the following scenario, two different transactions are updating rows in the same table. What happens at 11:45? (Choose the best answer.) A. One of the users calls the DBA who immediately kills one of the sessions holding the lock. B. The transactions in both Session 1 and Session 2 are both rolled back after both sessions receive an ORA-00060: Deadlock detected while waiting for resource message, and the statements in both transactions must be re-executed, but no other work is lost. C. Both Session 1 and Session 2 are killed by Oracle with an ORA-00028: Your session has been killed message and must redo all other statements executed since the last COMMIT. D. Session 1 generates an ORA-00060: Deadlock detected while waiting for resource message and rolls back the transaction. The user in Session 2 is then free to roll back or com- mit their transaction. Session 1 Time Session 2 update employees set salary = salary * 1.2 where employee_id = 102; 11:29 update employees set manager = 100 where employee_id = 109; update employees set salary = salary * 1.2 where employee_id = 109; 11:44 update employees set manager = 100 where employee_id = 102; ? 11:45 ? 4367.book Page 425 Monday, October 4, 2004 2:19 PM 426 Chapter 8  Managing Consistency and Concurrency 9. To retrieve the rollback segment name assigned to a transaction, you can join the dynamic per- formance view V$TRANSACTION to which other dynamic performance view? A. V$ROLLSTAT B. V$ROLLNAME C. V$UNDOSTAT D. V$TRANSACTION_ENQUEUE 10. Select the statement that is not true regarding undo tablespaces. A. Undo tablespaces will not be created if they are not specified in the CREATE DATABASE command. B. Two undo tablespaces can be active if a new undo tablespace was specified and the old one contains pending transactions. C. You can switch from one undo tablespace to another while the database is online. D. UNDO_MANAGEMENT cannot be changed dynamically while the instance is running. 11. To resolve a lock conflict, which of the following methods can you use? (Choose two.) A. Oracle automatically resolves the lock after a short but predefined time period by killing the session that is holding the lock. B. The DBA can kill the session holding the lock. C. The user can either roll back or commit the transaction that is holding the lock. D. Oracle automatically resolves the lock after a short but predefined period by killing the ses- sion that is requesting the lock. 12. If all extents in an undo segment fill up, which of the following occurs next? (Choose all that apply.) A. A new extent is allocated in the undo segment if all existing extents still contain active trans- action data. B. Other transactions using the segment are moved to another existing segment with enough free space. C. A new undo segment is created, and the transaction that filled up the undo segment is moved in its entirety to another undo segment. D. The first extent in the segment is reused if the undo data in the first extent is not needed. E. The transaction that filled up the undo segment spills over to another undo segment. 13. Which of the following commands returns control to the user immediately if a table is already locked by another user? A. LOCK TABLE HR.EMPLOYEES IN EXCLUSIVE MODE WAIT DEFERRED; B. LOCK TABLE HR.EMPLOYEES IN SHARE MODE NOWAIT; C. LOCK TABLE HR.EMPLOYEES IN SHARE MODE WAIT DISABLED; D. LOCK TABLE HR.EMPLOYEES IN EXCLUSIVE MODE NOWAIT DEFERRED; 4367.book Page 426 Monday, October 4, 2004 2:19 PM Review Questions 427 14. Two transactions occur at the wall clock times in the following table. What happens at 10:05? A. Session 2 will wait for Session 1 to commit or roll back. B. Session 1 will wait for Session 2 to commit or roll back. C. A deadlock will occur, and both sessions will hang unless one of the users cancels their state- ment or the DBA kills one of the sessions. D. A deadlock will occur, and Oracle will cancel one of the statements. E. Neither session is updating the same column, so no waiting or deadlock will occur. 15. Undo information falls into all the following categories except for which of the following? A. Uncommitted undo information B. Undo information required in case an instance crash requires a roll forward operation when the instance is restarted C. Committed undo information required to satisfy the undo retention interval D. Expired undo information that is no longer needed to support a running transaction 16. Undo segments are owned by which user? A. SYSTEM B. The user that initiated the transaction C. SYS D. The user that owns the object changed by the transaction Session 1 Time Session 2 update customer set region = ‘H’ where state=’WI’ and county=’GRANT’; 9:51 9:59 update customer set mgr=201 where state=’IA’ and county=’JOHNSON’; update customer set region=’H’ where state=’IA’ and county=’JOHNSON’; 10:01 10:05 update customer set mgr=201 where state=’WI’ and county=’GRANT’; 4367.book Page 427 Monday, October 4, 2004 2:19 PM [...]... by clicking the Collection Level link Clicking this link opens the Initialization Parameters screen in which you can specify any of the three pre-defined collection levels shown in Table 9.1 Figure 9.2 shows the AWR collection level being changed from TYPICAL to ALL FIGURE 9.1 Setting AWR statistics collection and retention using EM FIGURE 9.2 Changing the AWR statistics collection level Proactive Database... Network: http://otn .oracle. com/ deploy/availability /pdf/ OOW2000_same_ppt .pdf Figure 9.4 also shows that a large portion of our I/ O problems are related to specific database tables or indexes: “Individual database segments responsible for significant physical I/ O were found.” Clicking this link displays the detailed ADDM findings shown in Figure 9.6 ADDM has essentially identified the SALES_HISTORY table as... proactive monitoring The monitoring tools available in EM Database Control collect their information from a variety of sources (usually the same sources from which your existing third-party tools and home-grown scripts collect their monitoring information): data dictionary views, dynamic performance views, and the operating system Oracle 10g also makes extensive use of the costbased optimizer statistics... retention setting Chapter 9 Proactive Database Maintenance and Performance Monitoring ORACLE DATABASE 10G: ADMINISTRATION I EXAM OBJECTIVES COVERED IN THIS CHAPTER: Performance Monitoring Troubleshoot invalid and unusable objects Gather optimizer statistics View performance metrics React to performance issues Proactive Maintenance Set warning and critical alert thresholds Collect and use baseline metrics... tuning and diagnostic advisors Use the Automatic Database Diagnostic Monitor (ADDM) Manage the Automatic Workload Repository Describe server generated alerts Exam objectives are subject to change at any time without prior notice and at Oracle s sole discretion Please visit Oracle s Training and Certification website (http://www .oracle. com/education/certification/) for the most current exam objectives... Data Dictionary Views View Name Description DBA_ADVISOR_FINDINGS Describes the findings identified by the ADDM analysis DBA_ADVISOR_OBJECTS Describes the objects that are referenced in the ADDM findings and recommendations DBA_ADVISOR_RECOMMENDATIONS Describes the recommendations made based on ADDM findings DBA_ADVISOR_RATIONALE Describes the rationale behind each ADDM finding The following SQL statement... after selecting the analysis options displays the scheduling options Like previous EM Database Control scheduling screens, the time that the analysis will begin, its duration, and its frequency can all be defined on this page Finally, clicking Next displays the SQL Access Advisor: Review screen, as shown in Figure 9.24 This screen displays all the options that you specified before clicking Submit to actually... specify the job submission elements, click OK to begin analyzing the specified statement Choosing the Comprehensive level of analysis can be time-consuming and resource intensive Proactive Database Maintenance FIGURE 9.19 449 The SQL Tuning Advisor job scheduling screen A screen similar to the one shown in Figure 9.20 is displayed once the SQL Tuning Advisor completes its analysis of the specified... schema This utility is discussed in the next section The SQL Access Advisor You’ve seen how you can use the SQL Tuning Advisor to identify and create an index to minimize the DB Time for a particular statement The SQL Access Advisor provides additional support for finding potential schema modifications that you can use to reduce the amount of I/ O, CPU, and wait time for a given SQL statement Figure 9.22... user I/ O activity is experiencing waits for the database event “db file scattered read.” This event is caused by the I/ O activity that occurs when Oracle experiences a wait while performing a sequential disk read of contiguous blocks from a datafile into the buffer cache—usually when a table is being accessed using a full table scan or fast full index scan FIGURE 9.7 Sessions: Waiting And Working section . requests wait in the same queue as implicit locks and are satisfied in a first in, first out (FIFO) manner as each request releases the lock with either an implicit or explicit COMMIT or ROLLBACK. TABLE. objectives are subject to change at any time without prior notice and at Oracle s sole discretion. Please visit Oracle s Training and Certification website ( http://www .oracle. com/education/certification/ . work by waking up every 60 minutes and gathering statistical information from the data dictionary views, dynamic performance views, and optimizer and then storing this information in the database.

Ngày đăng: 07/08/2014, 11:22

TỪ KHÓA LIÊN QUAN