perfor-Hash Clusters A hash cluster is an alternative to storing data in a table and then creating an index on that table.. Listing 6.17: Creating an Indexed Cluster CREATE CLUSTER paren
Trang 1Indexed Clusters
An indexed cluster (or just cluster) contains two or more tables The cluster key is a
col-umn or multiple colcol-umns that can be used to join the data in these tables When thetables of the cluster are populated with data, the data in the tables is stored in thesame block, and the rows for both tables are related based on the cluster key columns.This has the basic effect of denormalizing the table Thus, if you are going to clustertables, they should be tightly related based on the keys
The cluster key of the cluster is defined when you create the cluster It can consist
of up to 16 columns, and the cluster key size is limited to something like half thedatabase block size Also, the cluster key cannot consist of a LONG, LONG RAW, orLOB datatype (CLOB, BLOB, and so on)
One benefit of clustering tables is that the cluster key is stored only once Thisresults in a slight improvement in performance, because the overall size of the clus-tered tables is smaller than the size of two individual tables storing the same data, soless I/O is required Another benefit of clusters is that, unlike indexes, they do notbrown-out Thus, performance of SELECT statements should not be negativelyimpacted by ongoing DML activity, as can happen with indexes
Clusters can improve join performance, but this can be at the cost of slower mance on scans of the individual tables in the cluster and any DML activity on thecluster To avoid problems, you should cluster only tables that are commonly joinedtogether and that have little DML activity
perfor-Hash Clusters
A hash cluster is an alternative to storing data in a table and then creating an index on
that table A hash cluster is the default type of cluster that will be created by the ATE CLUSTER command
CRE-In a hash cluster, the cluster key values are converted into a hash value This hashvalue is then stored along with the data associated with that key The hash value iscalculated by using a hashing algorithm, which is simply a mathematical way of gen-erating a unique identifier (The same keys would generate the same hash value, ofcourse.) You control the number of hash keys through the HASHKEYS parameter ofthe CREATE CLUSTER command Thus, the total number of possible cluster key val-ues is defined when the cluster is created Be careful to correctly choose this number,
or you will find that keys may end up being stored together
There are two different kinds of hash clusters:
• With a normal hash cluster, Oracle will convert the values of the WHERE clause,
if they contain the cluster key, to a hash value It will then use the hash value as
an offset value in the hash cluster, allowing Oracle to quickly go to the rowbeing requested
Trang 2• With a single-row hash cluster, the hash keys relate directly to the key in the table
stored in hash cluster This eliminates the scan that would be required on a mal hash cluster, since Oracle would scan the entire cluster for a single tablelookup by default
nor-When choosing the cluster key, you should consider only columns with a highdegree of cardinality You should also be careful about using hash clusters when atable will be subject to a great deal of range searches, such as searches on date ranges
Hash clusters generally are used when there is a constant need to look up specific andunique values, such as those you might find in a primary key or a unique index
For the hashing algorithm, you have three options The first is to use Oracle’s nal algorithm to convert the cluster key values into the correct hash value This workswell in most cases You can also choose to use the cluster key if your cluster key issome uniform value, such as a series of numbers generated by a sequence The otheroption is a user-defined algorithm in the form of a PL/SQL function
inter-Creating Clusters
You use the CREATE CLUSTER command to create both indexed clusters and hashclusters In order to create a cluster in your schema, you need the CREATE CLUSTERprivilege To create a cluster in another schema, you must have the CREATE ANYCLUSTER privilege You also need the appropriate QUOTA set for the tablespace inwhich you wish to create the clusters
Creating an Indexed Cluster
You use the CREATE CLUSTER command to create indexed clusters in your database
Listing 6.17 shows an example of creating an indexed cluster
Listing 6.17: Creating an Indexed Cluster
CREATE CLUSTER parent_child(parent_id NUMBER)
INDEXSIZE 512STORAGE (INITIAL 100k NEXT 100k);
It is the keyword INDEX in the CREATE CLUSTER command that makes this anindexed cluster Omit this keyword, and Oracle will default to creating a hash cluster
When you create the cluster, you define how many rows will be identified witheach cluster key by using the SIZE parameter The value associated with the SIZE key-word tells Oracle how much space to reserve for all rows with the same hash value,and this value should be a multiple of the database block size If SIZE is not a multiple
MANAGING CLUSTERS
Oracle Database Administration
P A R T
II
Trang 3of the database block size, it will be rounded up to the next multiple of the databaseblock size For example, if you have 1600 bytes left in the block, and SIZE is set at 512,then you will be able to store three cluster key sets within that block
WARNING It is important to set the SIZE parameter correctly for both indexed ters and hash clusters If you set SIZE too small, you can cause rows to be chained, whichcan cause additional I/O
clus-After you have created the cluster, you can add tables to the cluster Then you need
to create a cluster index before you can use the cluster or the tables in the cluster
Adding Tables to an Indexed Cluster
Listing 6.18 shows an example of creating two tables and adding them to the clustercreated in Listing 6.17
Listing 6.18: Adding Tables to a Cluster
CREATE TABLE parent(parent_id NUMBER PRIMARY KEY,last_name VARCHAR2(30) NOT NULL,first_name VARCHAR2(30) NOT NULL,middle_int CHAR(1),
sex CHAR(1),married_status CHAR(1))
CLUSTER parent_child (parent_id);
CREATE TABLE children(child_id NUMBER CONSTRAINT pk_children PRIMARY KEYUSING INDEX TABLESPACE indexes
STORAGE (INITIAL 200k NEXT 200k),parent_id NUMBER,
last_name VARCHAR2(30),first_name VARCHAR2(30),middle_int CHAR(1),medical_code VARCHAR2(30) CONSTRAINT children_check_upper
CHECK (medical_code = UPPER(medical_code) ))
CLUSTER parent_child(parent_id);
Trang 4Notice that you can still define primary keys on the tables, use the USING INDEXcommand to define where the primary key index is to be stored, and include theSTORAGE clause You can create indexes on tables in clusters, just as you would forany other table
Creating a Cluster Index
Once you have set up the cluster, you need to create a cluster index so that you canadd rows to our cluster Until this step is done, no data can be added to any of thetables that exist in the cluster To create the cluster index, you use the CREATE INDEXcommand using the ON CLUSTER keyword, as shown in the following example:
CREATE INDEX ic_parent_children
ON CLUSTER parent_child;
After creating the cluster index, you can work with the tables in the cluster
NOTE If you accidentally drop the cluster index, you will not lose the data in the cluster
However, you will not be able to use the tables in the cluster until the cluster index is re-created
Creating a Hash Cluster
Creating a hash cluster is similar to creating an indexed cluster The differences arethat you omit the INDEX keyword and add the HASHKEYS keyword Listing 6.19shows an example of using a CREATE CLUSTER statement to create a hash cluster
Listing 6.19: Creating a Hash Cluster
CREATE CLUSTER parent_child(parent_id NUMBER)
SIZE 512 HASHKEYS 1000STORAGE (INITIAL 100k NEXT 100k);
The SIZE and HASHKEYS keywords are used to calculate how much space to cate to the cluster The SIZE keyword defines how much of each block will be used tostore a single set of cluster key rows This value determines the number of cluster keyvalues that will be stored in each block of the cluster SIZE is defined in bytes, and you
allo-can also append a k or m to indicate that the number is in kilobytes or megabytes.
The HASHKEYS clause (which is not used when creating an indexed cluster)defines the number of cluster key values that are expected in the cluster Oracle willround up the number chosen to the next nearest prime number
MANAGING CLUSTERS
Oracle Database Administration
P A R T
II
Trang 5It is important to try to calculate the HASHKEYS and SIZE values as exactly as sible, but it is also sometimes a difficult task If you already have the data stored inanother database, or perhaps in a table that you were thinking of moving to a hashcluster, it might be easier to determine the settings for these values You could simplyset HASHKEYS to the number of unique rows in the table, based on a select set ofcolumns that make up either a primary or unique key, or on a pseudo unique key ifone does not exist.
pos-The SIZE value is a bit more complicated SIZE could be calculated by first takingthe overall size of the data in the object that you are thinking about moving to a hashcluster, and dividing that size by the number of unique key values This will give yousome idea of where to start making the SIZE parameter (I would add on a bit for over-head and a fudge factor.) By default, Oracle will allocate one block for every clusterkey value (which is potentially very expensive) Also, the value given by SIZE cannot
be greater than the block size of the database If it is, Oracle will use the databaseblock size instead
TIP Like many other things in the database world, there is no exact science to ing cluster sizes It’s important to get as close as you can to an accurate figure, but this maynot be possible until you have seen how the data is actually going to come in and load Inpractice, sizing may require one or two reorganizations of the object So, sizing typicallyinvolves doing your best to calculate the right numbers, and then adjusting those numbersbased on ongoing operations
calculat-Both SIZE and HASHKEYS can have a significant impact on performance If youallocate too much space (by making either SIZE or HASHKEYS too large), you will bewasting space Since fewer data rows will exist per block, full scans (not using thehash key) or range scans of the hash cluster will be degraded Also, Oracle uses thevalues of SIZE and HASHKEYS, along with the values of INITIAL and NEXT, to deter-mine the initial extent allocations for the hash cluster
The total amount of space that will be allocated to a hash cluster when it is created
is the greater of SIZE * HASHKEYS or INITIAL Thus, when the hash cluster is created,all of the initial space for the expected number of hash keys is mapped out in a struc-
ture called a hash table Subsequent extents are created as overflow space and will be
generated based on the NEXT parameter of the STORAGE clause Any rows added tothe cluster will first be added to the initial block in the hash table, based on the hashvalue of the cluster key column(s) in the row If the block that is assigned to that clus-ter key is full, then the row will be stored in an overflow block, and a pointer will be
Trang 6Once you have created the hash cluster, you add tables to the cluster, just as youwould with an index cluster (see Listing 6.18) You do not need to create a clusterindex, as you do for an indexed cluster After you add the tables to the hash cluster,the cluster is ready for use.
ALTER CLUSTER parent_childSTORAGE (NEXT 200k);
Dropping Clusters
To drop an indexed cluster, you must first drop the underlying cluster index, andthen you must drop the underlying tables of the cluster There is no way to decluster atable Thus, you may need to use the CREATE TABLE AS SELECT command to movethe data from tables in the cluster to tables outside the cluster
After you remove the cluster index and the tables of the index, you can use theDROP CLUSTER command If you don’t care about the data in the tables of the clus-ter, you may use the optional parameter INCLUDING TABLES in the DROP CLUSTERcommand, and Oracle will remove the underlying tables for you You may also need
to include the CASCADE CONSTRAINTS clause if the tables have referential integrityconstraints that will need to be dropped Here is an example of dropping the PAR-ENT_CHILD cluster created in Listing 6.19, including the tables:
DROP CLUSTER parent_childINCLUDING TABLES
Trang 7Viewing Cluster Information
The DBA_CLUSTERS view provides information about the clusters in your database.The following is a description of the view
SQL> DESC dba_clustersName Null? Type - - -OWNER NOT NULL VARCHAR2(30)CLUSTER_NAME NOT NULL VARCHAR2(30)TABLESPACE_NAME NOT NULL VARCHAR2(30)PCT_FREE NUMBERPCT_USED NOT NULL NUMBERKEY_SIZE NUMBERINI_TRANS NOT NULL NUMBERMAX_TRANS NOT NULL NUMBERINITIAL_EXTENT NUMBERNEXT_EXTENT NUMBERMIN_EXTENTS NOT NULL NUMBERMAX_EXTENTS NOT NULL NUMBERPCT_INCREASE NUMBERFREELISTS NUMBERFREELIST_GROUPS NUMBERAVG_BLOCKS_PER_KEY NUMBERCLUSTER_TYPE VARCHAR2(5)FUNCTION VARCHAR2(15)HASHKEYS NUMBERDEGREE VARCHAR2(10)INSTANCES VARCHAR2(10)CACHE VARCHAR2(5)BUFFER_POOL VARCHAR2(7)SINGLE_TABLE VARCHAR2(5)Here is an example of a query against the DBA_CLUSTERS view and its results:SELECT owner, cluster_name, tablespace_name
FROM dba_clusters WHERE owner NOT LIKE ’SYS’;
OWNER CLUSTER_NAME TABLESPACE - - -SCOTT TEST USERS
Trang 8Managing Rollback Segments
When creating a database, the DBA needs to carefully consider the number, size, andextents to be allocated to rollback segments in the database Rollback segments areused to provide rollback of incomplete transactions, read consistency, and databaserecovery
Planning Rollback Segments
There are as many differing opinions about how to initially size a rollback segment asthere are models of cars on the road The answer to the question of how many roll-back segments and what size to make them is the same as the answer to many suchDBA questions: It depends
There are a few considerations for planning rollback segments:
• The average size of a transaction
• The total number of concurrent transactions
• The type and frequency of transactionsThe main concerns with rollback segments in terms of performance and opera-tional success are the appropriate sizing of the rollback segment and contention for agiven rollback segment If you do not have enough rollback segments allocated, con-tention for rollback segments can occur
You also need to have the appropriate sizing of the rollback segment tablespace Asingle large job may cause a rollback segment to extend and take up the entire table-space If the rollback segment has OPTIMAL set (which should always be the case!), itwill eventually shrink after the transaction using that rollback segment has completed
or failed In the meantime, however, any attempt to extend other rollback segmentswill lead to transaction failure
Some DBAs create a separate large rollback segment in its own tablespace for largetransactions I personally don’t like this approach, because these large rollback seg-ments are rarely used, so they are a waste of space I prefer to lump that extra spaceinto one rollback segment tablespace that will allow the rollback segments to growwith the large transaction Then I set the OPTIMAL parameter so the rollback segmentwill shrink back to its correct size later on
NOTE Keep in mind the concept of I/O distribution with regard to rollback segments
I’ve seen more than one DBA just plug the rollback segment tablespace datafiles on onedisk, and then wonder why the system had such poor response time
MANAGING ROLLBACK SEGMENTS
Oracle Database Administration
P A R T
II
Trang 9I generally will create rollback segments according to the following formula:
I normally will make sure that the total size of each rollback segment (depending
on how much space is available) is about 1.3 times the size of the largest table in thedatabase This allows me to modify all of the rows of any table without any problems.(Of course, if you have some particularly large tables, this might not be possible.)Finally, I always throw as much space as I can to the rollback segment tablespace (par-ticularly in a production database) I am not fond of transaction failures due to lack ofrollback segment space For test or development systems where less disk space is avail-able, I use smaller rollback segments Again, it all depends on the environment
TIP If you find that you have users that are constantly blowing your tablespaces up withad-hoc queries that are poorly written, you should look into metering your users’ resourceuse with Oracle’s resource control facilities, rather then just allowing them to extend atablespace to eternity
The truth is that most DBAs guess at what they think is the right number and size
of rollback segments, and then monitor the system for contention Usually, when youfirst create a database, you have little idea of how many users will really be on it orhow big the average transaction or largest table will be
After you create your initial set of rollback segments, you need to monitor theirusage Chapter 15 provides details on monitoring rollback segment use If you findthat rollback segments have a significant number of extends, shrinks, or wraps, asdetermined from the V$ROLLSTAT performance view, you will need to rework theextent size and perhaps the number of extents in the rollback segment You may alsowant to review the V$WAITSTAT performance view for classes that include the wordUNDO in them If you see waits for these segment types, you probably need to addrollback segments to your database
Finally, you should use a uniform extent management policy with rollback ments This implies that the INITIAL and NEXT storage parameters for a rollback seg-ment will always be the same size Also, INITIAL and NEXT should be the same for all
Trang 10rollback segments in any given tablespace This helps eliminate any fragmentationissues in the rollback segment tablespaces
Creating Rollback Segments
To create a rollback segment, use the CREATE ROLLBACK SEGMENT command ating a rollback segment is like creating any other segment in most respects Youdefine a STORAGE clause for the object, and you can define to which tablespace therollback segment is assigned Listing 6.20 shows an example
Cre-Listing 6.20: Creating a Rollback Segment
CREATE ROLLBACK SEGMENT rbs01TABLESPACE rbs
STORAGE (INITIAL 1m NEXT 1m OPTIMAL 10m MINEXTENTS 10);
The OPTIMAL option within the STORAGE clause allows you to define a size thatyou want Oracle to shrink the rollback segment back to Thus, the rollback segmentwill expand as required, but Oracle will shrink it back down to the correct size later
As noted earlier, you should always use the OPTIMAL cause to prevent problems with
a rollback segment taking too much tablespace
The OPTIMAL and MINEXTENTS clauses cross-check each other Thus, you cannothave an OPTIMAL parameter that will cause the rollback segment to drop below thevalue defined by MINEXTENTS
As noted in the previous section, it is strongly encouraged that you make all ofyour extents uniform in size (thus, make sure that INITIAL and NEXT are set to thesame value) If you choose to make INITIAL or NEXT larger, make sure it is a multiple
of the smaller value
When you create a rollback segment it is not initially available for use You willneed to use the ALTER ROLLBACK SEGMENT command (discussed next) to bring therollback segment online Also, you will want to add the rollback segment to the data-base parameter file so that it will be brought online immediately when the database isstarted You use the ROLLBACK_SEGMENT parameter in the init.ora file to accom-plish this
Altering Rollback Segments
You can use the ALTER ROLLBACK SEGMENT command to alter the storage teristics of a rollback segment, bring it online after creation, or take it offline beforedropping it You can also use the ALTER ROLLBACK segment command to force therollback segment to shrink back to the size defined by OPTIMAL or, optionally, to a
charac-MANAGING ROLLBACK SEGMENTS
Oracle Database Administration
P A R T
II
Trang 11size you define in the ALTER ROLLBACK SEGMENT SHRINK command Here aresome examples of using the ALTER ROLLBACK SEGMENT command:
ALTER ROLLBACK SEGMENT rbs01 ONLINE;
ALTER ROLLBACK SEGMENT rbs01 OFFLINE;
ALTER ROLLBACK SEGMENT rbs01 SHRINK;
ALTER ROLLBACK SEGMENT rbs01 STORAGE (OPTIMAL 9m MINEXTENTS 9);
If you attempt to take a rollback segment offline while it is in use, it will go into anOFFLINE PENDING state (as you can see in the STATUS column of V$ROLLSTAT) Therollback segment will be taken offline immediately after the user transaction has com-pleted Since the ALTER ROLLBACK SEGMENT command is DDL, it will do animplied commit Thus, if the user whose transaction was holding up the rollback seg-ment before it was taken offline performed some sort of DML operation and did notcommit it, the ALTER ROLLBACK SEGMENT command would commit it for that user
Dropping Rollback Segments
Dropping a rollback segment is done with the DROP ROLLBACK SEGMENT command:DROP ROLLBACK SEGMENT rbs01;
Oracle will generate an error message (ORA-1545) if you attempt to drop a rollbacksegment if it is in an ONLINE or OFFLINE PENDING state
Viewing Rollback Segment Information
Three data dictionary views provide the primary information about rollback ments: DBA_ROLLBACK_SEGS, V$ROLLSTAT, and V$ROLLNAME
seg-The DBA_ROLLBACK_SEGS View
The DBA_ROLLBACK_SEGS view provides a list of all rollback segments that exist onthe system, both online and offline This view also includes information such as storage information (INITIAL, NEXT, and OPTIMAL) and the tablespace the rollbacksegment is occupying Here is a description of the view:
SQL> DESC dba_rollback_segsName Null? Type - - -SEGMENT_NAME NOT NULL VARCHAR2(30)OWNER VARCHAR2(6)TABLESPACE_NAME NOT NULL VARCHAR2(30)SEGMENT_ID NOT NULL NUMBERFILE_ID NOT NULL NUMBER
Trang 12BLOCK_ID NOT NULL NUMBERINITIAL_EXTENT NUMBERNEXT_EXTENT NUMBERMIN_EXTENTS NOT NULL NUMBERMAX_EXTENTS NOT NULL NUMBERPCT_INCREASE NUMBERSTATUS VARCHAR2(16)INSTANCE_NUM VARCHAR2(40)RELATIVE_FNO NOT NULL NUMBERHere is an example of a query against the DBA_ROLLBACK_SEGS view and itsresults:
SELECT segment_name, owner, tablespace_nameFROM dba_rollback_segs
ORDER BY segment_name;
SEGMENT_NAME OWNER TABLESPACE_NAME - - -RB01 SYS RBS
RB02 SYS RBSRB03 SYS RBSRBS01 SYS RBSSYSTEM SYS SYSTEMTEST_ME SYS LOCAL_UNIFORM
The V$ROLLSTAT and V$ROLLNAME Views
The V$ROLLSTAT and V$ROLLNAME views are typically used together STAT provides performance information about the individual rollback segments, asshown here
V$ROLL-SQL> DESC v$rollstatName Null? Type - - -USN NUMBEREXTENTS NUMBERRSSIZE NUMBERWRITES NUMBERXACTS NUMBERGETS NUMBERWAITS NUMBEROPTSIZE NUMBER
MANAGING ROLLBACK SEGMENTS
Oracle Database Administration
P A R T
II
Trang 13HWMSIZE NUMBERSHRINKS NUMBERWRAPS NUMBEREXTENDS NUMBERAVESHRINK NUMBERAVEACTIVE NUMBERSTATUS VARCHAR2(15)CUREXT NUMBERCURBLK NUMBERUnfortunately, as you can see, this view does not contain the name of the rollbacksegment, but rather the Undo Segment Number (USN) of the segment
The V$ROLLNAME view resolves the USN to the rollback segment name, as youcan see in its description:
SQL> DESC v$rollnameName Null? Type - - -USN NUMBERNAME NOT NULL VARCHAR2(30)Here is a sample query that uses both these views and its results:
SELECT a.name, b.extents, b.rssize, b.waits, b.extends, b.shrinksFROM v$rollname a, v$rollstat bWHERE a.usn=b.usn;
NAME EXTENTS RSSIZE WAITS EXTENDS SHRINKS - - - - - -SYSTEM 8 407552 0 0 0RB02 16 1636352 0 0 0Notice that not all of the rollback segments appear Only those rollback segmentsthat are online or have an OFFLINE PENDING status will appear in the V$ROLLSTATand V$ROLLNAME views
Managing Views
A view is a logical representation of a SQL statement that is stored in the database.
Views are useful when certain SQL statements are commonly used on a given database
Trang 14Also, views can be used to tune otherwise untunable code You can rename thetable that the code is attempting to access, and then create a view with the name ofthe table In this view, you can include hints that make accessing the table muchmore efficient This solution doesn’t always work, but it does in some cases.
You can use views to INSERT, UPDATE, and DELETE records from the database,with a few provisions in regards to key-preserved tables You can even create triggers
Creating Views
You use the CREATE VIEW statement to create a view You might also want to use theCREATE OR REPLACE VIEW command, should the view already exist Here is anexample of creating a view:
CREATE OR REPLACE VIEW v_emp
AS SELECT empno, ename FROM empWHERE ename BETWEEN ‘A%’ AND ‘D%’;
This example creates a simple view over the EMP table that displays only the employee
number and name for all employees whose names begin with the letters between A and D.
The CREATE VIEW command also has a FORCE option that will cause Oracle to createthe view even if the underlying tables of the view are not yet created, as in this example:
CREATE OR REPLACE FORCE VIEW v_emp
MANAGING VIEWS
Oracle Database Administration
P A R T
II
Trang 15AS SELECT empno, ename FROM empWHERE ename BETWEEN ‘A%’ AND ‘D%’;
From time to time, views may become invalid You can recompile a view with theALTER VIEW command using the COMPILE keyword:
ALTER VIEW v_emp COMPILE;
Finally, when you are just sick and tired of your view, you can drop it with theDROP VIEW command:
DROP VIEW v_emp;
Using Your Views
The way you create views can have an impact on performance, just as the way youcreate SQL statements can have an impact on performance on those SQL statements
If you have two tables (or more) in the view joined together, the keys involved in thejoin cannot be updated Basically, if your UPDATE operation will cause changes torows in both tables, it will fail Also, if in the SELECT portion of the view, you choose
to include a column from a table that cannot be updated, you will find that your ity to remove records from that view will be compromised To understand what canhappen, let’s look at some examples:
abil-DROP VIEW giview;
DROP TABLE gijoe;
DROP TABLE rank;
CREATE TABLE gijoe( part_id NUMBER PRIMARY KEY,Toy_name VARCHAR2(30),
Toy_rank NUMBER);
CREATE TABLE rank(toy_rank NUMBER PRIMARY KEY,rank_name VARCHAR2(30) );
INSERT INTO rank VALUES (1, ’Private’);
INSERT INTO rank VALUES (2, ’Sgt’);
INSERT INTO rank VALUES (3, ’Captain’);
INSERT INTO gijoe VALUES (10, ’Joe’,1);
INSERT INTO gijoe VALUES (20, ’Sarge’,2);
INSERT INTO gijoe VALUES (30, ’Cappie’,3);
Trang 16Now, create a view.
CREATE VIEW giview AS SELECT a.part_id, a.toy_name, b rank_nameFROM gijoe a, rank b
WHERE a.toy_rank=b.toy_rank;
Update a record… this works
UPDATE giview SET toy_name=’Joseph’ WHERE part_id=10;
Insert a new record… this will fail!
INSERT INTO giview VALUES (4,’Barbie’,’Sarge’);
Note that the INSERT fails This is because one of the columns that the example is
trying to INSERT INTO is not key preserved To be key preserved, each column being
updated must relate to the primary key in the base table that the column is in In thisexample, we are trying to update a column in the RANK table (RANK_NAME) How-ever, the primary key of the RANK table (TOY_RANK) is not part of the UPDATE state-ment (or the view) Since we are not able to reference the primary key columns of theRANK table in the INSERT statement (again, because these columns are not part of theview we are updating), the TOY_NAME column cannot be updated The bottom line
is that with the way this view is constructed, we cannot insert records into the lying base tables through this view
under-We might rebuild the view in a slightly different way to try to get the UPDATEoperation to succeed:
DROP VIEW giview;
Now, create a view
CREATE VIEW giview AS SELECT a.part_id, a.toy_name, b.toy_rank,
b rank_nameFROM gijoe a, rank bWHERE a.toy_rank=b.toy_rank;
Insert a new record… this will fail!
INSERT INTO giview (part_id, toy_name, toy_rank)VALUES (4,’Barbie’,2);
Still, it fails with this error:
Trang 17RANK.TOY_RANK column instead of the GIJOE.TOY_RANK column Thus, it is trying
to update two tables instead of one Let’s rewrite the view again:
DROP VIEW giview;
Now, create a view
CREATE VIEW giview AS SELECT a.part_id, a.toy_name, a.toy_rank,
b rank_nameFROM gijoe a, rank bWHERE a.toy_rank=b.toy_rank;
Insert a new record… this will fail!
INSERT INTO giview (part_id, toy_name, toy_rank)VALUES (4,’Barbie’,2);
Once we use the GIJOE.TOY_RANK column rather than the RANK.TOY_RANK umn, we have met the requirement that only one table can be updated at a time.Unfortunately, since this view accesses A.TOY_RANK, we will have a problem if wewant to use the view to insert a record into the RANK table, as shown in this example:INSERT INTO giview
col-(toy_rank, rank_name)VALUES (10, ‘General’);
Same old song—we get an ORA-1776 error (the patriotic Oracle message!) again
So, how do we work around this problem? We might try to include both columns inthe view:
DROP VIEW giview;
Now, create a view
CREATE OR REPLACE VIEW giview AS SELECT a.part_id, a.toy_name, a.toy_rank as girank,b.toy_rank as update_rank, b rank_name
FROM gijoe a, rank bWHERE a.toy_rank=b.toy_rank;
INSERT INTO giview(update_rank, rank_name)VALUES (10, ‘General’);
But this doesn’t work either Basically, given this view, you will not be able to doINSERT operations on columns that are in the RANK table This is because Oracle willnot allow you to change (or add) a column that is part of the join criteria of the view.For example, if you try to update the RANK_NAME column in GIVIEW, this impacts
Trang 18the join of the view, because the TOY_RANK column is the join column in the view.
Because of this, you cannot update the RANK_NAME in the GIVIEW table However,
if you want to change a TOY_NAME column value, this is acceptable because it is notpart of the join between the two tables, and thus will not impact the change
TIP INSTEAD OF triggers can be used as an effective solution to the problem of non-keypreserved DML These triggers can be written to override DML actions against a view andimplement those changes in the base tables of the view instead Refer to the Oracle docu-mentation for more information about INSTEAD OF triggers
Views as Performance Problems
Views can be performance problems, as well as performance god-sends Be cautiouswhen you create views that they only bring back the rows you really need Also be care-ful of situations where you have views, stacked on views, stacked on views The per-formance of such designs can be terrible
I knew a developer who wrote code to use such a stacked-view design All the oper really needed was 2 columns of information, yet he was going through a view thatwas returning some 25 columns of information, via a stack of about four different lev-els of views Each level contained joins that you would not believe (including outerjoins, which views seem to have particularly hard times with)
devel-The developer came to me asking why his code performed so poorly Once I had himrewrite his query to not use the view, but directly query the two-table join that heneeded, his code’s performance was increased dramatically This just serves to demon-strate that one of the follies of views is that they can make developers lazy in design-ing efficient code
Using Inline Views
Oracle also offers an inline view that is very handy An inline view is part of a SQLstatement It allows you, in the body of the SQL statement, to define the SQL for a
Trang 19view that the SQL statement will use to resolve its query Here is an example of using
an inline view:
SELECT * FROM /* This starts the inline view */
(SELECT owner, table_name, num_rows FROM dba_tables
WHERE num_rows IS NOT NULLORDER BY num_rows desc)/* This is the end of the inline view */
WHERE ROWNUM < 10;
This example gives the top-ten largest tables in the database, in terms of rowcount Note that this particular bit of code will work only with Oracle8i This isbecause the ORDER BY operation that occurs in the body of the inline view was notsupported before Oracle8i The ORDER BY clause sorts the result set of the data,before it is passed to the main query
Viewing View Information
The DBA_VIEWS view is used to assist in the management of views in the database Itprovides information about the view, such as its owner, its name, and the SQL used tocreate the view Here is a description of DBA_VIEW:
SQL> DESC dba_viewsName Null? Type - - -OWNER NOT NULL VARCHAR2(30)VIEW_NAME NOT NULL VARCHAR2(30)TEXT_LENGTH NUMBERTEXT LONGTYPE_TEXT_LENGTH NUMBERTYPE_TEXT VARCHAR2(4000)OID_TEXT_LENGTH NUMBER
OID_TEXT VARCHAR2(4000)VIEW_TYPE_OWNER VARCHAR2(30)VIEW_TYPE VARCHAR2(30)The following is an example of a query that uses the DBA_VIEW and its result:SELECT owner, view_name
FROM dba_viewsWHERE owner != ’SYS’;
Trang 20OWNER VIEW_NAME - -SYSTEM AQ$DEF$_AQCALLSYSTEM AQ$DEF$_AQERRORSCOTT GIVIEW
In this example, you see that the SCOTT user owns a view called GIVIEW If youwanted to see the SQL statement that as associated with that view, you could querythe TEXT column of the DBA_VIEWS view Note that TEXT is a LONG, therefore youwill probably need to use the SET LONG and SET ARRAY commands in SQL*Plus
MANAGING VIEWS
Oracle Database Administration
P A R T
II