Ta có được hiển thị và thao tác trên bảng nhanh hơn Các yếu tố • Nhằm cải thiện hiệu năng • Có thể tạo trên 1 hay nhiều cột • Cập nhật tự độngkhi thêm/xóa • Có thể lập, hủy chỉ mục mà kh
Trang 1CHƯƠNG II: QUẢN LÝ DỮ LIỆU TRONG CSDL PHÂN TÁN (số tiết 4 )
• Lập chỉ mục
2.1.1 Tổng quan ( Chỉ mục là gì?Tại sao lập chỉ mục)
Là một cấu trúc tùy chọn cho các bảng hay các bó dữ liệu Nó tăng tốc quá trình làm việc với bảng chỉ với các truy vấn đơn giản Ta có được hiển thị và thao tác trên bảng nhanh hơn
Các yếu tố
• Nhằm cải thiện hiệu năng
• Có thể tạo trên 1 hay nhiều cột
• Cập nhật tự động(khi thêm/xóa)
• Có thể lập, hủy chỉ mục mà không ảnh hưởng đến bảng
Các loại chỉ mục
• B-tree indexes: the default and the most common
• B-tree cluster indexes: defined specifically for cluster
• Hash cluster indexes: defined specifically for a hash cluster
• Global and local indexes: relate to partitioned tables and indexes
• Reverse key indexes: most useful for Oracle Real Application Clusters
applications
• Bitmap indexes: compact; work best for columns with a small set of values
• Function-based indexes: contain the precomputed value of a function/expression
• Domain indexes: specific to an application or cartridge
Chi tiết (Các kiểu Index: B-tree và bitmap)
• Cấu trúc B-tree : B-tree có các giá trị lưu trong cây cân bằng cho phép tìm kiếm nhị phân nhanh
o Root-Các nhánh(nhiều)-Các lá với Index—Keycolumn length—Key column value—ROWID
o Tìm thêm nguyên lý và các giá trị
Trang 2• Cấu trúc Bitmap: Bitmap: có bitmap cho cac giá tri dữ liệu, sẽ được đối sánh khi tìm kiếm Có tác dụng khi cột có giá trị nhỏ(VD: Nam và Nũ)
o Giá trị khóa(cặp độ dài-giá trị=giá trị khóa)—Start ROWID—End ROWID—bitmap segment
o Nguyên lý: Dùng B-Tree để định vị các segment bitmap đã cho của cây
Các tùy chọn Index
- Unique index
- Sắp xếp tăng( hay giảm)
- Index khóa đảo ngược (các giá trị byte đảo)
- Index theo Khóa phức hợp: nhiều cột
- Index dựa trên hàm
- Index nén khi có nhiều khóa lặp lại
==
Chỉ mục hoạt động thế nào
Xử lý C/S Linh hoạt, liên tác, quy mô Tính phức tạp, giá thành phát
triển cao, bài toán liên tác
Xử lý CSDL
song song
Tăng tốc độ, mạnh, quy mô cho cải thiện hiệu năng dự đoán
Báì toán liên tác, giá thành cao
CSDL PT Điều khiển cục bộ dữ liệu, cải thiện
hiệu năng, giảm giá thành truyền thông, tăng độ tin cậy
Tính phức tạp cao, bổ sung yếu
tố an ninh
Scale up tăng cường
• Managing Database Links
• Viewing Information About Database Links
• Creating Location Transparency
• Managing Statement Transparency
• Managing a Distributed Database: Scenarios
Định nghĩa
Các loại
Trang 3Những vấn đề khi xây dựng và sử dụng chỉ mục
• Tạo chỉ mục
Tạo chỉ mục sau khi có dữ liệu bảng
oChỉ mục yêu cầu không gian nhớ không lớn
o Các giá trị tương đối duy nhất trong cột
o Có phạm vi giá trị rông (tốt khi dùng chỉ mục thông thường)
o Có phạm vi gtri nhỏ( tốt cho chỉ mục bitmap)
o Các cột chứa giá trị null, nhưng truy vấn chọn mọi hàng có giá trị Ví dụ truy vấn
• Hủy chỉ mục
• Thay đổi chỉ mục
• Xem thông tin chỉ mục
2.1.2 Trường hợp nghiên cứu
2.1.2.1 Chỉ mục trong Oracle(Các khái niệm)
Trong Oracle
Tạo chỉ mục sau khi có dữ liệu bảng
Trang 4o Có thể có được nhờ SQL*Loader hay import Điều này nhanh hơn khi tạo
dữ liệu rồi mới tạo chỉ mục
o Chỉ mục yêu cầu không gian nhớ không lớn
o Có thể dùng mặc định hay qua tham số Sort_AREA_SIZE
Lập chỉ mục đúng các bảng và các cột chỉ nên lập chỉ mục khi cần
Có thể dùng các cột ứng viên cho chỉ mục
o Các giá trị tương đối duy nhất trong cột
o Có phạm vi giá trị rông (tốt khi dùng chỉ mục thông thường)
o Có phạm vi gtri nhỏ( tốt cho chỉ mục bitmap)
o Các cột chứa giá trị null, nhưng truy vấn chọn mọi hàng có giá trị Ví dụ truy vấn :
WHERE COL_X > -9.99 * power(10,125)
Lúc đó thay bằng : WHERE COL_X IS NOT NULL
Các cột không nên chọn làm chỉ mục:
o Có nhiều giá trị null trong cột mà không tìm kiếm các giá trị not null
o Không lập chỉ mục cho cột LONG and LONG RAW
Kích thứơc của lối vào chỉ mục đơn không vượt quá một nửa của không gian có sẵn trong data block
Lập thứ tự cột chỉ mục với hiệu năng
Thứ tự cột lập chỉ mục rất quan trọng, nên lập chỉ mục cho cột hay dùng nhất đầu tiênNếu lập cho col1,col2,col3 thì chỉ có tác dụng bắt đầu từ col1, sau đó đến col2 không có làm với col2, col3
Hạn chế số lượng chỉ mục cho từng bảng: (Khi thêm hủy các hàng, sửa nội dung hàng thì cập nhật chỉ mục)
Cần cân nhắc việc lập chỉ mục Lợi nhất khi các cột lập chỉ mục là read-only
Trang 5
o Hủy các chỉ mục không cần thiết
o Nếu không tăng tốc truy vấn, bảng nhỏ
o Truy vấn không dùng index
o Index phải xóa trước khi phải rebuit
Dự đoán kích thứơc thiết lập các thâm số bộ nhớ
Dự đoán không gian bảng, kích thước index, undo
Làm cho index bố trí liên tục, tiết kiệm thời gian IO
Nên có kích thước bằng một nửa datablock
Đặc tả không gian bảng cho mỗi chỉ mục
Nếu dùng chung không gian bảng thì thuận tiện hơn, đảm bảo các ứng dụng giành sẵn(online)
Nếu dùng khác đĩa thì hiện năng tốt hơn nhưng khó khăn khi offline, việc tham chiếu sẽ khó khăn
Xem xét việc song song tạo chỉ mục
• Có thể song song việc tạo chỉ mục như song song việc tạo các bảng, việc này thực hiện nhanh hơn
• Tham số bộ nhớ được dùng tách rời cho mỗi tiến trình server Giá trị khởi đầu INITIAL là 5M và song song 12 cột ít nhất cần 60M khi tạo chỉ mục
Xem xét tạo chỉ mục với NOLOGGING
Tạo index và phát sinh các bản ghi undo tối thiểu bằng cách đặc tả NOLOGGING trong câu lệnh CREATE INDEX (nếu dùng NOLOGGING thì nên backup sau khi tạo index)Khi dùng NOLOGGING có cái lợi
• Tiết kiệm không gian trong các file redo
• Giảm thời gian tạo index
• Hiệu năng cao do song song việc tạo index lớn
Xem xét giá thành và lợi nhuận khi gắn kết hay dựng lại chỉ mục
Trang 6Khi index lớn thì có thể bị phân mảnh Để loại bỏ có thể rebuid hay coalesie Cần cân nhắc trọng số khi nào thì dùng đúng Xem bảng
Table 16-1 To Rebuild or Coalesce That Is the Question
Chuyển nhanh dữ liệu đến tablespace khác Không thể chuyển index đến tablespace
khác
Giá thành cao Giá thành thấp: không yêu cầu không gian
đĩa nhiều hơn
Tạo cây mới, thu ngắn độ cao nếu cần Gắn kết các khối là trong cùng một nhánh
cây
Cho phép thay đổi nhanh tham số bộ nhớ và
không gian bảng mà không hề phải hủy index
ALTER INDEX vmoore COALESCE;
Figure 16-1 minh họa ALTER INDEX COALESCE trên index vmoore Trước khi thực hiện thao tác, 2 khối lá đầu đầy 50% Như vậy ta có cơ hội giảm nhỏ phân mảnh và lấp đầy khối đầu trong khi giải phóng khối thứ hai
Figure 16-1 Coalescing Indexes
Trang 7Xem xét giá thành trước khi vô hiệu hóa hay hủy ràng buộc
Vì khóa chính và unique quan hệ với index nên cần cân nhắc khi hủy hay vô hiệu ràng buộc UNIQUE or PRIMARY KEY
Nếu ràng buộc lớn thì có thể tkiệm thời gian bỏ lại hiệu lực UNIQUE or PRIMARY KEY hơn là hủy
Cần có tùy chọn tường minh cho ràng buộc UNIQUE or PRIMARY KEY
2.1.2.2 Quản trị chỉ mục
a Tạo chỉ mục
Để tạo index Tối thiểu cần nhất các điều kiện
• Bảng hay bó được index trong lược đồ
• Có quyền INDEX trên bảng index
• Có quyền hệ thống CREATE ANY INDEX
Để tạo index trên lược đồ khác, tối thiểu cần
• Có quyền hệ thống CREATE ANY INDEX
• Người sử hữu lược đồ khác có quota cho không gian bảng để ràng buộc index và các phân chương index hay quyền hệ thống UNLIMITED TABLESPACE
Tạo chỉ mục tường minh
Có thể tạo chỉ mục tường minh(ngoài ràng buộc toàn vẹn) dùng câu lệnh SQL CREATE INDEX Vd: tao index tên emp_ename cho cột ename của bảng emp
CREATE INDEX emp_ename ON emp(ename)
Trang 8 Tạo chỉ mục tường minh đơn nhất(unique)
Khái niệm unique và non-unique
Dùng CREATE UNIQUE INDEX để tạo index UNIQUE:
CREATE UNIQUE INDEX dept_unique_index ON dept (dname)
• Tạo chỉ mục liên kết với ràng buộc
CSDL Oracle buộc một khóa UNIQUE hay ràng buộc toàn vẹn PRIMARY KEY trên một bảng khi tạo một một index unique hay khóa primary Index này tự động được tạo bởi CSDL khi ràng buộc cho phép Không hành động nào được yêu cầu khi công bố CREATE TABLE hay ALTER TABLE để tạo index, nhưng ta có thể đặc tả USING INDEX để điều khiển qua việc tạo bảng này Điều này bao gồm cả khi một ràng buộc được định nghĩa và cho phép và khi định nghái nhưng không cho phép
Để cho phép ràng buộc UNIQUE hay PRIMARY KEY, như vậy phải tạo một index liên quan, người sở hữu bảng phải có quotacho không gian bảng hướng đến index hay quyền
hệ thống UNLIMITED TABLESPACE Index liên quan đến ràng buộc thường lấy tên của ràng buộc trừ phi tùy chọn được đặc tả khác đi
B1: Đặc tả tùy chọn lưu trữ cho một chỉ mục kết nối với một ràng buộc(unique hay
Primary key)-Mệnh đề ENABLE PRIMARY KEY/UNIQUE
Tùy chọn ràng buộc UNIQUE and PRIMARY KEY Sử dụng mệnh đề USING
INDEX Ví dụ lệnh tạo bảng sau CREATE TABLE cho phép ràng buộc PRIMARY KEY và đặc tả tùy chọn với chỉ mục:
CREATE TABLE emp (
empno NUMBER(5) PRIMARY KEY, age INTEGER)
ENABLE PRIMARY KEY USING INDEX
TABLESPACE users;
Trang 9B2: Đặc tả chỉ mục liên kết với ràng buộc
Nếu cần nhiều chỉ mục kết nối với ràng buộc UNIQUE and PRIMARY KEY CSDL cho phép:
• Đặc tả một ràng buộc đang có mà CSDL dùng nó để bắt theo ràng buộc
• Đặc tả một câu lệnh CREATE INDEX mà CSDL dùng để tạo chỉ mục và bắt theo ràng buộc
Lúc đó dùng mệnh đề USING INDEX (trong lệnh) VD:
Example 1: (tạo bảng a có a1 la khóa chính dùng chỉ mục ai theo ràng buộc a(a1) )
CREATE TABLE a (
a1 INT PRIMARY KEY USING INDEX (create index ai on a (a1)));
Example 2: (tạo bảng b có b1,b2 ràng buộc bu1 là unique dùng chỉ mục bi theo b1,b2 ràng buộc bu2 là unique theo b1,b2 dùng index bi)
CREATE TABLE b(
b1 INT,
b2 INT,
CONSTRAINT bu1 UNIQUE (b1, b2)
USING INDEX (create unique index bi on b(b1, b2)),
CONSTRAINT bu2 UNIQUE (b2, b1) USING INDEX bi);
Example 3:(Tạo bảng c có c1,c2 tạo chỉ mục ci theo c1,c2 thêm vào bảng ràng buộc cpk theo primary c1 dùng index ci)
CREATE TABLE c(c1 INT, c2 INT);
CREATE INDEX ci ON c (c1, c2);
ALTER TABLE c ADD CONSTRAINT cpk PRIMARY KEY (c1) USING INDEX ci;Nếu câu lệnh tạo một index với một ràng buộc và cũng dùng index cho ràng buộc khác,
hệ thống sẽ sắp xếp lại mệnh đề để tạo index trước khi tái sử dụng nó
• Thu thập các thống kê ngẫu nhiên khi tạo chỉ mục
Cho cơ hội thu thập thông tin thống kế ở giá thành tài nguyên nhỏ khi tạo và tái sử dụng index Các thống kê này được lưu trong từ điển dữ liệu cho việc sử dụng tới bằng tối ưu việc chọn một kế hoạch cho thực hiện lệnh SQL VD: Tính toán index, bảng,thống kê các cột khi xây dựng index emp_ename trên cột ename của bảng emp:
CREATE INDEX emp_ename ON emp(ename)
COMPUTE STATISTICS;
b Thay đổi chỉ mục
Trang 10Để thay đổi index Tối thiểu cần nhất các điều kiện
• Lược đồ phải chứa chỉ mục
• Có quyền hệ thống ALTER ANY INDEX trên bảng index
Các hoạt động
• Xây dựng lại hay thống nhất một chỉ mục hiện có
• Hủy định vị các không gian không dùng và định vị lại một mở rộng mới
• Đặc tả vận hành song song/hoặc không và mứa độ song song
• Thay đổi các tham số lưu trữ hay thuộc tính vật lý
• Đặc tả LOGGING hay NOLOGGING
• Cho phép hay hủy các khóa nén
• Đánh dấu các chỉ mục không dùng
• Khởi động hay dừng việc giám sát việc dùng chỉ mục
Ta không thể thay đổi cấu trúc cột chỉ mục(thứ tự??)
Nội dung khảo sát:
• Altering Storage Characteristics of an Index
• Rebuilding an Existing Index
• Monitoring Index Usage
Thay đổi các đặc tính lưu trữ của chỉ mục(Lệnh ALTER INDEX)
Thay đổi các tham số của bất kỳ chỉ mục nào bao gồm những gì được tạo bởi CSDL để bắt các ràng buộc toan vẹn primary key hay unique dùng lệnh ALTER INDEX VD: lệnh thay đổi chỉ mục emp_ename:
ALTER INDEX emp_ename
STORAGE (PCTINCREASE 50);
Các tham số INITIAL and MINEXTENTS không thể thay đổi và được xác định ngay từ đầu Mọi thiết lập tham số chỉ ảnh hưởng đến các mở rộng (extent) định vị cho chỉ mục
Trang 11Để thay đổi các ràng buộc toàn vẹn, ta có thể chỉnh các tham số lưu trữ bằng cách dùng câu lệnh ALTER TABLE gồm mệnh đề con USING UNDEX của mệnh đề ENABLE, VD: Ví dụ hiệu chỉnh tùy chọn lưu trữ của bảng emp theo ràng buộc primary key:
ALTER TABLE emp
ENABLE PRIMARY KEY USING INDEX;
Thay đổi có Xây dựng lại một chỉ mục đang có (Mệnh đề REBUILD)
Trước khi xây dựng lại một chỉ mục đã có, cần cân nhắc giá thnahf (rebuid hay coalesce) theo bảng 16-1
Khi xây dựng lại một chỉ mục, ta dùng index cũ như dữ liệu nguồn Theo cách này cho phép thay đổi các đặc tính lưu trữ hay chuyển các phân đoạn giữa các khối Nó hiệu quả hơn khi bỏ chỉ mục và dùng CREAT INDEX
VD: Xây dựng lại index đang có emp_name:
ALTER INDEX emp_name REBUILD;
Mệnh đề REBUILD phải đi ngay sau tên của chỉ mục Nó không thể conjuntion với mệnh
recreating the index
Nếu không có không gian yêu cầu để xây dựng lại thì thay bằng coalesce Coalescing an index is an online operation
Thay đổi có Giám sát việc sử dụng chỉ mục (Mệnh đề MONITORING
USAGE)
CSDL Oracle cung cấp phương tiện giám sát chỉ mục khi chúng được dùng Khi một chỉ mục không được dùng, có thể hủy loại bỏ các tổng phí
Sử dụng lệnh để khởi động
Trang 12ALTER INDEX index MONITORING USAGE;
Sau đó Sử dụng lệnh để dừng giám sát:
ALTER INDEX index NOMONITORING USAGE;
Chứng cứ V$OBJECT_USAGE có thể được dùng để xem chỉ mục có được dùng không Chứng cứ chứa cột USED có giá trị (YES/NO) tùy chỉ mục có dược dùng hay không trong thời gian giám sát Chứng cứ cũng chứa cột thời gian khởi động và dừng giám sát
và cột MONITORING (YES/NO) chỉ thị giám sát hiện thời có active hay không
Mỗi lần đặc tả MONITORING USAGE, thì chứng cứ V$OBJECT_USAGE được tlap lại cho chỉ mục đặc tả CÁc giám sát trước bị xóa, reset và thời gian mới được ghi Khi ta đặc tả MONITORING USAGE, không giám sát nào được thực hiện và thời gian kết thúc được ghi lại Cho tới khi ALTER INDEX MONITORING USAGE được công bố, thông tin chứng cứ là không thay đổi
c Giám sát không gian sử dụng chỉ mục (dùng ANALYZE INDEX VALIDATE STRUCTURE sau đó SELECT PCT_USED FROM INDEX_STATS WHERE NAME= )
Nếu các khóa chỉ mục thường xuyên được đưa thêm, hủy, cập nhật thì chỉ mục sẽ mất tính hiệu quả về không gian theo thời gian Giám sát hiệu quả sử dụng không gian bằng cách trước hết phân tích cấu trúc chỉ mục dùng ANALYZE INDEX VALIDATE
STRUCTURE, sau đó truy vấn view INDEX_STATS:
SELECT PCT_USED FROM INDEX_STATS WHERE NAME = 'index';
Phần trăm không gian sử dụng chỉ mục thay đổi khi thường xuyên thêm,cập nhật hay xóa chỉ mục Giám sát tính hữu hiệu của không gian bằng cách thực hiện một số lần :
• Phân tích thống kê
• Kiểm tra tính hợp lệ chỉ mục
• Kiểm tra PCT_USED
• Hủy hay xây dựng lại(hay coalesing ) chỉ mục
• Khi nhận thấy không gian chỉ mục xuống dưới ngưỡng, ta có thể condense không gian chỉ mục bằng hủy và tái xây dựng lại (hay coalesing) nó
Trang 13d Hủy chỉ mục (Lệnh DROP INDEX)
Quyền: Để hủy, chỉ muc phải nằm trong lược đồ của ta và ta cần có quyền hệ thống
DROP ANY INDEX
Các nguyên nhân cho việc hủy chỉ mục:
• The index is no longer required
• The index is not providing anticipated performance improvements for queries issued against the associated table For example, the table might be very small, or there might be many rows in the table but very few index entries
• Applications do not use the index to query the data
• The index has become invalid and must be dropped before being rebuilt
• The index has become too fragmented and must be dropped before being rebuilt.Khi hủy một chỉ mục, toàn bộ mở rộng segment chỉ mục được trả về cho không gian bảng chứa và trở nên dành sẵn cho các đối tượng khac trong không gian bảng
Nếu tạo chỉ mục tường minh CREATE INDEX thì hủy chỉ mục bằng DROP INDEX VD: hủy chỉ mục cho emp_ename:
DROP INDEX emp_ename;
Ta không thể chỉ hủy chỉ mục liên kết với một ràng buộc khóa UNIQUE hay PRIMARY KEY Trước đó phải disable hay drop bản thân ràng buôc
Note:
Nếu bảng bị drop mọi chỉ mục kết nối sẽ tự động drop
e Xem thông tin chỉ mục
Để xem các thông tin có thể xem các chứng cứ sau:
DBA_IND_COLUMNS These views describe the columns of indexes on tables
Trang 14These views contain optimizer statistics for indexes.
INDEX_STATS Stores information from the last ANALYZE
INDEX VALIDATE STRUCTURE statement.INDEX_HISTOGRAM Stores information from the last ANALYZE
INDEX VALIDATE STRUCTURE statement.V$OBJECT_USAGE Contains index usage information produced by the ALTER
INDEX MONITORING USAGE functionality.
• Người quản trị cần lập lịch và giám sát việc các tác vụ bảo trì phát lại CSDL như backup / nightly data warehousing loads và extracts
Trang 15• Người phát triển tạo chương trình và thư viện chương trình để nsd đầu cuối co thể dùng để tạo hay giams sat công việc của họ Ngay trong công việc thông thường cũng vậy
Bộ lập lịch làm gì? Thực hiện các chức năng phức tạp của chức năng lập lịch, ta có thể
dùng để:
• Lập lịch tác vụ dựa trên thời gian và sự kiện
Chạy theo ngày giờ hay sự kiện Cho phepsgiamr giá thành điều hành (rỗi thì làm việc khác) hay gặp sự kiện nào đó
• Lập lịch xử lý theo cách mô hình doanh nghiệp yêu cầu
• Quản trị và giám sát tác vụ
• Vận hành và giám sát tác vụ trong môi trường cluster
• Schedule job execution based on time or events
• Schedule job processing in a way that models your business requirements
The Scheduler enables limited computing resources to be allocated appropriately among competing jobs, thus aligning job processing with your business needs This is accomplished in the following ways:
o Jobs that share common characteristic and behavior can be grouped into larger entities called job classes You can prioritize among the classes by controlling the resources allocated to each class This enables you to ensure that your critical jobs have priority and have enough resources to complete For example, if you have a critical project to load a data warehouse, then you can combine all the data warehousing jobs into one class and give priority to it over other jobs by allocating it a high
percentage of the available resources
o The Scheduler takes prioritization of jobs one step further, by providing you the ability to change the prioritization based on a schedule Because your definition of a critical job can change over time, the Scheduler enables you to also change the priority among your jobs over that time frame For example, you may consider the jobs to load a data warehouse
to be critical jobs during non-peak hours but not during peak hours In such a case, you can change the priority among the classes by changing the resource allocated to each class See "Creating Job Classes" and
"Creating Windows" for more information
o In addition to running jobs based on a time schedule, the Scheduler
enables you start jobs in response to system or business events Your
Trang 16applications can detect events and then signal the Scheduler Depending
on the type of signal sent, the Scheduler starts a specific job An example
of using events to align your job processing with business needs is to prepare event-based jobs for when a transaction fails, such as someone trying to withdraw more money from a bank account than is available In this case, you could run jobs that check for suspicious activity in this account
• Manage and monitor jobs
There are multiple states that a job undergoes from its creation to its completion Scheduler activity is logged and information such as the status of the job and the last run time of the job can be easily tracked This information is stored in views and can be easily queried using Enterprise Manager or a SQL query These views provide valuable information about jobs and their execution that can help you schedule and manage your jobs better For example, a DBA can easily track all jobs that failed for user scott See "Monitoring and Managing the Scheduler"
• Execute and manage jobs in a clustered environment
A cluster is a set of database instances that cooperates to perform the same task Oracle Real Application Clusters (RAC) provides scalability and reliability without any change to your applications The Scheduler fully supports execution
of jobs in such a clustered environment To balance the load on your system and for better performance, you can also specify the database service where you want
a job to run See "Using the Scheduler in Real Application Clusters
Environments" for more information
Trang 172.2.2 Trường hợp nghiên cứu(Oracle)
Để thực hiện lập lịch tự động, đơn giản quản trị tác vụ, Oracle cung cấp các chức năng
và thủ tục trong gói DBMA_SCHEDULER Chúng là bộ lập lịch, có khả năng lập trình từ PL/SQL
Có khả năng lập lịch các tác vụ thông qua Bộ lập lịch Oracle
• Overview of the Scheduler
• Basic Scheduler Concepts
• Advanced Scheduler Concepts
Có hai loại sự kiện
• Các sự kiện đưa ra bởi bộ lập lịch
• Các sự kiện đưa ra bởi một ứng dụng
• Móc xích-Chains
Một chuối chương trình kết nối các đối tượng đơn hay tổ hợp với nhau VD: chạy chương trình A rồi đến B hay Chạy A, nếu thì đến C
Trang 18• Một chương trình
• Móc xích khác(một xích lồng)
• Một sự kiện
Các Programs, Jobs, và Schedules quan hệ với nhau thế nào
Để định nghĩa vận hành cái gì và khi nào ta gán quan hệ giữa các programs, jobs, và schedules Xem hình
Các khái niệm lập lịch nâng cao
Cho phép người quản trị CSDL giám sát nhiều khía cạnh tiên tiến của lập lịch Người phát triển thì không cần
• Các lớp công việc-Job Classes
• Các cửa sổ-Windows
• Các nhóm cửa sổ- Window Groups
Job Classes
Job classes provide a way to:
• Assign the same set of attribute values to member jobs
Each job class specifies a set of attributes, such as logging level When you assign a job to a job class, the job inherits those attributes For example, you can specify the same policy for purging log entries for all payroll jobs
• Set service affinity for member jobs
You can set the service attribute of a job class to a desired database service name This determines the instances in a Real Application Clusters environment that run the member jobs, and optionally the system
resources that are assigned to member jobs See "Service Affinity when Using the Scheduler" for more information
• Set resource allocation for member jobs
Trang 19Job classes provide the link between the Database Resource Manager and the Scheduler, because each job class can specify a resource consumer group as an attribute Member jobs then belong to the specified consumer group, and are assigned resources according to settings in the current resource plan.
Alternatively, you can leave the resource_consumer_group
attribute NULL and set the service attribute of a job class to a desired database service name That service can in turn be mapped to a resource consumer group
See Chapter 24, "Using the Database Resource Manager" for more
information on mapping services to consumer groups
• Group jobs for prioritization
Within the same job class, you can assign priority values of 1-5 to
individual jobs so that if two jobs in the class are scheduled to start at the same time, the one with the higher priority takes precedence This
ensures that you do not have a less important job preventing the timely completion of a more important one
If two jobs have the same assigned priority value, the job with the earlier start date takes precedence If no priority is assigned to a job, its priority defaults to 3
Note:
Job priorities are used only to prioritize among jobs in the same class
There is no guarantee that a high priority job in class A will be started before a low priority job in class B, even if they share the same schedule Prioritizing among jobs of different classes depends on the current
resource plan and on the designated resource consumer group or service name of each job class
When defining job classes, you should try to classify jobs by functionality
Consider dividing jobs into groups that access similar data, such as marketing, production, sales, finance, and human resources
Some of the restrictions to keep in mind are:
Trang 20• A job must be part of exactly one class When you create a job, you can specify which class the job is part of If you do not specify a class, the job automatically becomes part of the class DEFAULT_JOB_CLASS.
• Dropping a class while there are still jobs in that class results in an error You can force a class to be dropped even if there are still jobs that are members of that class, but all jobs referring to that class are then
automatically disabled and assigned to the class DEFAULT_JOB_CLASS Jobs belonging to the dropped class that are already running continue to run under class settings determined at the start of the job
Windows
You create windows to change resource allocation among jobs during various time periods of the day, week, and so on A window is represented by an interval
of time with a well-defined beginning and end, such as "from 12am-6am"
Windows work with job classes to control resource allocation Each window
specifies the resource plan to activate when the window opens (becomes
active), and each job class specifies a resource consumer group or specifies a database service, which can map to a consumer group A job that runs within a window therefore has resources allocated to it according to the consumer group
of its job class and the resource plan of the window
Figure 26-2 shows a workday that includes two windows In this configuration, jobs that belong to the job class that links to Consumer Group 1 get more resources in the morning than in the afternoon The opposite is true for jobs in the job class that links to Consumer Group 2
Figure 26-2 Windows help define the resources that are allocated to jobs
Description of "Figure 26-2 Windows help define the resources that are allocated to jobs"
Trang 21See Chapter 24, "Using the Database Resource Manager" for more information
on resource plans and consumer groups
You can assign a priority to each window If windows overlap, the window with the highest priority is chosen over other windows with lower priorities The
Scheduler automatically opens and closes windows as window start times and end times come and go
A job can name a window in its schedule_name attribute The Scheduler then starts the job when the window opens If a window is already open, and a new job is created that points to that window, the job is not started until the next time the window opens
See "Creating Windows" for examples of creating and using windows
Note:
If necessary, you can temporarily block windows from switching the current resource plan For more information, see "Enabling the Database Resource Manager", or the discussion of the DBMS_RESOURCE_MANAGER.SWITCH_PLAN package procedure in Oracle
Database PL/SQL Packages and Types Reference
Window Groups
You can group windows for ease of use in scheduling jobs If a job must run during multiple time periods throughout the day, week, and so on, you can create a window for each time period, and then add the windows to a window group You can then set the schedule_name attribute of the job to the name
of this window group, and the job executes during all the time periods specified
in the window group
For example, if you had a window called "Weekends" and a window called
"Weeknights," you could add these two windows to a window group called
"Downtime." The data warehousing staff could then create a job to run queries according to this Downtime window group—on weeknights and weekends—when the queries could be assigned a high percentage of available resources
If a window in a window group is already open, and a new job is created that points to that window group, the job is not started until the next window in the window group opens
See "Creating Window Groups" for examples of creating window groups
Kiến trúc bộ lập lịch
Trang 22Figure 26-3 Scheduler Components
Mô tả
CÁc tác vụ được đkhiển bởi CSDL
• Bảng tác vụ - The Job Table
Bảng tác vụ chứa mọi tác vụ với 1 bảng trên một CSDL Bảng lưu thông tin cho mọi tác
vụ như tên người sở hữu, mức độ đăng nhập Có thể tìm thông tin này nhờ xem
*_SCHEDULER_JOBS
Các tác vụ là đối tượng CSDL và có thể tích lũy nên chiếm không gian
lớn Vì vậy tác vụ có thể tự giải phóng mặc định sau khi hoàn tất Hành vi này được điều khiển bởi thuộc tính auto_drop của tác vụ
• Bộ phối hợp công việc-The Job Coordinator
Tiến trình nền (cjqNNN) được khởi động tự động khi tác vụ bắt đầu hay các cửa sổ phải
mở Nó cũng tự động đóng sau chu kỳ…không hoạt động của bộ lập lịch Nó thực hiện:
Trang 23• Điều khiển và spawn các slaves tác vụ
• Truy vấn bảng tác vụ
• Nhặt các tác vụ trên bảng trên cơ sở thông thường và đặt chúng vào cache, nâng cao hiệu năng do không phải đến đĩa
• Lấy các tác vụ từ cache bộ nhớ và đưa đến các slave để vận hành
• Làm sạch các pool slave khi slave không cần nữa
• Nghỉ khi không còn job được lập lịch
• Đánh thức khi một job đi vào vận hành hay được tạo khi dùng thủ tục
• Các công việc vân hành thế nào-How Jobs Execute
Khi một job được lấy ra để xử lý, slave job:
1 Tập trung các metadata cần thiết để chạy job.Ví dụ: các biến chương trình hay các thông tin đặc quyền
2 Khởi đông một phiên CSDL như người sở hữu job, khởi động một giao dịch và rồi chạy job
3 Ngay sau khi job hoàn tất, slave chuyển giao và kết thúc gdich
4 Đóng phiên
• Các nô lệ công việc-Job Slaves
Chúng thường vận hành các job ta đệ trình Chúng được đánh thức/nhận thức được bởi
bộ phối hợp job khi thời điểm job được vân hành đến Chúng tập trung metadata để chạy joc từ bảng job
Trang 24Khi một job thực hiện, các slaves
• Tái lập lịch các job nếu yêu cầu
• Cập nhât bảng job để phản ánh xem liệu job đã xong hay cần được lập lịch để chạy nữa
• Đưa một lối vào trong bảng log job
• Cập nhất số đếm chạy, và nếu cần số đếm sự cố và số đếm làm lại
• Dọn dẹp
• Nhìn công việc mới(nếu không, đi nghỉ)
Các bộ lập lịch bố trí kích thước pool slave tự động nếu yêu cầu
• Sử dụng bộ lập lịch trong môi trường bó ứng dụng thực-Using the
Scheduler in Real Application Clusters Environments
Trong môi trương RAC, bộ lập lịch dùng một bảng job cho một CSDL và một bộ phối hợp job cho mỗi thể hiện CÁc bộ phối hợp job truyền thông vói nhau để giữ thông tin hiện thời CÁc bộ lập lịch muốn cân bằng tải của các job của một lớp job qua các thể hiện dành sẵn khi lớp job không có ái lực dịch vụ, hay qua các thể hiện được gán cho các dịch
vụ riêng khi lớp job có ái lực dvu
Figure 26-4 kiến trúc RAC chuẩn với mỗi bộ phối hợp thể hiện job trao đổi thông tin với cái khác
Figure 26-4 RAC Architecture and the Scheduler
Trang 25ÁI lực dvu khi dùng bộ lập lịch
Bộ lập lịch cho phép ta đặc tả dvu CSDL nào mà job sẽ chạy trên đó (ái lực dvu) Điều này đảm bảo sẵn sàng hơn ái lực thể hiện vì nó đảm bảo rằng các nút khác có thể được gán động đến dvu nếu một thể hiện down Ái lực thể hiện không có khare nang này nên khi một thể hiện down, không có job nào với ái lực thể hiện đó có thể chạy cho đến khi thể hiện đó trở nên backup Hình dưới mô tả ví dụ chuẩn của các dvu và các thể hiện được dùng như thế nào
Figure 26-5 Service Affinity and the Scheduler
Trang 26The Job Table
The Job Coordinator
How Jobs Execute
Job Slaves
Using the Scheduler in Real Application Clusters Environments
26 Scheduler Concepts
• Overview of the Scheduler
• Basic Scheduler Concepts
• Advanced Scheduler Concepts
Trang 27• Scheduler Architecture
Overview of the Scheduler
What Can the Scheduler Do?
Basic Scheduler Concepts
The Scheduler offers a modular approach for managing tasks within the Oracle environment Advantages of modularity include easier management of your database environment and reusability of scheduler objects when creating new tasks that are similar to existing tasks
In the Scheduler, most components are database objects like a table, which enables you to use normal Oracle privileges
The basic elements of the Scheduler are:
A program is a separate entity from a job Jobs run at a certain time or because
a certain event occurred, and invoke a certain program Jobs can be created that point to existing program objects, which means that different jobs can use the same program and run the program at different times and with different settings
Trang 28Given the right privileges, different users can thus use the same program without having to redefine it This enables the creation of program libraries, where users can select from a list of existing programs.
Because a Scheduler program can invoke a stored procedure or other executable that requires arguments, a means is provided to store default values for those arguments as program attributes
See "Creating Programs" for more information about programs, and "Jobs" for an overview of jobs
specified, which indicates when the schedule expires
A schedule can also specify that a job be executed when a certain event occurs, such as a badge swipe or inventory dropping below a threshold For more
information on events, see "Events"
Similar to programs, schedules are objects that can be named and saved in the database Users can then share named schedules For example, the end of a business quarter may be a common time frame for many jobs Instead having to define an end-of-quarter schedule each time a new job is defined, job creators can point to a named schedule
Some examples of schedules you might use to control time-based jobs are:
• Run on Wednesday, December 26th, 2001 at 2pm
• Run every Monday, at 8am, starting on December 26th, 2001, and ending
on January 31st, 2002
• Run on every working day
See "Creating Schedules" for more information
Jobs
A job is a user-defined task that is scheduled to run one or more times It is a combination of what needs to be executed (the action) and when (the schedule) Users with the right privileges can create jobs either by:
Trang 29• Specifying as job attributes both the action to perform (for example, an inline PL/SQL anonymous block) and the schedule by which to perform the action (for example, every day at noon, or when a certain event occurs)
• Specifying as job attributes the names of an existing program object and
an existing schedule object
Like programs and schedules, jobs are objects that can be named and saved in the database
Job Arguments
You can specify job arguments to customize a named program object Job
arguments override the default argument values in the program object, and provide values for those program arguments that have no default value In
addition, job arguments can provide argument values to an inline action (for example, a stored procedure) that the job specifies
A job cannot be enabled until all required program argument values are defined, either as defaults in a referenced program object, or as job arguments
A common example of a job is one that runs a set of nightly reports If different departments require different reports, you can create a program for this task that can be shared among different users from different departments The
program action would be to run a reports script, and the program would have one argument: the department number Each user can then create a job that points to this program, and can specify the department number as a job
When a job is created, only one entry is added to the Scheduler's job table to represent the job Each time the job runs, an entry is added to the job log
Therefore, if you create a job that has a repeating schedule, you will find one entry in the job views and multiple entries in the job log Each job instance log
Trang 30entry provides information about a particular run, such as the job completion status and the start and end time Each run of the job is assigned a unique log id which is used in both the job log and job run details views.
See "How to View Scheduler Information" for more information
Events
An event is a message sent by one application or system process to another to
indicate that some action or occurrence has been detected An event is raised (sent) by one application or process, and consumed (received) by one or more
applications or processes
There are two kinds of events in the Scheduler:
• Events raised by the Scheduler
The Scheduler can raise an event to indicate state changes that occur within the Scheduler itself For example, the Scheduler can raise an event when a job starts, when a job completes, when a job exceeds its allotted run time, and so on The consumer of the event is an application that takes some action in response to the event
For example, if due to a high system load, a job is still not started 30 minutes after the scheduled start time, the Scheduler can raise an event that causes a handler application to send a notification e-mail to the
database administrator
• Events raised by an application
An application can raise an event to be consumed by the Scheduler The Scheduler reacts to the event by starting a job You can create a schedule that references an event instead of containing date, time, and recurrence
information If a job is assigned to such a schedule (an event schedule),
the job runs when the event is raised You can also create a job that has
no schedule assigned and that directly references an event as the means
to start the job
For example, when an inventory tracking system notices that the
inventory has gone below a certain threshold, it can raise an event that starts an inventory replenishment job
The Scheduler uses Oracle Streams Advanced Queuing to raise and consume events When raising a job state change event, the Scheduler enqueues a
Trang 31message onto a default event queue Applications subscribe to this queue,
dequeue event messages, and take appropriate action When raising an event to notify the Scheduler to start a job, an application enqueues a message onto a queue that was specified when setting up the job
Each position within a chain of interdependent programs is referred to as a step Typically, after an initial set of chain steps has started, the execution of
successive steps depends on the completion of one or more previous steps Each step can point to one of the following:
Multiple steps in the chain can invoke the same program or nested chain
In a sense, a chain resembles a decision tree, with many possible paths for selecting which steps run and when A list of rules is used to decide which
actions to perform at any particular stage An example of a rule is "If step 2 fails
or step 3 fails, wait an hour and then start step 4."
While a job pointing to a chain is running, the current state of all steps of the running chain can be monitored
A typical situation where you might want to create a chain is to combine the different programs necessary for a successful financial transaction
Trang 32See "Using Chains" for more information.
How Programs, Jobs, and Schedules are Related
To define what is executed and when, you assign relationships among programs, jobs, and schedules Figure 26-1 illustrates examples of such relationships
Figure 26-1 Relationships Among Programs, Jobs, and Schedules
Description of "Figure 26-1 Relationships Among Programs, Jobs, and Schedules"
To understand Figure 26-1, consider a situation where tables are being analyzed
In this example, P1 would be a program to analyze a table using the
DBMS_STATS package The program has an input parameter for the table
name Two jobs, J1 and J2, both point to the same program, but each supplies
a different table name Additionally, schedule S1 could specify a run time of 2:00 a.m every day The end result would be that the two tables named in J1 and J2 are analyzed daily at 2:00 a.m
Note that J4 points to no other entity, so it is self-contained with all relevant information defined in the job itself P2, P9 and S2 illustrate that you can leave
a program or schedule unassigned if you want You could, for example, create a program that calculates a year-end inventory and temporarily leave it unassigned
to any job
Advanced Scheduler Concepts
Trang 33Many Scheduler capabilities enable database administrators to control more advanced aspects of scheduling Typically, these topics are not as important for application developers.
This section discusses the following advanced topics:
• Job Classes
• Windows
• Window Groups
Job Classes
Job classes provide a way to:
• Assign the same set of attribute values to member jobs
Each job class specifies a set of attributes, such as logging level When you assign a job to a job class, the job inherits those attributes For
example, you can specify the same policy for purging log entries for all payroll jobs
• Set service affinity for member jobs
You can set the service attribute of a job class to a desired database service name This determines the instances in a Real Application Clusters environment that run the member jobs, and optionally the system
resources that are assigned to member jobs See "Service Affinity when Using the Scheduler" for more information
• Set resource allocation for member jobs
Job classes provide the link between the Database Resource Manager and the Scheduler, because each job class can specify a resource consumer group as an attribute Member jobs then belong to the specified consumer group, and are assigned resources according to settings in the current resource plan
Alternatively, you can leave the resource_consumer_group
attribute NULL and set the service attribute of a job class to a desired database service name That service can in turn be mapped to a resource consumer group
Trang 34See Chapter 24, "Using the Database Resource Manager" for more
information on mapping services to consumer groups
• Group jobs for prioritization
Within the same job class, you can assign priority values of 1-5 to
individual jobs so that if two jobs in the class are scheduled to start at the same time, the one with the higher priority takes precedence This
ensures that you do not have a less important job preventing the timely completion of a more important one
If two jobs have the same assigned priority value, the job with the earlier start date takes precedence If no priority is assigned to a job, its priority defaults to 3
Note:
Job priorities are used only to prioritize among jobs in the same class
There is no guarantee that a high priority job in class A will be started before a low priority job in class B, even if they share the same schedule Prioritizing among jobs of different classes depends on the current
resource plan and on the designated resource consumer group or service name of each job class
When defining job classes, you should try to classify jobs by functionality
Consider dividing jobs into groups that access similar data, such as marketing, production, sales, finance, and human resources
Some of the restrictions to keep in mind are:
• A job must be part of exactly one class When you create a job, you can specify which class the job is part of If you do not specify a class, the job automatically becomes part of the class DEFAULT_JOB_CLASS
• Dropping a class while there are still jobs in that class results in an error You can force a class to be dropped even if there are still jobs that are members of that class, but all jobs referring to that class are then
automatically disabled and assigned to the class DEFAULT_JOB_CLASS Jobs belonging to the dropped class that are already running continue to run under class settings determined at the start of the job
Windows
Trang 35You create windows to change resource allocation among jobs during various time periods of the day, week, and so on A window is represented by an interval
of time with a well-defined beginning and end, such as "from 12am-6am"
Windows work with job classes to control resource allocation Each window
specifies the resource plan to activate when the window opens (becomes
active), and each job class specifies a resource consumer group or specifies a database service, which can map to a consumer group A job that runs within a window therefore has resources allocated to it according to the consumer group
of its job class and the resource plan of the window
Figure 26-2 shows a workday that includes two windows In this configuration, jobs that belong to the job class that links to Consumer Group 1 get more resources in the morning than in the afternoon The opposite is true for jobs in the job class that links to Consumer Group 2
Figure 26-2 Windows help define the resources that are allocated to jobs
Description of "Figure 26-2 Windows help define the resources that are allocated to jobs"
See Chapter 24, "Using the Database Resource Manager" for more information
on resource plans and consumer groups
You can assign a priority to each window If windows overlap, the window with the highest priority is chosen over other windows with lower priorities The
Scheduler automatically opens and closes windows as window start times and end times come and go
A job can name a window in its schedule_name attribute The Scheduler then starts the job when the window opens If a window is already open, and a new job is created that points to that window, the job is not started until the next time the window opens
Trang 36See "Creating Windows" for examples of creating and using windows.
Note:
If necessary, you can temporarily block windows from switching the current resource plan For more information, see "Enabling the Database Resource Manager", or the discussion of the DBMS_RESOURCE_MANAGER.SWITCH_PLAN package procedure in Oracle
Database PL/SQL Packages and Types Reference
Window Groups
You can group windows for ease of use in scheduling jobs If a job must run during multiple time periods throughout the day, week, and so on, you can create a window for each time period, and then add the windows to a window group You can then set the schedule_name attribute of the job to the name
of this window group, and the job executes during all the time periods specified
in the window group
For example, if you had a window called "Weekends" and a window called
"Weeknights," you could add these two windows to a window group called
"Downtime." The data warehousing staff could then create a job to run queries according to this Downtime window group—on weeknights and weekends—when the queries could be assigned a high percentage of available resources
If a window in a window group is already open, and a new job is created that points to that window group, the job is not started until the next window in the window group opens
See "Creating Window Groups" for examples of creating window groups
Scheduler Architecture
This section discusses the Scheduler's architecture, and describes:
• The Job Table
• The Job Coordinator
• How Jobs Execute
• Job Slaves
• Using the Scheduler in Real Application Clusters Environments
Figure 26-3 illustrates how jobs are handled by the database
Trang 37Figure 26-3 Scheduler Components
Description of "Figure 26-3 Scheduler Components"
The Job Table
The job table is a container for all the jobs, with one table per database The job table stores information for all jobs such as the owner name or the level of logging You can find this information in the *_SCHEDULER_JOBS views
Jobs are database objects, and can therefore accumulate and take up too much space To avoid this, job objects are automatically dropped by default after completion This behavior is controlled by the auto_drop job attribute
See "How to View Scheduler Information" for the available job views and
administration
The Job Coordinator
The job coordinator is a background process (cjqNNN) that is automatically started when jobs must be run, or windows must be opened It is automatically brought down after a sustained period of Scheduler inactivity The job
coordinator:
• Controls and spawns the job slaves
• Queries the job table
• Picks up jobs from the job table on a regular basis and places them in a memory cache This improves performance by avoiding going to the disk
Trang 38• Takes jobs from the memory cache and passes them to job slaves for execution
• Cleans up the job slave pool when slaves are no longer needed
• Goes to sleep when no jobs are scheduled
• Wakes up when a new job is about to be executed or a job was created using the CREATE_JOB procedure
• Upon database startup after an abnormal database shutdown, recovers any jobs that were running
You do not need to set when the job coordinator checks the job table; the
system chooses the time frame automatically
One job coordinator is used per instance This is also the case in RAC
environments
See Also:
"How to View Scheduler Information" for job coordinator administration and "Using the Scheduler
in Real Application Clusters Environments" for RAC information
How Jobs Execute
When a job is picked for processing, the job slave:
1 Gathers all the metadata needed to run the job As an example,
arguments of the program and privilege information
2 Starts a database session as the owner of the job, starts a transaction, and then starts executing the job
3 Once the job is complete, the slave commits and ends the transaction
4 Closes the session
Job Slaves
Job slaves actually execute the jobs you submit They are awakened by the job coordinator when it is time for a job to be executed They gather metadata to run the job from the job table
When a job is done, the slaves:
Trang 39• Reschedule the job if required
• Update the state in the job table to reflect whether the job has completed
or is scheduled to run again
• Insert an entry into the job log table
• Update the run count, and if necessary, failure count and retry count
• Clean up
• Look for new work (if none, they go to sleep)
The Scheduler dynamically sizes the slave pool as required
Using the Scheduler in Real Application Clusters Environments
In a Real Application Clusters (RAC) environment, the Scheduler uses one job table for each database and one job coordinator for each instance The job coordinators communicate with each other to keep information current The Scheduler attempts to balance the load of the jobs of a job class across all
available instances when the job class has no service affinity, or across the instances assigned to a particular service when the job class does have service affinity
Figure 26-4 illustrates a typical RAC architecture, with each instance's job
coordinator exchanging information with the others
Figure 26-4 RAC Architecture and the Scheduler
Trang 40Description of "Figure 26-4 RAC Architecture and the Scheduler"
Service Affinity when Using the Scheduler
The Scheduler enables you to specify the database service under which a job should be run (service affinity) This ensures better availability than instance affinity because it guarantees that other nodes can be dynamically assigned to the service if an instance goes down Instance affinity does not have this
capability, so, when an instance goes down, none of the jobs with an affinity to that instance will be able to run until the instance comes back up Figure 26-5illustrates a typical example of how services and instances could be used
Figure 26-5 Service Affinity and the Scheduler