Friday 6 February 2009

SQL Language Enhancements in 9i

SQL Language Enhancements

9i includes SQL language enhancements to conform to the latest international SQL standard, referred to as SQL-99, as well as some other miscellaneous improvements.

Joins

SQL statements that perform joins typically combine rows from two or more tables or views. The Cartesian product is a join that has no WHERE clause, so it produces all combinations of rows from the two input tables. With SQL-99 syntax, use the keywords CROSS JOIN to implement a Cartesian product.

Use the keywords NATURAL JOIN to match rows that have the same values in all columns that have the same name (the columns must also have the same data type):

SELECT location_id, city, department_name
FROM location_table NATURAL JOIN department_table ;

To perform a join where columns do not have the same name or you want to specify the columns on which to join, use the keyword USING:

SELECT location_id, city, department_name
FROM location_table JOIN department_table
USING (location_id) ;

Alternatively, you could use the SQL-99 ON clause to specify the join separate from other parts of the WHERE clause.

Recall that outer joins combine all matching rows from both tables, along with rows that are unmatched (from either one table or the other). A full outer join returns all matching rows, plus the unmatched rows from both tables. Previously in Oracle, you encoded a plus sign (“+”) after a table name to signify an outer join. Using the SQL-99 syntax, use the words LEFT OUTER JOIN or RIGHT OUTER JOIN instead. The table to the left or right (respectively) of these keywords returns its unmatched rows. Or specify FULL OUTER JOIN to retrieve the matched rows plus the unmatched rows from both tables.

Here’s an example LEFT OUTER JOIN:

SELECT d.department_id, e.last_name
FROM departments d LEFT OUTER JOIN employees e
ON d.department_id = e.department_id
ORDER BY d.department_id;

The FULL OUTER JOIN returns rows based on the matching condition, plus unmatched rows from the tables to the left and right of the join clause:

SELECT d.department_id as d_dept_id, e.department_id as e_dept_id,
e.last_name
FROM departments d FULL OUTER JOIN employees e
ON d.department_id = e.department_id
ORDER BY d.department_id;

(The above two examples are from Oracle Corporation’s Oracle9I SQL Reference manual, Release 2.)

CASE Expressions

Expressions consists of one or more values, operators, and SQL functions and result in a value. Expressions can appear in any of these clauses:

  • The SELECT clause of queries
  • The WHERE, ORDER BY, or HAVING clauses
  • The VALUES clause of the INSERT statement
  • The SET clause of an UPDATE statement

Oracle9I introduces CASE expressions. They have two basic forms. Here are examples of each:

SELECT last_name, class_membership,
CASE class_membership WHEN 1 THEN ‘Freshman’
WHEN 2 THEN ‘Sophomore’
WHEN 3 THEN ‘Junior’
WHEN 4 THEN ‘Senior’
ELSE ‘Unknown’ END As “Classes”
FROM class_table ;

The second CASE expression format is called the searched CASE. In it, comparisons occur after the WHEN clause:

SELECT last_name, salary,
CASE WHEN salary < 40000 THEN ‘low’
WHEN salary < 80000 THEN ‘average’
WHEN salary >= 80000 THEN ‘high’ END As “Category”
FROM employee_table ORDER BY last_name ;

New Functions

9i introduces many new SQL functions. Among them are various analytic functions. These provide for distributions, percentiles, ranking, histograms and grouping sets.

Also new is the NULLIF function: NULLIF (expression_1, expression_2)

If the expressions are equal, it returns NULL. Otherwise, it returns expression_1.

The COALESCE function returns the first value in the list that is not null:

COALESCE (expression_1, expression_2, expression_3 . . .)

Globalization Enhancements

9i introduces new date and time data types:

Data Type:

Use:

TIMESTAMP

Like DATE, but specifies fractional seconds precision

TIMESTAMP WITH TIME ZONE

TIMESTAMP plus time zone displacement

TIMESTAMP WITH LOCAL TIME ZONE

TIMESTAMP plus time normalized in terms of the database’s time zone

INTERVAL DAY TO SECOND

A time duration stored in terms of days, hours, minutes and seconds

INTERVAL YEAR TO MONTH

A time duration stored in terms of years and months

Oracle supports two new Unicode character sets:

New Character Set:

Use:

AL16UTF16

2-byte, fixed width (also known as UTF16 or UCS2

AL32UTF8

8-bit, variable-width encoding (use instead of UTF8)

9i allows you to have up to 4 sort values for each character to enable a four-level linguistic sort.

Miscellaneous Language Enhancements

9i introduces the SQL MERGE statement. This provides for bulk insert and/or update of data records into a table. If a record matches a row in the target table as per some condition you specify, that row in the target table is updated with the new record. If a record in the input stream does not match any row in the existing table (as per the ON condition you specify), that that row is inserted into the target table.

9i allows you to create a constraint index explicitly as part of the CREATE TABLE statement:

CREATE TABLE my_table
(column_a CHAR(5)
CONSTRAINT my_pk PRIMARY KEY
USING INDEX
(CREATE INDEX
my_ix ON my_table(column_a)),
column_b CHAR(10) );

When you later drop or disable the constraint, you can tell Oracle either to KEEP or DROP the index:

ALTER TABLE my_table DROP CONSTRAINT my_pk KEEP INDEX ;

Oracle now minimizes the duration of its lock during foreign-key checking. Previously these locks were held until the operation completed. 9i also caches up to 256 primary keys, for quicker reference during referential checking.

On the SELECT...FOR UPDATE statement, you can now specify a maximum number of seconds to wait for any locks the statement requires:

SELECT... FOR UPDATE WAIT 10 ;

If the program does not get the lock within the specified time, it receives an error message from Oracle and continues.

For performance, 9i can generate native C code from PL/SQL procedures, then compile the C code (assuming you have a C compiler on your server).

9i introduces a new GUI tool called the Locale Builder. It maintains information about the language, territory, character set, sort definitions, and the like in .nlt and .nlb files. Start the Locale Builder by issuing the command: lbuilder under Unix, or select it off the Windows server “Start Program” menus.

Thursday 5 February 2009

Performance Enhancements from 8i to 9i

Performance Enhancements

Index Monitoring

You can now monitor an index to determine if it’s being used and collect usage statistics. To turn on monitoring:

ALTER INDEX my_index MONITORING USAGE;

To turn off monitoring:

ALTER INDEX my_index NOMONITORING USAGE;

Query the dictionary table V$OBJECT_USAGE to view the index utilization data.

Skip Scan Index Access

Oracle can now use skip scan technology to scan composite indexes when a query does not include the prefix column. This applies to B-tree, cluster, and descending index scans. It does not apply to bitmap, domain, and function-based indexes, or to reverse-key indexes.

First Rows Optimization

9i introduces the ability to optimize data retrieval for the first N rows, where the only allowable values for N are: 1, 10, 100 or 1,000. You can set the optimizer mode for a session like this:

ALTER SESSION SET OPTIMIZER_MODE = FIRST_ROWS_100 ;

Or set the OPTIMIZER_GOAL, which overrides the OPTIMIZER_MODE:

ALTER SESSION SET OPTIMIZER_GOAL = FIRST_ROWS_1000 ;

Remember that an optimizer hint on a particular SQL statement overrides either of these settings for that one statement.

System Statistics

Use these procedures in the DBMS_STATS package to gather system statistics (for CPU and I/O utilization). Be sure to gather statistics during a period in which the workload is typical.

Procedure:

Use:

GATHER_SYSTEM_STATS

Gathers system performance statistics

SET_SYSTEM_STATS

Sets system stats in the table specified by the STATTAB parameter

GET_SYSTEM_STATS

Gets system stats from the table specified by the STATTAB parameter

9i includes these values you encode on the METHOD_OPT parameter of various DBMS_STATS procedures:

Parameter:

Use:

SKEWONLY

Creates histogram based on data distribution

REPEAT

Creates histogram with the same number of buckets

AUTO

Creates histogram based on data distribution and how the column is accessed

The new 9i columns in the PLAN_TABLE to track system statistics include: CPU_COST, IO_COST, and TEMP_SPACE.

Cursor Sharing

Traditionally Oracle only shares a cursor in the Shared Pool if the statements are completely identical (even including spacing). This is the default cursor sharing setting of EXACT. 9i now allows you to set this parameter to SIMILAR, or even to force cursor sharing with FORCE:

ALTER SESSION SET cursor_sharing = SIMILAR ;

Tuesday 3 February 2009

Workspace Management

Workspace Management

Workspace management is new in 9i. This feature allows you to version-enable tables, so that different users can have their own private versions of tables. Oracle internally keeps track of changes to different workspaces and you must resolve any update conflicts prior to merging them. Here are the steps to using workspace management:

  1. Version-enable one or more tables
  2. Create workspaces
  3. Grant privileges
  4. Access and use the workspace
  5. Resolve conflicts between original and versioned rows
  6. Refresh/merge workspaces
  7. Disable versioning tables
  8. Remove workspaces

Versioning-enabling works on the unit of a table. Different workspaces contain different table versions. A version-enabled table has the suffix _LT appended to its regular name.

You can only refresh or merge table versions after you have resolved any data conflicts (different row data) between them. Oracle provides package DBMS_WM and the new role WM_ADMIN_ROLE to manage the new workspace feature. Workspace management is installed by default, or login as user SYS and run OWMINST.PLB.