{"id":28529,"date":"2022-03-10T20:30:42","date_gmt":"2022-03-10T20:30:42","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=28529"},"modified":"2022-12-23T06:47:55","modified_gmt":"2022-12-23T06:47:55","slug":"secondary-index-in-teradata","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/secondary-index-in-teradata\/","title":{"rendered":"Secondary Index in Teradata"},"content":{"rendered":"\n<figure class=\"wp-block-table is-style-stripes\"><table class=\"has-vivid-cyan-blue-background-color has-background\"><thead><tr><th>#<\/th><th>Contents<\/th><\/tr><\/thead><tbody><tr><td>1.<\/td><td>What is Secondary Index in Teradata?<\/td><\/tr><tr><td>2.<\/td><td>When we can Define Secondary Index?<\/td><\/tr><tr><td>3.<\/td><td>Types of Secondary Index<\/td><\/tr><tr><td>4.<\/td><td>Unique Secondary Index (USI)<\/td><\/tr><tr><td>5.<\/td><td>Non Unique Secondary Index (NUSI)<\/td><\/tr><tr><td>6.<\/td><td>Value-Ordered NUSI<\/td><\/tr><tr><td>7.<\/td><td>Advantage and Disadvantage of Secondary Index<\/td><\/tr><tr><td>8.<\/td><td>Primary\u00a0Index vs. Secondary Index<\/td><\/tr><tr><td>9.<\/td><td>Additional Topic<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>To Understand More About Secondary Index in Teradata we have to go through below points:<\/p>\n\n\n\n<p>There are 3 general ways to access a table:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Primary Index access (one AMP access)<\/li><li>Secondary Index access (two or all AMP access)<\/li><li>Full Table Scan (all AMP access)<\/li><\/ul>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is Secondary Index in Teradata?<\/h2>\n\n\n\n<p>A secondary index provides an alternate pathto the rows of a table.<\/p>\n\n\n\n<p>A secondary index can be used to maintain uniquenesswithin a column or set of columns.<\/p>\n\n\n\n<p>A table can have from 0 to 32 secondary indexes.<\/p>\n\n\n\n<p><strong>Secondary Indexes:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Do not affect table distribution.<\/li><li>Add overhead, both in terms of disk space and maintenance.<\/li><li>May be added or dropped dynamically as needed.<\/li><li>Are chosen to improve table performance.<\/li><\/ul>\n\n\n\n<p><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">When we can Define Secondary Index?<\/h3>\n\n\n\n<p>A Secondary Index may be defined:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>At table creation (CREATE TABLE)<\/li><li>Also can define after table creation  Using (CREATE INDEX)<\/li><li>may be up to 64 columns<\/li><\/ul>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Types of Secondary Index<\/h2>\n\n\n\n<p><strong>There are two types of secondary index<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Unique Secondary Index (USI)<\/li><li>Non Unique Secondary Index (NUSI)<\/li><\/ul>\n\n\n\n<p><strong>Unique Secondary Index (USI)<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>If the index choice of column(s) is unique, it is called a USI. <\/li><li>Unique Secondary Index Accessing a row via a USI is a 2AMP operation.<\/li><\/ul>\n\n\n\n<p><strong>Syntax to create USI:<\/strong><\/p>\n\n\n\n<p><em>CREATE UNIQUE INDEX (EmpNumber) ON Employee;<\/em><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>Non<\/strong> <strong>Unique Secondary Index (NUSI)<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>If the index choice of column(s) is non-unique, it is called a NUSI.<\/li><li>Non-Unique Secondary Index Accessing row(s) via a NUSI is an all AMP operation<\/li><\/ul>\n\n\n\n<p><strong>Syntax to create USI:<\/strong><\/p>\n\n\n\n<p><em>CREATE INDEX (LastName) ON Employee;<\/em><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Unique Secondary Index (USI) <\/h2>\n\n\n\n<p>A Subtable is created on each AMP anytime a secondary index is created. A secondary index\u00a0requires a\u00a0<strong>separate physical structure<\/strong>\u00a0(the\u00a0subtable\u00a0).<\/p>\n\n\n\n<p><strong>What\u00a0is in a Unique Secondary Index (USI) Subtable?<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/03\/image-25.png\" alt=\"\" class=\"wp-image-28533\" width=\"585\" height=\"254\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/03\/image-25.png 926w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/03\/image-25-300x131.png 300w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/03\/image-25-768x334.png 768w\" sizes=\"auto, (max-width: 585px) 100vw, 585px\" \/><\/figure>\n\n\n\n<p>The USI Subtable contains two columns:<\/p>\n\n\n\n<p>1. Emp_No&nbsp;(The USI column)<\/p>\n\n\n\n<p>2. Row-ID\u00a0of the real Primary Index of the base table<\/p>\n\n\n\n<p>Inside the secondary index subtable is the column Emp_No (USI Column). Then, there is the corresponding Row-ID of the real Primary Index of the table. The USI subtable\u2019s Primary Index is Emp_No, and it is hashed and has a Row-ID like a normal table.<\/p>\n\n\n\n<p><strong>A\u00a0Unique Secondary Index (USI) Subtable is hashed<\/strong>:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/03\/image-26.png\" alt=\"\" class=\"wp-image-28534\" width=\"600\" height=\"310\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/03\/image-26.png 824w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/03\/image-26-300x155.png 300w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/03\/image-26-768x397.png 768w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/figure>\n\n\n\n<ul class=\"wp-block-list\"><li>The USI Subtable spreads the rows evenly among the AMPs. The Primary Index of the Subtable is Emp_No (The USI column).<\/li><li>Subtable rows are hashed by their Primary Index (Emp_No) and distributed evenly. They contain the Row-ID of the base row, thus providing a pointer to the AMP and row.<\/li><\/ul>\n\n\n\n<p><strong>How\u00a0the Parsing Engine uses the USI Subtable<\/strong>:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/03\/image-27.png\" alt=\"\" class=\"wp-image-28535\" width=\"540\" height=\"334\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/03\/image-27.png 893w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/03\/image-27-300x186.png 300w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/03\/image-27-768x476.png 768w\" sizes=\"auto, (max-width: 540px) 100vw, 540px\" \/><\/figure>\n\n\n\n<ul class=\"wp-block-list\"><li>A\u00a0USI is a Two-AMP Operation.<\/li><li>The first AMP is assigned to read the subtable and the second the base table. <\/li><li>Two binary searches are performed in total, and one row is returned.<\/li><\/ul>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Non Unique Secondary Index (NUSI)<\/h2>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/03\/image-28.png\" alt=\"\" class=\"wp-image-28536\" width=\"556\" height=\"330\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/03\/image-28.png 808w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/03\/image-28-300x178.png 300w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/03\/image-28-768x456.png 768w\" sizes=\"auto, (max-width: 556px) 100vw, 556px\" \/><\/figure>\n\n\n\n<p>A Subtable is created on each AMP. <\/p>\n\n\n\n<p><strong>What\u00a0is in a Unique Secondary Index (USI) Subtable?<\/strong><\/p>\n\n\n\n<p><br>The NUSI Subtable only contains two columns:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li> First_Name\u00a0(The NUSI column)<\/li><li>Row-ID\u00a0of the real Primary Index of the base table<\/li><\/ul>\n\n\n\n<p>The NUSI rows get their own Row-ID, but they are not hashed to different AMPs and stay AMP local. That is why NUSI know as AMP locally.<\/p>\n\n\n\n<p><strong>Non-\u00a0Unique Secondary Index (NUSI) Subtable is AMP Local<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/03\/image-29.png\" alt=\"\" class=\"wp-image-28537\" width=\"479\" height=\"230\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/03\/image-29.png 810w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/03\/image-29-300x144.png 300w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/03\/image-29-768x369.png 768w\" sizes=\"auto, (max-width: 479px) 100vw, 479px\" \/><\/figure>\n\n\n\n<p>The NUSI Subtable is AMP local because all values in the subtable are of those in the base table (on the same AMP).<\/p>\n\n\n\n<p><strong>How\u00a0the Parsing Engine uses the NUSI Subtable<\/strong>:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/03\/image-30.png\" alt=\"\" class=\"wp-image-28538\" width=\"445\" height=\"226\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/03\/image-30.png 802w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/03\/image-30-300x152.png 300w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/03\/image-30-768x390.png 768w\" sizes=\"auto, (max-width: 445px) 100vw, 445px\" \/><\/figure>\n\n\n\n<ul class=\"wp-block-list\"><li>First_Name is a Non-Unique Secondary Index!<\/li><li>The Parsing Engine will order each AMP to check if they have a \u2018Kyle\u2019 in their NUSI Subtable.<\/li><li>Each AMP will simultaneously perform a binary search on their NUSI subtable.<\/li><li>If an AMP has a \u2018Kyle\u2019 the PE will order them to retrieve the base row (AMP Local).<\/li><li>If there are 50 AMPs then all 50 AMPs perform a binary search simultaneously and if they find a \u201cKyle\u201d they perform another binary search on their local base table.<\/li><\/ul>\n\n\n\n<p><strong>Note:<\/strong> \u00a0A NUSI query always searches all AMPs, but the intent is not to do a Full Table Scan. If there are 50 AMPs, then a minimum of 50 binary searches are done.<\/p>\n\n\n\n<p><strong>Drop Secondary Index<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Dropping named secondary indexes<\/li><\/ul>\n\n\n\n<p>         DROP INDEX indexname ON tutorial_db.employee; <\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Dropping unnamed secondary indexes<\/li><\/ul>\n\n\n\n<p>         DROP INDEX(co) ON tutorial_db.employee;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Value-Ordered NUSI<\/h2>\n\n\n\n<p>It is called a Value Ordered NUSI because subtable is sorted numerically based on actual column value instead being sorted by Secondary Index Value hash.Works best for the range queries.Can be defined only on &#8216;numeric&#8217; &amp; &#8216;date&#8217; data types.<\/p>\n\n\n\n<p><strong>Creating\u00a0a Value-Ordered NUSI<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/03\/image-31.png\" alt=\"\" class=\"wp-image-28539\" width=\"471\" height=\"323\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/03\/image-31.png 789w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/03\/image-31-300x206.png 300w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/03\/image-31-768x527.png 768w\" sizes=\"auto, (max-width: 471px) 100vw, 471px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Advantage and Disadvantage of Secondary Index<\/h2>\n\n\n\n<p><strong>Advantage<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>A table can have up to 32 secondary indexes (including join\/hash indexes).<\/li><li>Maximum of 64 columns can be added in a single secondary index for a table.<\/li><li>Does not affect data distribution for table and can be added or dropped as needed.<\/li><li>Improves table access performance by providing alternative path.<\/li><\/ul>\n\n\n\n<p><strong>Disadvantage<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Secondary index on multiple columns is less usable.Index would be used only when all columns are used in the WHERE clause.<\/li><li>Adds overhead, both in terms of disk space and maintenance.<\/li><\/ul>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Primary\u00a0Index vs. Secondary Index<\/h2>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"794\" height=\"451\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/03\/image-32.png\" alt=\"\" class=\"wp-image-28540\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/03\/image-32.png 794w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/03\/image-32-300x170.png 300w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/03\/image-32-768x436.png 768w\" sizes=\"auto, (max-width: 794px) 100vw, 794px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Additional Topic<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Full Table Scans<\/h3>\n\n\n\n<p>A full table scan is another way to access data without using any Primary or Secondary Indexes.<\/p>\n\n\n\n<p>Every row of the table must be read.<\/p>\n\n\n\n<p>Full table scans typically occur when either:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>An index is not used in the query<\/li><li>An index is used in a non-equality<\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p># Contents 1. What is Secondary Index in Teradata? 2. When we can Define Secondary Index? 3. Types of Secondary Index 4. Unique Secondary Index (USI) 5. Non Unique Secondary&#8230; <\/p>\n","protected":false},"author":48,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[7338],"tags":[],"class_list":["post-28529","post","type-post","status-publish","format-standard","hentry","category-teradata"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/28529","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/48"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=28529"}],"version-history":[{"count":1,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/28529\/revisions"}],"predecessor-version":[{"id":28541,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/28529\/revisions\/28541"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=28529"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=28529"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=28529"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}