You are on page 1of 9

Module 9: Secondary Indexes and Table Scans

After completing this module, you will be able to:


Define Secondary Indexes.
Distinguish between the implementation of unique and
non-unique secondary indexes.
Define Full Table Scans and what causes them.
Describe the operation of a Full Table Scan in a parallel
environments.

Secondary Indexes
There are 3 general ways to access a table:
Primary Index access

(one AMP access)

Secondary Index access (two or all AMP access)


Full Table Scan

(all AMP access)

A secondary Index provides an alternate path to the rows of a table.


A table can have from 0 to 32 secondary indexes.
Secondary Indexes:
Do not effect table distribution.
Add overhead, both in terms of disk space and maintenance.
May be added or dropped dynamically as needed.
Are chosen to improve table performance.

Choosing a Secondary Index


A Secondary Index may be defined ...
at table creation

(CREATE TABLE)

following table creation

(CREATE INDEX)

may be up to 16 columns (V2R4.1); 64 columns (V2R5.0)


NUSI

USI
If the index choice of column(s) is
unique, it is called a USI.
Unique Secondary Index)

If the index choice of column(s) is nonunique, it is called a NUSI.


Non-Unique Secondary Index

Accessing a row via a USI is a 2 AMP


operation.

Accessing row(s) via a NUSI is an all AMP


operation.

CREATE UNIQUE INDEX

CREATE INDEX

(Employee_Number) ON Employee;

(Last_Name) ON Employee;

Notes:
Secondary Indexes cause an internal sub-table to be built.

Dropping the index causes the sub-table to be deleted.

Unique Secondary Index (USI) Access


Message Passing Layer

Create USI
CREATE UNIQUE INDEX
(Cust) ON Customer;

AMP 1

AMP 2

USI Subtable

Access via USI

RowID
244, 1
505, 1
744, 4
757, 1

SELECT *
FROM
Customer
WHERE Cust = 56;

Cust
74
77
51
27

RowID
884, 1
639, 1
915, 9
388, 1

AMP 3

USI Subtable
RowID
135, 1
296, 1
602, 1
969, 1

Cust
98
84
56
49

Table ID
100

Row Hash USI Value


602

to MPL

56

Cust
31
40
45
95

RowID
638, 1
640, 1
471, 1
778, 3

RowID
175, 1
489, 1
838, 4
919, 1

Cust
37
72
12
62

RowID
107, 1
717, 2
147, 2
822, 1

778

Message Passing Layer

USI Value = 56
Hashing
Algorithm

RowID
288, 1
339, 1
372, 2
588, 1

USI Subtable

Row Hash Unique Val

100

Customer
Table ID = 100

USI Subtable

RowID
555, 6
536, 5
778, 7
147, 1

Table ID

PE

AMP 4

AMP 1

AMP 2

AMP 3

AMP 4

Base Table

Base Table

Base Table

Base Table

RowID Cust Name


USI
107, 1 37 White
536, 5 84 Rice
638, 1 31 Adams
640, 1 40 Smith

Phone
NUPI
555-4444
666-5555
111-2222
222-3333

RowID Cust Name


USI
471, 1 45 Adams
555, 6 98 Brown
717, 2 72 Adams
884, 1 74 Smith

Phone
NUPI
444-6666
333-9999
666-7777
555-6666

RowID Cust Name


USI
147, 1 49 Smith
147, 2 12 Young
388, 1 27 Jones
822, 1 62 Black

Phone
NUPI
111-6666
777-4444
222-8888
444-5555

RowID Cust Name


USI
639, 1 77 Jones
778, 3 95 Peters
778, 7 56 Smith
915, 9 51 Marsh

Phone
NUPI
777-6666
555-7777
555-7777
888-2222

Non-Unique Secondary Index (NUSI) Access


Message Passing Layer

Create NUSI
AMP 1

CREATE INDEX (Name) ON


Customer;

AMP 2

NUSI Subtable

Access via NUSI


SELECT *
FROM
Customer
WHERE Name = 'Adams';

RowID
432, 8
448, 1
567, 3
656, 1

Name
Smith
White
Adams
Rice

RowID
640, 1
107, 1
638, 1
536, 5

AMP 3

NUSI Subtable
RowID Name
432, 3 Smith
567, 2 Adams
852, 1

Brown

RowID
884, 1
471, 1
717, 2
555, 6

AMP 4

NUSI Subtable
RowID
432, 1
448, 4
567, 6
770, 1

Name
Smith
Black
Jones
Young

RowID
147, 1
822, 1
338, 1
147, 2

NUSI Subtable
RowID
155, 1
396, 1
432, 5
567, 1

Name
Marsh
Peters
Smith
Jones

RowID
915, 9
778, 3
778, 7
639, 1

PE
Customer
NUSI Value = 'Adams'
Table ID = 100
Hashing
Algorithm

Table ID
100

to MPL

AMP 2

Base Table

Row Hash NUSI Value


567

AMP 1

Adams

RowID Cust Name


NUSI
107, 1 37 White
536, 5 84 Rice
638, 1 31 Adams
640, 1 40 Smith

Phone
NUPI
555-4444
666-5555
111-2222
222-3333

AMP 3

Base Table
RowID Cust Name
NUSI
471, 1 45 Adams
555, 6 98 Brown
717, 2 72 Adams
884, 1 74 Smith

Phone
NUPI
444-6666
333-9999
666-7777
555-6666

AMP 4

Base Table
RowID Cust Name
NUSI
147, 1 49 Smith
147, 2 12 Young
388, 1 27 Jones
822, 1 62 Black

Phone
NUPI
111-6666
777-4444
222-8888
444-5555

Base Table
RowID Cust Name
NUSI
639, 1 77 Jones
778, 3 95 Peters
778, 7 56 Smith
915, 9 51 Marsh

Phone
NUPI
777-6666
555-7777
555-7777
888-2222

Comparison of Primary and Secondary Indexes


Index Feature
Required?
Number per Table1

Primary

Secondary

Yes

No

0 - 32

Max Number of Columns (V2R4.1)

16

16

Max Number of Columns (V2R5)

64

64

Unique or Non-unique

Both

Both

Affects Row Distribution

Yes

No

Created/Dropped Dynamically

No

Yes

Improves Access

Yes

Yes

Multiple Data Types

Yes

Yes

Separate Physical Structure

No

Sub-table

Extra Processing Overhead

No

Yes

May be Partitioned (V2R5)

Yes

No

Full Table Scans


Every row of the table must be read.
All AMPs scan their portion of the table in parallel.
Fast and efficient on Teradata due to parallelism.
Full table scans typically occur when either:

An index is not used in the query


An index is used in a non-equality test
Customer
Cust_ID
USI

Cust_Name

Cust_Phone
NUPI

Examples of Full Table Scans:


SELECT * FROM Customer WHERE Cust_Phone LIKE '524-_ _ _ _';
SELECT * FROM Customer WHERE Cust_Name = 'Davis';
SELECT * FROM Customer WHERE Cust_ID > 1000;

Review Questions
Fill each box with either Yes, No, or the appropriate
number.
USI
Access
# AMPs
# rows
Parallel Operation
Uses Hash Maps
Uses Separate Sub-table
Reads all data blocks of table

NUSI
Access

FTS

Module 9: Review Question Answers


Fill each box with either Yes, No, or the appropriate
number.
USI
Access

NUSI
Access

FTS

# AMPs

All

All

# rows

0-1

0-n

0 - all

Parallel Operation

Uses Hash Maps

Uses Separate Sub-table

Reads all data blocks of table

You might also like