Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Semestr2 / 1 - Oracle / Oracle selected docs / Database concepts.pdf
Скачиваний:
30
Добавлен:
12.05.2015
Размер:
6.96 Mб
Скачать

Indexes

How Indexes Are Searched

Index Unique Scan

Index unique scan is one of the most efficient ways of accessing data. This access method is used for returning the data from B-tree indexes. The optimizer chooses a unique scan when all columns of a unique (B-tree) index are specified with equality conditions.

Steps in Index Unique Scans

1.Start with the root block.

2.Search the block keys for the smallest key greater than or equal to the value.

3.If key is greater than the value, then follow the link before this key to the child block.

4.If key is equal to the value, then follow this link to the child block.

5.If no key is greater than or equal to the value in Step 2, then follow the link after the highest key in the block.

6.Repeat steps 2 through 4 if the child block is a branch block.

7.Search the leaf block for key equal to the value.

8.If key is found, then return the ROWID.

9.If key is not found, then the row does not exist.

Figure 10–8 shows an example of an index unique scan and is described in the text that follows the figure.

10-38 Oracle9i Database Concepts

Indexes

Figure 10–8 Example of an Index Unique Scan

Di Lu Rh

B

C

Cr

 

F

H

Kar

 

Mo

P

Ph

 

Sam

St

Su

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Karl, ROWID

Kathy, ROWID

Kim, ROWID

Lance, ROWID

 

Luis, ROWID

Martin, ROWID

Mary, ROWID

May, ROWID

Mike, ROWID

 

Morris, ROWID

Nancy ROWID

Nick, ROWID

Nicole, ROWID

Norm, ROWID

 

Pablo, ROWID

Patrick, ROWID

Paula, ROWID

Peter, ROWID

 

Phil, ROWID

Pierre, ROWID

Rachel, ROWID

Rajiv, ROWID

Raoul, ROWID

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

If searching for Patrick:

In the root block, Rh is the smallest key >= Patrick.

Follow the link before Rh to branch block (N, P, Ph).

In this block, Ph is the smallest key >= Patrick.

Follow the link before Ph to leaf block (Pablo, Patrick, Paula, Peter).

In this block, search for key Patrick = Patrick.

Found Patrick = Patrick, return (KEY, ROWID).

If searching for Meg:

In the root block, Rh is the smallest key >= Meg.

Follow the link before Rh to branch block (N, P, Ph).

In this block, Mo is the smallest key >= Meg.

Follow the link before Mo to leaf block (Luis,… , May, Mike).

In this block, search for key = Meg.

Schema Objects 10-39

Indexes

Did not find key = Meg, return 0 rows.

Index Range Scan

Index range scan is a common operation for accessing selective data. It can be bounded (bounded on both sides) or unbounded (on one or both sides). Data is returned in the ascending order of index columns. Multiple rows with identical values are sorted (in ascending order) by the ROWIDs.

How Index Range Scans Work Index range scans can happen on both unique and non-unique indexes. B-tree non-unique indexes are identical to the unique B-tree indexes. However, they allow multiple values for the same key.

For a range scan, you can specify an equality condition. For example:

name = ‘ALEX’ - start key = ‘ALEX’, end key = ‘ALEX’

Alternatively, specify an interval bounded by start key and end key. For example:

name LIKE ‘AL%’ - start key = ‘AL, end key < ‘AM’

order_id BETWEEN 100 AND 120 - start key = 100, end key = 120

Or, specify just a start key or an end key (unbounded range scan). For example:

order_book_date > SYSDATE - 30 (orders booked in last month)

employee_hire_date < SYSDATE - 3650 (employees with more than a decade of service)

Figure 10–9 shows an example of a bounded range scan and is described in the text that follows the figure.

10-40 Oracle9i Database Concepts

Indexes

Figure 10–9 Example of a Bounded Range Scan

Di Lu Rh

B

C

Cr

 

F

H

Kar

 

N

P

Ph

 

Sam

St

Su

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Karl, ROWID

Kathy, ROWID

Kim, ROWID

Lance, ROWID

 

Luis, ROWID

Mark, ROWID

Mary, ROWID

Mike, ROWID

Mike, ROWID

 

Nancy, ROWID

Nancy ROWID

Nancy, ROWID

Nicole, ROWID

Norm, ROWID

 

Pablo, ROWID

Paula, ROWID

Paula, ROWID

Peter, ROWID

 

Phil, ROWID

Pierre, ROWID

Rachel, ROWID

Rajiv, ROWID

Raoul, ROWID

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Steps in a Bounded Range Scan

1.Start with the root block.

2.Search the block keys for the smallest key greater than or equal to the start key.

3.If key > start key, then follow the link before this key to the child block.

4.If key = start key, then follow this link to the child block.

5.If no key is greater than or equal to the start key in Step 2, then follow the link after the highest key in the block.

6.Repeat steps 2 through 4 if the child block is a branch block.

7.Search the leaf block keys for the smallest key greater than or equal to the start key.

8.While key <= end key:

If the key columns meet all WHERE clause conditions, then return the (value,

ROWID).

Follow the link to the right.

Schema Objects 10-41

Indexes

Here, the range scans make use of the fact that all the leaf nodes are linked from left to right. In Step 7, extra filtering conditions on the index columns can be applied before accessing the table by ROWID.

Range scans bounded on the left (unbounded on the right) start the same. However, they do not check for the end point. They continue until they reach the right-most leaf key.

Range scans bounded on the right traverse the index tree to the left-most leaf key and then follow step #6 and # 7 until they reach a key greater then the specified condition.

With range scans using the non-unique B-tree index, if searching for Nancy:

Start key = ‘Nancy’, end key < ‘Nancy’.

In the root block, Rh is the smallest key >= start key.

Follow the link before Rh to branch block (N, P, Ph).

In this block, P is the smallest key >= start key.

Follow the link before P to leaf block (Nancy, …, Nicole, Norm).

In this block, Nancy is the smallest key >= start key.

Because Nancy <= end key, return the (KEY, ROWID).

Next key Nancy <= end key, return the (KEY, ROWID).

Next key Nancy <= end key, return the (KEY, ROWID).

Next key Nicole > end key, terminate the range scan.

If searching for ‘P%’:

Start key = ‘P’, end key < ‘Q’.

In the root block, Rh is the smallest key >= start key.

Follow the link before Rh to branch block (N, P, Ph).

In this block, P is the smallest key = start key.

Follow this link to leaf block (Pablo,…, Peter).

In this block, Pablo is the smallest key >= start key.

Because Pablo <= end key, return the (KEY, ROWID).

Next key Paula <= end key, return the (KEY, ROWID).

Next key Paula <= end key, return the (KEY, ROWID).

10-42 Oracle9i Database Concepts

Indexes

Next key Phil <= end key, return the (KEY, ROWID). Next key Pierre <= end key, return the (KEY, ROWID). Next key Rachel > end key, terminate the range scan.

Index Range Scan Descending

Steps in a Bounded Descending Range Scan For a descending range scan (like with the normal range scan), specify an equality condition or an interval.

1.Start with the root block.

2.Search the block keys for the biggest key less than or equal to the end key.

3.Follow the link to the child block.

4.If no key is less than or equal to the end key in step 2, then follow the link before the lowest key in the block.

5.Repeat steps 2 through 4 if the child block is a branch block.

6.Search the leaf block keys for the biggest key less than or equal to the end key.

7.While key >= start key:

If the key columns meet all WHERE clause conditions, then return the (value,

ROWID).

Follow the link to the left.

Here, the range scans make use of the fact that all the leaf nodes are linked from right to left.

Figure 10–10 shows examples of a bounded range scan and is described in the text that follows the figure.

Schema Objects 10-43

Indexes

Figure 10–10 Examples of Range Scans Using the Non-Unique B-tree Index

Di Lu Rh

B

C

Cr

 

F

H

Kar

 

N

P

Ph

 

Sam

St

Su

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Karl, ROWID

Kathy, ROWID

Kim, ROWID

Lance, ROWID

 

Luis, ROWID

Mark, ROWID

Mary, ROWID

Mike, ROWID

Mike, ROWID

 

Nancy, ROWID

Nancy ROWID

Nancy, ROWID

Nicole, ROWID

Norm, ROWID

 

Pablo, ROWID

Paula, ROWID

Paula, ROWID

Peter, ROWID

 

Phil, ROWID

Pierre, ROWID

Rachel, ROWID

Rajiv, ROWID

Raoul, ROWID

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

If searching for Nancy:

Start key = ‘Nancy’, end key < ‘Nancy’.

In the root block, Lu is the biggest key <= end key.

Follow the link to branch block (N, P, Ph).

In this branch block, N is the biggest key <= end key.

Follow the link after N to leaf block (Nancy, …, Nicole, Norm).

In this leaf block, Nancy is the biggest key <= end key.

Nancy >= start key, return the (KEY, ROWID).

Prev key Nancy >= start key, return the (KEY, ROWID).

Prev key Nancy >= start key, return the (KEY, ROWID).

Prev key Mike < start key, terminate the range scan.

If searching for ‘P%’:

Start key = ‘P’, end key < ‘Q’.

10-44 Oracle9i Database Concepts

Соседние файлы в папке Oracle selected docs