Documentos de Académico
Documentos de Profesional
Documentos de Cultura
SQL
hints
/*+ hint */
/*+ hint(argument) */
All hints except /*+ rule */ cause the CBO to be used. Therefore, it
is good practise to analyze the underlying tables if hints are used (or
the query is fully hinted.
There should be no schema names in hints. Hints must use aliases if
alias names are used for table names. So the following is wrong:
select /*+ index(scott.emp ix_emp) */ from scott.emp
emp_alias
better:
select /*+ index(emp_alias ix_emp) */ ... from scott.emp
emp_alias
Hint categories
Hints can be categorized as follows:
• ALL_ROWS
One of the hints that 'invokes' the Cost based optimizer
ALL_ROWS is usually used for batch processing or data
warehousing systems.
• FIRST_ROWS
One of the hints that 'invokes' the Cost based optimizer
FIRST_ROWS is usually used for OLTP systems.
• CHOOSE
One of the hints that 'invokes' the Cost based optimizer
This hint lets the server choose (between ALL_ROWS and
FIRST_ROWS, based on statistics gathered.
• RULE
The RULE hint should be considered deprecated as it is
dropped from Oracle9i2.
• CLUSTER
Performs a nested loop by the cluster index of one of the
tables.
• FULL
Performs full table scan.
• HASH
Hashes one table (full scan) and creates a hash index for
that table. Then hashes other table and uses hash index to
find corresponding records. Therefore not suitable for < or
> join conditions.
• ROWID
Retrieves the row by rowid
• INDEX
Specifying that index index_name should be used on table
tab_name: /*+ index (tab_name index_name) */
Specifying that the index should be used the the CBO
thinks is most suitable. (Not always a good choice).
Starting with Oracle 10g, the index hint can be described:
/*+ index(my_tab my_tab(col_1, col_2)) */. Using
the index on my_tab that starts with the columns col_1
and col_2.
• INDEX_ASC
• INDEX_COMBINE
• INDEX_DESC
• INDEX_FFS
• INDEX_JOIN
• NO_INDEX
• AND_EQUAL
The AND_EQUAL hint explicitly chooses an execution plan
that uses an access path that merges the scans on several
single-column indexes
• FACT
The FACT hint is used in the context of the star
transformation to indicate to the transformation that the
hinted table should be considered as a fact table.
• MERGE
• NO_EXPAND
• NO_EXPAND_GSET_TO_UNION
• NO_FACT
• NO_MERGE
• NOREWRITE
• REWRITE
• STAR_TRANSFORMATION
• USE_CONCAT
• DRIVING_SITE
• HASH_AJ
• HASH_SJ
• LEADING
• MERGE_AJ
• MERGE_SJ
• NL_AJ
• NL_SJ
• USE_HASH
• USE_MERGE
• USE_NL
• NOPARALLEL
• PARALLEL
• NOPARALLEL_INDEX
• PARALLEL_INDEX
• PQ_DISTRIBUTE
Additional Hints
• ANTIJOIN
• APPEND
If a table or an index is specified with nologging, this hint
applied with an insert statement produces a direct path
insert which reduces generation of redo.
• BITMAP
• BUFFER
• CACHE
• CARDINALITY
• CPU_COSTING
• DYNAMIC_SAMPLING
• INLINE
• MATERIALIZE
• NO_ACCESS
• NO_BUFFER
• NO_MONITORING
• NO_PUSH_PRED
• NO_PUSH_SUBQ
• NO_QKN_BUFF
• NO_SEMIJOIN
• NOAPPEND
• NOCACHE
• OR_EXPAND
• ORDERED
• ORDERED_PREDICATES
• PUSH_PRED
• PUSH_SUBQ
• QB_NAME
• RESULT_CACHE (Oracle 11g)
• SELECTIVITY
• SEMIJOIN
• SEMIJOIN_DRIVER
• STAR
The STAR hint forces a star query plan to be used, if
possible. A star plan has the largest table in the query last
in the join order and joins it with a nested loops join on a
concatenated index. The STAR hint applies when there are
at least three tables, the large table's concatenated index
has at least three columns, and there are no conflicting
access or join method hints. The optimizer also considers
different permutations of the small tables.
• SWAP_JOIN_INPUTS
• USE_ANTI
• USE_SEMI
Undocumented hints:
• BYPASS_RECURSIVE_CHECK
Workaraound for bug 1816154
• BYPASS_UJVC
• CACHE_CB
• CACHE_TEMP_TABLE
• CIV_GB
• COLLECTIONS_GET_REFS
• CUBE_GB
• CURSOR_SHARING_EXACT
• DEREF_NO_REWRITE
• DML_UPDATE
• DOMAIN_INDEX_NO_SORT
• DOMAIN_INDEX_SORT
• DYNAMIC_SAMPLING
• DYNAMIC_SAMPLING_EST_CDN
• EXPAND_GSET_TO_UNION
• FORCE_SAMPLE_BLOCK
• GBY_CONC_ROLLUP
• GLOBAL_TABLE_HINTS
• HWM_BROKERED
• IGNORE_ON_CLAUSE
• IGNORE_WHERE_CLAUSE
• INDEX_RRS
• INDEX_SS
• INDEX_SS_ASC
• INDEX_SS_DESC
• LIKE_EXPAND
• LOCAL_INDEXES
• MV_MERGE
• NESTED_TABLE_GET_REFS
• NESTED_TABLE_SET_REFS
• NESTED_TABLE_SET_SETID
• NO_FILTERING
• NO_ORDER_ROLLUPS
• NO_PRUNE_GSETS
• NO_STATS_GSETS
• NO_UNNEST
• NOCPU_COSTING
• OVERFLOW_NOMOVE
• PIV_GB
• PIV_SSF
• PQ_MAP
• PQ_NOMAP
• REMOTE_MAPPED
• RESTORE_AS_INTERVALS
• SAVE_AS_INTERVALS
• SCN_ASCENDING
• SKIP_EXT_OPTIMIZER
• SQLLDR
• SYS_DL_CURSOR
• SYS_PARALLEL_TXN
• SYS_RID_ORDER
• TIV_GB
• TIV_SSF
• UNNEST
• USE_TTT_FOR_GSETS
Thanks
Thanks to Guy Hengel who helped on this page.
Misc
Specifying a query block in a hint.
Select (SQL)
From Wikipedia, the free encyclopedia
Jump to: navigation, search
The SQL SELECT statement returns a result set of records from one or more tables.[1][2]
It retrieves zero or more rows from one or more base tables, temporary tables, or views in
a database. In most applications, SELECT is the most commonly used Data Manipulation
Language (DML) command. As SQL is a non-procedural language, SELECT queries
specify a result set, but do not specify how to calculate it: translating the query into an
executable "query plan" is left to the database system, more specifically to the query
optimizer.
Contents
[hide]
• 1 Examples
• 2 Limiting result rows
o 2.1 ROW_NUMBER() window function
o 2.2 RANK() window function
o 2.3 Non-standard syntax
o 2.4 Result limits
o 2.5 Hierarchical query
• 3 Window function
• 4 References
• 5 External links
[edit] Examples
C1 C2 C1 C2
1 a SELECT * FROM T; 1 a
2 b 2 b
C1 C2 C1
1 a SELECT C1 FROM T; 1
2 b 2
C1 C2 C1 C2
1 a SELECT * FROM T WHERE C1 = 1; 1 a
2 b
C1 C2 C1 C2
1 a SELECT * FROM T ORDER BY C1 DESC; 2 b
2 b 1 a
Given a table T, the query SELECT * FROM T will result in all the elements of all the
rows of the table being shown.
With the same table, the query SELECT C1 FROM T will result in the elements from the
column C1 of all the rows of the table being shown. This is similar to a projection in
Relational algebra, except that in the general case, the result may contain duplicate rows.
This is also known as a Vertical Partition in some database terms, restricting query output
to view only specified fields or columns.
With the same table, the query SELECT * FROM T WHERE C1 = 1 will result in all the
elements of all the rows where the value of column C1 is '1' being shown — in Relational
algebra terms, a selection will be performed, because of the WHERE clause. This is also
known as a Horizontal Partition, restricting rows output by a query according to specified
conditions.
• cursors, or
• By introducing SQL window function to the SELECT-statement
ROW_NUMBER() OVER may be used for a simple limit on the returned rows. E.g., to return
no more than ten rows:
( SELECT
COLUMNS
FROM tablename
) foo
ROW_NUMBER can be non-deterministic: if sort_key is not unique, each time you run
the query it is possible to get different row numbers assigned to any rows where sort_key
is the same. When sort_key is unique, each row will always get a unique row number.
The RANK() OVER window function acts like ROW_NUMBER, but may return more than
n rows in case of tie conditions. E.g., to return the top-10 youngest persons:
SELECT * FROM (
SELECT
person_id,
person_name,
age
FROM person
) AS foo
The above code could return more than ten rows, e.g. if there are two people of the same
age, it could return eleven rows.
Not all DBMSes support the mentioned window functions, and non-standard syntax has
to be used. Below, variants of the simple limit query for different DBMSes are listed:
SELECT * FROM T LIMIT 10 MySQL, PostgreSQL (also supports the standard, since
OFFSET 20 version 8.4), SQLite, H2
SELECT * from T WHERE
ROWNUM <= 10 Oracle (also supports the standard, since Oracle8i)
SELECT FIRST 10 * from T Ingres
SELECT FIRST 10 * FROM T
order by a Informix
Informix (row numbers are filtered after order by is
SELECT SKIP 20 FIRST 10
* FROM T order by c, d evaluated. SKIP clause was introduced in a v10.00.xC4
fixpack)
SELECT * FROM T FETCH DB2 (also supports the standard, in Linux, Windows, and
FIRST 10 ROWS ONLY Unix since DB2 v8, z/OS support added in v9)
SELECT TOP 10 * FROM T
MS SQL Server (also supports the standard, since SQL
Server 2005), Sybase ASE, MS Access
SELECT TOP 10 START AT Sybase SQL Anywhere (also supports the standard, since
20 * FROM T version 9.0.1)
SELECT FIRST 10 SKIP 20
* FROM T Interbase, Firebird
SELECT * FROM T ROWS 20
TO 30 Firebird (since version 2.1)
[edit] Hierarchical query
For example,
calculates the sum of the populations of all rows having the same city value as the current
row.
Partitions are specified using the OVER clause which modifies the aggregate. Syntax:
<OVER_CLAUSE> :: =
[ ORDER BY <expression> ] )
The OVER clause can partition and order the result set. Ordering is used for order-
relative functions such as row_number.
Adding Row
Number to SQL
SELECT result
Post Need
comments help?
Introduction
This article will explain
how we can add
sequence row number
to a SQL select query
starting from 1
onwards. This can be
achieved by using built
in SQL function ?
ROW_NUMBER()?. This
function simply
generates row number
for each row in the
result. You can specify
the partition and order
by criteria. This is how
it works:
SELECT EmployeeId,
EmployeeName,
Salary
FROM Employees
EmployeeId
EmployeeName
Salary
------------------
------------------
-
1002 Alden
4000
2343
Lawson
4500
2004
Barbra
4800
1105
Marsden
4500
3116 Mac
5000
Use of ROW_NUMBER()
will assign sequence
number to rows as:
SELECT
ROW_NUMBER()
OVER
(ORDER BY
EmployeeName) AS
Row,
EmployeeId,
EmployeeName,
Salary
FROM Employees
Row EmployeeId
EmployeeName
Salary
------------------
------------------
-------
1 1002
Alden
4000
2 2343
Lawson
4500
3 2004
Barbra
4800
4 1105
Marsden
4500
5 3116
Mac
5000
Using
ROW_NUMBER()
for calculating
Nth highest
salary
We can utilize this
function for calculating
Nth highest salary of a
employee. Suppose we
want to find employee
with 4th highest salary.
This can be done as:
SELECT * FROM
(SELECT
ROW_NUMBER()
OVER
(ORDER BY Salary)
AS Row,
EmployeeId
, EmployeeName,
Salary
FROM
Employees) AS EMP
WHERE Row = 4
Row EmployeeId
EmployeeName
Salary
------------------
------------------
-------
4 1105
Marsden
4500
Using
ROW_NUMBER()
in case of
pagination
This can also be used
for getting rows which
belongs to a particular
page only. This is very
common scenario of a
business application
where we have lots of
rows in database and
we want to filter based
on page number.
SELECT * FROM
(SELECT
ROW_NUMBER()
OVER
(ORDER BY
EmployeeName) AS
Row,
EmployeeId
, EmployeeName,
Salary
FROM
Employees) AS EMP
------------------
------------------
-------
2 2343
Lawson
4500
3 2004
Barbra
4800
4 1105
Marsden
4500
Post Need
comments help?