Difference Between Teradata Primary Index and Primary Key

Embed Size (px)

Citation preview

  • 7/23/2019 Difference Between Teradata Primary Index and Primary Key

    1/4

    Difference between Teradata Primary Index

    and Primary Key

    ByRajOn October 15, 2012 1 Comment

    mailShare

    One must not get confused between Primary Key and Primary Index in Teradata. Primary KEY

    is more of a logical thing however Primary INDEX is more of physical thing. In Teradata,

    Primary INDEX is used for finding best access path for data retrieval and data insertion andPrimary KEY is used for finding each rows uniquely just like in other RDBMS. So below are

    few differences between PRIMARY KEY and PRIMARY INDEX:

    PRIMARY KEY PRIMARY INDEX

    1 PRIMARY KEY cannot be NULL PRIMARY INDEX can be NULL

    2PRIMARY KEY is notmandatory in Teradata

    PRIMARY INDEX is mandatoryIn Teradata

    3PRIMARY KEY does not helpin data distribution.

    PRIMARY INDEX helps indatadistribution.

    4PRIMARY KEY should beunique.

    PRIMARY INDEX can be UNIQUE

    (Unique Primary Index)

    or NON UNIQUE (Primary Index).

    5PRIMARY KEY is logicalimplementation.

    PRIMARY INDEX is physicalimplementation.

    Now we will see few scenarios to see how these two are handled in Teradata:

    http://usefulfreetips.com/Teradata-SQL-Tutorial/author/Raj/http://usefulfreetips.com/Teradata-SQL-Tutorial/author/Raj/http://usefulfreetips.com/Teradata-SQL-Tutorial/author/Raj/http://usefulfreetips.com/Teradata-SQL-Tutorial/difference-between-teradata-primary-index-and-primary-key/#commentshttp://usefulfreetips.com/Teradata-SQL-Tutorial/difference-between-teradata-primary-index-and-primary-key/#commentshttp://usefulfreetips.com/Teradata-SQL-Tutorial/difference-between-teradata-primary-index-and-primary-key/#commentshttp://www.blog.usefulfreetips.com/2012/07/01/how-teradata-distributes-rows/http://www.blog.usefulfreetips.com/2012/07/01/how-teradata-distributes-rows/http://www.blog.usefulfreetips.com/2012/07/01/how-teradata-distributes-rows/http://www.blog.usefulfreetips.com/2012/07/01/how-teradata-distributes-rows/http://www.blog.usefulfreetips.com/2012/07/01/how-teradata-distributes-rows/http://www.blog.usefulfreetips.com/2012/07/01/how-teradata-distributes-rows/http://www.blog.usefulfreetips.com/2012/07/01/how-teradata-distributes-rows/http://usefulfreetips.com/Teradata-SQL-Tutorial/difference-between-teradata-primary-index-and-primary-key/#commentshttp://usefulfreetips.com/Teradata-SQL-Tutorial/author/Raj/
  • 7/23/2019 Difference Between Teradata Primary Index and Primary Key

    2/4

    PRIMARY KEY and PRIMARY INDEX in TERADATA

    a) I have not defined PRIMARY INDEX or PRIMARY KEY on table what will happen

    now: In this case, Teradata will check if any column is defined as UNIQUE, then it will make

    that column as UNIQUE PRIMARY INDEX else first column will be created as PRIMARY

    INDEX.

    b) I have not defined PRIMARY INDEX however a column is defined as PRIMARY

    KEY: In this case, Teradata will make the PRIMARY KEY column as UNIQUE PRIMARYINDEX of the table.

    c) I have defined both PRIMARY KEY and PRIMARY INDEX on different column: In this

    case, Teradata will make PRIMARY KEY column as UNIQUE SECONDARY INDEX i.e.

    UNIQUE INDEX on the table.

    So one must understand the importance of PRIMARY INDEX in Teradata. Generally,

    PRIMARY KEY concept is taken care by UNIQUE PRIMARY INDEX in Teradataenvironment.

    Secondary Index in Teradata with Example

    ByRajOn December 29, 2012 2 Comments

    http://usefulfreetips.com/Teradata-SQL-Tutorial/author/Raj/http://usefulfreetips.com/Teradata-SQL-Tutorial/author/Raj/http://usefulfreetips.com/Teradata-SQL-Tutorial/author/Raj/http://usefulfreetips.com/Teradata-SQL-Tutorial/secondary-index-in-teradata-with-example/#commentshttp://usefulfreetips.com/Teradata-SQL-Tutorial/secondary-index-in-teradata-with-example/#commentshttp://usefulfreetips.com/Teradata-SQL-Tutorial/secondary-index-in-teradata-with-example/#commentshttp://usefulfreetips.com/Teradata-SQL-Tutorial/wp-content/uploads/2012/10/PRIMARY-KEY-and-PRIMARY-INDEX-in-TERADATA.jpghttp://usefulfreetips.com/Teradata-SQL-Tutorial/secondary-index-in-teradata-with-example/#commentshttp://usefulfreetips.com/Teradata-SQL-Tutorial/author/Raj/
  • 7/23/2019 Difference Between Teradata Primary Index and Primary Key

    3/4

    mailShare

    What is Secondary Index in Teradata ? Secondary Index in Teradata provides an alternate path to

    retrieve the data. It is used only for data retrieval and it has nothing to do with data storage. Fordata storage, Teradata use PRIMARY INDEX. So why do we need Secondary Index , when

    Primary Index is available for Data Storage as well as Data Retrieval ? There may be some

    situations when the query may not be using Primary Index column for data retrieval. In such

    cases, data retrieval will be very slow. In such situations we can create Secondary Index on suchcolumns which are not part of PRIMARY Index however are used very often in JOIN conditions

    or other conditions for data retrieval. Like PRIMARY INDEX, we have two types ofSECONDARY Index too:

    Unique Secondary Index:

    CREATE UNIQUE INDEX [COLUMN_NAME] ON TABLENAME;

    Non-Unique Secondary Index:

    CREATE INDEX [COLUMN_NAME] ON TABLENAME;

    Creating SECONDARY INDEX may help in performance optimization however it also comes atsome cost in terms of resources. Whenever a SECONDARY index is created on table , a subtableis created on all the AMPs which hold following information:

    SECONDARY INDEX VALUE || SECONDARY INDEX ROW_ID || PRIMARY INDEX

    ROW_ID

    So whenever we query using column defined as SECONDARY INDEX, all AMPs are asked tocheck for their sub-table if they hold that value. If yes, then AMPs retrieve the corresponding

    PRIMARY INDEX row_id from their subtable. Then the AMP holding the PRIMARY INDEXrow_id is asked to retrieve respective records. Hence, Data Retrieval via Secondary Index is

    always 2 AMP or more AMP operation. For NUSI [Non Unique Secondary Index] subtable is

    created in the same AMP holding the PRIMARY row_id. However for USI[Unique SecondaryIndex], subtables hold the information about rows of different AMPs. Secondary Index avoids

  • 7/23/2019 Difference Between Teradata Primary Index and Primary Key

    4/4

    FULL TABLE scan. However one should collect STATS on Secondary Index columns in order

    to allow Optimizer to use Secondary Index and not Full Table Scan.

    Advantages of Secondary Index:

    Avoids FULL Table Scan by providing alternate data retrieval path.

    Enhances performances. Can be dropped and created anytime. A table may have multiple Secondary Index defined where as only one Primary Index is

    permissible.

    Disadvantages of Secondary Index:

    Needs extra storage space for SUBTABLE. Needs extra I/O to maintain SUBTABLE. Collect STATS is required in order to avoid FULL TABLE SCAN.

    To Drop Secondary Index use below command:

    DROP INDEX [COLUMN_NAME] ON TABLENAME;