Что такое oracle sql

7 SQL

This chapter provides an overview of the Structured Query Language ( SQL ) and how Oracle Database processes SQL statements.

This chapter includes the following topics:

Introduction to SQL

SQL (pronounced sequel ) is the set-based, high-level declarative computer language with which all programs and users access data in an Oracle database. Although some Oracle tools and applications mask SQL use, all database operations are performed using SQL. Any other data access method circumvents the security built into Oracle Database and potentially compromises data security and integrity.

SQL provides an interface to a relational database such as Oracle Database. SQL unifies tasks such as the following in one consistent language:

Creating, replacing, altering, and dropping objects

Inserting, updating, and deleting table rows

Controlling access to the database and its objects

Guaranteeing database consistency and integrity

SQL Data Access

There are two broad families of computer languages: declarative languages that are nonprocedural and describe what should be done, and procedural languages such as C++ and Java that describe how things should be done. SQL is declarative in the sense that users specify the result that they want, not how to derive it. The SQL language compiler performs the work of generating a procedure to navigate the database and perform the desired task.

SQL enables you to work with data at the logical level. You need be concerned with implementation details only when you want to manipulate the data. For example, the following statement queries records for employees whose last name begins with K :

SQL Standards

Oracle strives to follow industry-accepted standards and participates actively in SQL standards committees. Industry-accepted committees are the American National Standards Institute (ANSI) and the International Organization for Standardization (ISO). Both ANSI and the ISO/IEC have accepted SQL as the standard language for relational databases.

The latest SQL standard was adopted in July 2003 and is often called SQL:2003. One part of the SQL standard, Part 14, SQL/XML (ISO/IEC 9075-14) was revised in 2006 and is often referred to as SQL/XML:2006.

Oracle SQL includes many extensions to the ANSI/ISO standard SQL language, and Oracle Database tools and applications provide additional statements. The tools SQL*Plus, SQL Developer, and Oracle Enterprise Manager enable you to run any ANSI/ISO standard SQL statement against an Oracle database and any additional statements or functions available for those tools.

Oracle Database SQL Language Reference for an explanation of the differences between Oracle SQL and standard SQL

SQL*Plus User’s Guide and Reference for SQL*Plus commands, including their distinction from SQL statements

Overview of SQL Statements

SQL reserved words have special meaning in SQL and should not be used for any other purpose. For example, SELECT and UPDATE are reserved words and should not be used as table names.

A SQL statement must be the equivalent of a complete SQL sentence, such as:

Oracle Database only runs complete SQL statements. A fragment such as the following generates an error indicating that more text is required:

Oracle SQL statements are divided into the following categories:

Data Definition Language (DDL) Statements

Delete all the data in schema objects without removing the structure of these objects ( TRUNCATE ).

Add a comment to the data dictionary ( COMMENT ).

DDL enables you to alter attributes of an object without altering the applications that access the object. For example, you can add a column to a table accessed by a human resources application without rewriting the application. You can also use DDL to alter the structure of objects while database users are performing work in the database.

Example 7-1 uses DDL statements to create the plants table and then uses DML to insert two rows in the table. The example then uses DDL to alter the table structure, grant and revoke privileges on this table to a user, and then drop the table.

Example 7-1 DDL Statements

An implicit COMMIT occurs immediately before the database executes a DDL statement and a COMMIT or ROLLBACK occurs immediately afterward. In Example 7-1, two INSERT statements are followed by an ALTER TABLE statement, so the database commits the two INSERT statements. If the ALTER TABLE statement succeeds, then the database commits this statement; otherwise, the database rolls back this statement. In either case the two INSERT statements have already been committed.

«Overview of Database Security» to learn about privileges and roles

Data Manipulation Language (DML) Statements

Data manipulation language ( DML ) statements query or manipulate data in existing schema objects. Whereas DDL statements enable you to change the structure of the database, DML statements enable you to query or change the contents. For example, ALTER TABLE changes the structure of a table, whereas INSERT adds one or more rows to the table.

DML statements are the most frequently used SQL statements and enable you to:

Retrieve or fetch data from one or more tables or views ( SELECT ).

Add new rows of data into a table or view ( INSERT ) by specifying a list of column values or using a subquery to select and manipulate existing data.

Change column values in existing rows of a table or view ( UPDATE ).

Update or insert rows conditionally into a table or view ( MERGE ).

Remove rows from tables or views ( DELETE ).

View the execution plan for a SQL statement ( EXPLAIN PLAN ). See «How Oracle Database Processes DML».

Lock a table or view, temporarily limiting access by other users ( LOCK TABLE ).

Oracle Database 2 Day Developer’s Guide to learn how to query and manipulate data

SELECT Statements

Table 7-1 shows two required keywords and two keywords that are commonly found in a SELECT statement. The table also associates capabilities of a SELECT statement with the keywords.

Table 7-1 Keywords in a SQL Statement

Specifies which columns should be shown in the result. Projection produces a subset of the columns in the table.

Specifies the tables or views from which the data should be retrieved.

Specifies the order in which the rows should be shown.

Joins

A join is a query that combines rows from two or more tables, views, or materialized views. Example 7-2 joins the employees and departments tables ( FROM clause), selects only rows that meet specified criteria ( WHERE clause), and uses projection to retrieve data from two columns ( SELECT ). Sample output follows the SQL statement.

Example 7-2 Sample Join

Figure 7-1 graphically represents the operations of projection and selection in the join shown in Example 7-2.

Figure 7-1 Projection and Selection

Что такое oracle sql. Смотреть фото Что такое oracle sql. Смотреть картинку Что такое oracle sql. Картинка про Что такое oracle sql. Фото Что такое oracle sql
Description of «Figure 7-1 Projection and Selection»

Join types include the following:

Oracle Database SQL Language Reference for detailed descriptions and examples of joins

Subqueries and Implicit Queries

A subquery is a SELECT statement nested within another SQL statement. Subqueries are useful when you must execute multiple queries to solve a single problem.

Example 7-3 Subquery

The only DML statement that does not necessarily include a query component is an INSERT statement with a VALUES clause. For example, an INSERT INTO TABLE mytable VALUES (1) statement does not retrieve rows before inserting a row.

Transaction Control Statements

Transaction control statements manage the changes made by DML statements and group DML statements into transactions. These statements enable you to:

Make changes to a transaction permanent ( COMMIT ).

Undo the changes in a transaction, since the transaction started ( ROLLBACK ) or since a savepoint ( ROLLBACK TO SAVEPOINT ). A savepoint is a user-declared intermediate marker within the context of a transaction.

The ROLLBACK command ends a transaction, but ROLLBACK TO SAVEPOINT does not.

Set a point to which you can roll back ( SAVEPOINT ).

Establish properties for a transaction ( SET TRANSACTION ).

Specify whether a deferrable integrity constraint is checked following each DML statement or when the transaction is committed ( SET CONSTRAINT ).

Session Control Statements

Session control statements enable you to:

Alter the current session by performing a specialized function, such as enabling and disabling SQL tracing ( ALTER SESSION ).

Enable and disable roles, which are groups of privileges, for the current session ( SET ROLE ).

The following example turns on SQL tracing for the session and then enables all roles granted in the current session except dw_manager :

Session control statements do not implicitly commit the current transaction.

Oracle Database SQL Language Reference for ALTER SESSION syntax and semantics

System Control Statement

Following are examples of system control statements:

The ALTER SYSTEM statement does not implicitly commit the current transaction.

Oracle Database SQL Language Reference for ALTER SYSTEM syntax and semantics

Embedded SQL Statements

Embedded SQL statements incorporate DDL, DML, and transaction control statements within a procedural language program. They are used with the Oracle precompilers. Embedded SQL is one approach to incorporating SQL in your procedural language applications. Another approach is to use a procedural API such as Open Database Connectivity ( ODBC) or Java Database Connectivity ( JDBC).

Embedded SQL statements enable you to:

Assign variable names ( DECLARE STATEMENT ).

Initialize descriptors ( DESCRIBE ).

Specify how error and warning conditions are handled ( WHENEVER ).

Retrieve data from the database ( FETCH ).

Overview of the Optimizer

To understand how Oracle Database processes SQL statements, it is necessary to understand the part of the database called the optimizer (also known as the query optimizer or cost-based optimizer ). All SQL statements use the optimizer to determine the most efficient means of accessing the specified data.

Use of the Optimizer

To execute a DML statement, Oracle Database may have to perform many steps. Each step either retrieves rows of data physically from the database or prepares them for the user issuing the statement.

Many different ways of processing a DML statement are often possible. For example, the order in which tables or indexes are accessed can vary. The steps that the database uses to execute a statement greatly affect how quickly the statement runs. The optimizer generates execution plans describing possible methods of execution.

Evaluation of expressions and conditions

Inspection of integrity constraints to learn more about the data and optimize based on this metadata

Choice of optimizer goals

Choice of access paths

Choice of join orders

The optimizer generates most of the possible ways of processing a query and assigns a cost to each step in the generated execution plan. The plan with the lowest cost is chosen as the query plan to be executed.

You can obtain an execution plan for a SQL statement without executing the plan. Only an execution plan that the database actually uses to execute a query is correctly termed a query plan.

You can influence optimizer choices by setting the optimizer goal and by gathering representative statistics for the optimizer. For example, you may set the optimizer goal to either of the following:

The ALL_ROWS hint instructs the optimizer to get the last row of the result to the client application as fast as possible.

Initial response time

The FIRST_ROWS hint instructs the optimizer to get the first row to the client as fast as possible.

A typical end-user, interactive application would benefit from initial response time optimization, whereas a batch-mode, non-interactive application would benefit from total throughput optimization.

Oracle Database Performance Tuning Guide for more information about the optimizer and using hints

Optimizer Components

The optimizer contains three main components, which are shown in Figure 7-2.

Figure 7-2 Optimizer Components

Что такое oracle sql. Смотреть фото Что такое oracle sql. Смотреть картинку Что такое oracle sql. Картинка про Что такое oracle sql. Фото Что такое oracle sql
Description of «Figure 7-2 Optimizer Components»

The input to the optimizer is a parsed query (see «SQL Parsing»). The optimizer performs the following operations:

The optimizer receives the parsed query and generates a set of potential plans for the SQL statement based on available access paths and hints.

The optimizer estimates the cost of each plan based on statistics in the data dictionary. The cost is an estimated value proportional to the expected resource use needed to execute the statement with a particular plan.

The optimizer compares the costs of plans and chooses the lowest-cost plan, known as the query plan, to pass to the row source generator (see «SQL Row Source Generation»).

Query Transformer

The query transformer determines whether it is helpful to change the form of the query so that the optimizer can generate a better execution plan. The input to the query transformer is a parsed query, which is represented by a set of query blocks.

Estimator

The estimator determines the overall cost of a given execution plan. The estimator generates three different types of measures to achieve this goal:

This measure represents the number of rows in a row set.

This measure represents units of work or resource used. The query optimizer uses disk I/O, CPU usage, and memory usage as units of work.

If statistics are available, then the estimator uses them to compute the measures. The statistics improve the degree of accuracy of the measures.

Plan Generator

The optimizer automatically manages plans and ensures that only verified plans are used. SQL Plan Management (SPM) allows controlled plan evolution by only using a new plan after it has been verified to be perform better than the current plan.

Diagnostic tools such as the EXPLAIN PLAN statement enable you to view execution plans chosen by the optimizer. EXPLAIN PLAN shows the query plan for the specified SQL query if it were executed now in the current session. Other diagnostic tools are Oracle Enterprise Manager and the SQL*Plus AUTOTRACE command. Example 7-6 shows the execution plan of a query when AUTOTRACE is enabled.

Oracle Database Performance Tuning Guide to learn about the optimizer components

Access Paths

An access path is the way in which data is retrieved from the database. For example, a query that uses an index has a different access path from a query that does not. In general, index access paths are best for statements that retrieve a small subset of table rows. Full scans are more efficient for accessing a large portion of a table.

The database can use several different access paths to retrieve data from a table. The following is a representative list:

This type of scan reads all rows from a table and filters out those that do not meet the selection criteria. The database sequentially scans all data blocks in the segment, including those under the high water mark that separates used from unused space (see «Segment Space and the High Water Mark»).

The rowid of a row specifies the data file and data block containing the row and the location of the row in that block. The database first obtains the rowids of the selected rows, either from the statement WHERE clause or through an index scan, and then locates each selected row based on its rowid.

This scan searches an index for the indexed column values accessed by the SQL statement (see «Index Scans»). If the statement accesses only columns of the index, then Oracle Database reads the indexed column values directly from the index.

A hash scan is used to locate rows in a hash cluster, where all rows with the same hash value are stored in the same data block (see «Overview of Hash Clusters». The database first obtains the hash value by applying a hash function to a cluster key value specified by the statement. Oracle Database then scans the data blocks containing rows with this hash value.

The optimizer chooses an access path based on the available access paths for the statement and the estimated cost of using each access path or combination of paths.

Optimizer Statistics

Optimizer statistics are a collection of data that describe details about the database and the objects in the database. The statistics provide a statistically correct picture of data storage and distribution usable by the optimizer when evaluating access paths.

Optimizer statistics include the following:

These include the number of rows, number of blocks, and average row length.

These include the number of distinct values and nulls in a column and the distribution of data.

These include the number of leaf blocks and index levels.

These include CPU and I/O performance and utilization.

Oracle Database gathers optimizer statistics on all database objects automatically and maintains these statistics as an automated maintenance task. You can also gather statistics manually using the DBMS_STATS package. This PL/SQL package can modify, view, export, import, and delete statistics.

Optimizer statistics are created for the purposes of query optimization and are stored in the data dictionary. These statistics should not be confused with performance statistics visible through dynamic performance views.

Optimizer Hints

A hint is a comment in a SQL statement that acts as an instruction to the optimizer. Sometimes the application designer, who has more information about a particular application’s data than is available to the optimizer, can choose a more effective way to run a SQL statement. The application designer can use hints in SQL statements to specify how the statement should be run.

For example, suppose that your interactive application runs a query that returns 50 rows. This application initially fetches only the first 25 rows of the query to present to the end user. You want the optimizer to generate a plan that gets the first 25 records as quickly as possible so that the user is not forced to wait. You can use a hint to pass this instruction to the optimizer as shown in the SELECT statement and AUTOTRACE output in Example 7-4.

Example 7-4 Execution Plan for SELECT with FIRST_ROWS Hint

The execution plan in Example 7-4 shows that the optimizer chooses an index on the employees.department_id column to find the first 25 rows of employees whose department ID is over 50. The optimizer uses the rowid retrieved from the index to retrieve the record from the employees table and return it to the client. Retrieval of the first record is typically almost instantaneous.

Example 7-5 shows the same statement, but without the optimizer hint.

Example 7-5 Execution Plan for SELECT with No Hint

In this case, the database cannot return the first row to the client until the index range scan of EMP_DEPARTMENT_IX completes. Thus, this generated plan would take longer to return the first record. Unlike the plan in Example 7-4, which accesses the table by index rowid, the plan in Example 7-5 uses multiblock I/O, resulting in large reads. The reads enable the last row of the entire result set to be returned more rapidly.

Overview of SQL Processing

This section explains how Oracle Database processes SQL statements. Specifically, the section explains the way in which the database processes DDL statements to create objects, DML to modify data, and queries to retrieve data.

Stages of SQL Processing

Figure 7-3 depicts the general stages of SQL processing: parsing, optimization, row source generation, and execution. Depending on the statement, the database may omit some of these steps.

Figure 7-3 Stages of SQL Processing

Что такое oracle sql. Смотреть фото Что такое oracle sql. Смотреть картинку Что такое oracle sql. Картинка про Что такое oracle sql. Фото Что такое oracle sql
Description of «Figure 7-3 Stages of SQL Processing»

SQL Parsing

During the parse call, the database performs the following checks:

Syntax Check

Oracle Database must check each SQL statement for syntactic validity. A statement that breaks a rule for well-formed SQL syntax fails the check. For example, the following statement fails because the keyword FROM is misspelled as FORM :

Semantic Check

The semantics of a statement are its meaning. Thus, a semantic check determines whether a statement is meaningful, for example, whether the objects and columns in the statement exist. A syntactically correct statement can fail a semantic check, as shown in the following example of a query of a nonexistent table:

Shared Pool Check

When a user submits a SQL statement, the database searches the shared SQL area to see if an existing parsed statement has the same hash value. The hash value of a SQL statement is distinct from the following values:

Memory address for the statement

Oracle Database uses the SQL ID to perform a keyed read in a lookup table. In this way, the database obtains possible memory addresses of the statement.

Hash value of an execution plan for the statement

A SQL statement can have multiple plans in the shared pool. Each plan has a different hash value. If the same SQL ID has multiple plan hash values, then the database knows that multiple plans exist for this SQL ID.

Parse operations fall into the following categories, depending on the type of statement submitted and the result of the hash check:

During the hard parse, the database accesses the library cache and data dictionary cache numerous times to check the data dictionary. When the database accesses these areas, it uses a serialization device called a latch on required objects so that their definition does not change (see «Latches»). Latch contention increases statement execution time and decreases concurrency.

Soft parses can vary in the amount of work they perform. For example, configuring the session shared SQL area can sometimes reduce the amount of latching in the soft parses, making them «softer.»

In general, a soft parse is preferable to a hard parse because the database skips the optimization and row source generation steps, proceeding straight to execution.

Figure 7-4 is a simplified representation of a shared pool check of an UPDATE statement in a dedicated server architecture.

Figure 7-4 Shared Pool Check

Что такое oracle sql. Смотреть фото Что такое oracle sql. Смотреть картинку Что такое oracle sql. Картинка про Что такое oracle sql. Фото Что такое oracle sql
Description of «Figure 7-4 Shared Pool Check»

If a check determines that a statement in the shared pool has the same hash value, then the database performs semantic and environment checks to determine whether the statements mean the same. Identical syntax is not sufficient. For example, suppose two different users log in to the database and issue the following SQL statements:

Even if two statements are semantically identical, an environmental difference can force a hard parse. In this case, the environment is the totality of session settings that can affect execution plan generation, such as the work area size or optimizer settings. Consider the following series of SQL statements executed by a single user:

In the preceding example, the same SELECT statement is executed in three different optimizer environments. Consequently, the database creates three separate shared SQL areas for these statements and forces a hard parse of each statement.

Oracle Database Performance Tuning Guide to learn how to configure the shared pool

SQL Optimization

As explained in «Overview of the Optimizer», query optimization is the process of choosing the most efficient means of executing a SQL statement. The database optimizes queries based on statistics collected about the actual data being accessed. The optimizer uses the number of rows, the size of the data set, and other factors to generate possible execution plans, assigning a numeric cost to each plan. The database uses the plan with the lowest cost.

The database must perform a hard parse at least once for every unique DML statement and performs optimization during this parse. DDL is never optimized unless it includes a DML component such as a subquery that requires optimization.

Oracle Database Performance Tuning Guide for detailed information about the query optimizer

SQL Row Source Generation

A row source is a row set returned by a step in the execution plan along with a control structure that can iteratively process the rows. The row source can be a table, view, or result of a join or grouping operation.

An ordering of the tables referenced by the statement

An access method for each table mentioned in the statement

A join method for tables affected by join operations in the statement

Data operations such as filter, sort, or aggregation

Example 7-6 Execution Plan

SQL Execution

During execution, the SQL engine executes each row source in the tree produced by the row source generator. This step is the only mandatory step in DML processing.

Figure 7-5 Row Source Tree

Что такое oracle sql. Смотреть фото Что такое oracle sql. Смотреть картинку Что такое oracle sql. Картинка про Что такое oracle sql. Фото Что такое oracle sql
Description of «Figure 7-5 Row Source Tree»

In Figure 7-5, each node of the tree acts as a row source, which means that each step of the execution plan either retrieves rows from the database or accepts rows from one or more row sources as input. The SQL engine executes each row source as follows:

Step 6 uses a full table scan to retrieve all rows from the departments table.

Step 5 uses a full table scan to retrieve all rows from the jobs table.

Steps indicated by the clear boxes operate on row sources.

Step 1 performs another hash join, accepting row sources from Steps 2 and 6, joining each row from the Step 6 source to its corresponding row in Step 2, and returning the result to the client.

In some execution plans the steps are iterative and in others sequential. The plan shown in Example 7-6 is iterative because the SQL engine moves from index to table to client and then repeats the steps.

During execution, the database reads the data from disk into memory if the data is not in memory. The database also takes out any locks and latches necessary to ensure data integrity and logs any changes made during the SQL execution. The final stage of processing a SQL statement is closing the cursor.

Oracle Database Performance Tuning Guide for detailed information about execution plans and the EXPLAIN PLAN statement

How Oracle Database Processes DML

Result set rows can be fetched either a row at a time or in groups. In the fetch stage, the database selects rows and, if requested by the query, orders the rows. Each successive fetch retrieves another row of the result until the last row has been fetched.

In general, the database cannot determine for certain the number of rows to be retrieved by a query until the last row is fetched. Oracle Database retrieves the data in response to fetch calls, so that the more rows the database reads, the more work it performs. For some queries the database returns the first row as quickly as possible, whereas for others it creates the entire result set before returning the first row.

Read Consistency

In general, a query retrieves data by using the Oracle Database read consistency mechanism. This mechanism, which uses undo data to show past versions of data, guarantees that all data blocks read by a query are consistent to a single point in time.

Data Changes

DML statements that must change data use the read consistency mechanism to retrieve only the data that matched the search criteria when the modification began. Afterward, these statements retrieve the data blocks as they exist in their current state and make the required modifications. The database must perform other actions related to the modification of the data such as generating redo and undo data.

How Oracle Database Processes DDL

Oracle Database processes DDL differently from DML. For example, when you create a table, the database does not optimize the CREATE TABLE statement. Instead, Oracle Database parses the DDL statement and carries out the command.

The database process DDL differently because it is a means of defining an object in the data dictionary. Typically, Oracle Database must parse and execute many recursive SQL statements to execute a DDL command. Suppose you create a table as follows:

Typically, the database would run dozens of recursive statements to execute the preceding statement. The recursive SQL would perform actions such as the following:

Issue a COMMIT before executing the CREATE TABLE statement

Verify that user privileges are sufficient to create the table

Determine which tablespace the table should reside in

Ensure that the tablespace quota has not been exceeded

Ensure that no object in the schema has the same name

Insert rows that define the table into the data dictionary

Issue a COMMIT if the DDL statement succeeded or a ROLLBACK if it did not

Oracle Database Advanced Application Developer’s Guide to learn about SQL processing for application developers

Источник

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *

KeywordRequired?DescriptionCapability