Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... ·...

151
® Teradata SQL and Studio for ADAPT 54986-PNC Version 15.10.2 Student Guide

Transcript of Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... ·...

Page 1: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

®

Teradata SQL and Studio for ADAPT

54986-PNC Version 15.10.2 Student Guide

Page 2: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Trademarks The following names are registered names or trademarks and are used throughout this manual. The product or products described in this book are licensed products of Teradata Corporation or its affiliates. Teradata, BYNET, DBC/1012, DecisionCast, DecisionFlow, DecisionPoint, Eye logo design, InfoWise, Meta Warehouse, MyCommerce, SeeChain, SeeCommerce, SeeRisk, Teradata Decision Experts, Teradata Source Experts, WebAnalyst, and You’ve Never Seen Your Business Like This Before, and Raising Intelligence are trademarks or registered trademarks of Teradata Corporation or its affiliates. Adaptec and SCSISelect are trademarks or registered trademarks of Adaptec, Inc. AMD Opteron and Opteron are trademarks of Advanced Micro Devices, Inc. BakBone and NetVault are trademarks or registered trademarks of BakBone Software, Inc. EMC2, PowerPath, SRDF, and Symmetrix are registered trademarks of EMC2 Corporation. GoldenGate is a trademark of GoldenGate Software, a division of Oracle Corporation. Hewlett-Packard and HP are registered trademarks of Hewlett-Packard Company. Intel, Pentium, and XEON are registered trademarks of Intel Corporation. IBM, CICS, RACF, Tivoli, z/OS, and z/VM are registered trademarks of International Business

Machines Corporation. Linux is a registered trademark of Linus Torvalds. LSI and Engenio are registered trademarks of LSI Corporation. Microsoft, Active Directory, Windows, Windows NT, and Windows Server are registered

trademarks of Microsoft Corporation in the United States and other countries. Novell and SUSE are registered trademarks of Novell, Inc., in the United States and other

countries. QLogic and SANbox trademarks or registered trademarks of QLogic Corporation. SAS and SAS/C are trademarks or registered trademarks of SAS Institute Inc. SPARC is a registered trademark of SPARC International, Inc. Symantec, NetBackup, and VERITAS are trademarks or registered trademarks of Symantec

Corporation or its affiliates in the United States and other countries. Unicode is a collective membership mark and a service mark of Unicode, Inc. UNIX is a registered trademark of The Open Group in the United States and other countries. Other product and company names mentioned herein may be the trademarks of their respective owners.

The materials included in this book are a licensed product of Teradata Corporation.

Miamisburg, Ohio, U.S.A. All Rights Reserved.

Page 3: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Table of Contents

Teradata SQL and Studio for ADAPT Version 15.10.2

Module 1 - Teradata Studio Features

Teradata Studio Overview............................................................................................................ 1-3 Creating Connections to Teradata ................................................................................................ 1-4 Modular Display .......................................................................................................................... 1-6 Teradata Studio Perspectives ....................................................................................................... 1-8 Customizing Studio Perspectives ................................................................................................. 1-9 Query Development Perspective ................................................................................................ 1-10 Data Source Explorer View ....................................................................................................... 1-11 Customizing Preferences in Data Source Explorer .................................................................... 1-12 Data Source Explorer – Display Options ................................................................................... 1-14 Data Source Explorer –User Choice Option .............................................................................. 1-15 Creating a SQL Statement (SQL Editor) ................................................................................... 1-16 Opening SQL Editor .................................................................................................................. 1-18 Working in SQL Editor .............................................................................................................. 1-20 SQL Query Builder .................................................................................................................... 1-21 Using Query Builder .................................................................................................................. 1-22 Adding Tables Using Query Builder.......................................................................................... 1-23 Creating Joins in Query Builder ................................................................................................. 1-24 Applying Query Filters and Other Options in Query Builder .................................................... 1-25 SQL Editor Preferences – Editor Font ....................................................................................... 1-26 SQL Editor Preferences – Text Editor Options ......................................................................... 1-27 SQL Editor Preferences – Customizing SQL Assistance .......................................................... 1-28 Commenting Lines of SQL ........................................................................................................ 1-29 Limiting the Number of Rows Returned (SAMPLE) ................................................................ 1-30 Result Set Viewer Overview ...................................................................................................... 1-31 Result Set Customization ........................................................................................................... 1-32 Exporting Result Sets ................................................................................................................. 1-35 SQL History View ..................................................................................................................... 1-37 SQL History Output ................................................................................................................... 1-38 Options to Save Files ................................................................................................................. 1-40 Module 1: Summary ................................................................................................................... 1-41 Module 1: Review Questions ..................................................................................................... 1-42 Module 1: Lab Exercises ............................................................................................................ 1-43

Page 4: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Module 2 - Locating and Using Object Information

Viewing Database Objects in Data Source Explorer ................................................................... 2-3 Teradata View .............................................................................................................................. 2-5 Searching for Objects in Data Source Explorer ........................................................................... 2-6 SQL Shortcuts in Data Source Explorer ...................................................................................... 2-7 Shortcuts to Typing Object Names .............................................................................................. 2-8 Help and Show Commands ......................................................................................................... 2-9 HELP View Command .............................................................................................................. 2-10 Locating View Information ....................................................................................................... 2-11 Viewing the SQL Source for a View ......................................................................................... 2-12 HELP Macro Command ............................................................................................................ 2-13 The Teradata “SHOW View” Command .................................................................................. 2-14 Other SQL SHOW Commands .................................................................................................. 2-15 Useful Built-In Functions .......................................................................................................... 2-16 Finding Tables, Views, Macros, and Stored Procedures ........................................................... 2-17 Finding and Understanding Columns ........................................................................................ 2-18 Module 2: Summary .................................................................................................................. 2-20 Module 2: Review Questions ..................................................................................................... 2-21 Module 2: Lab Exercise ............................................................................................................. 2-22

Module 3 - Understanding Space in Teradata

Databases and Users Defined ...................................................................................................... 3-4 Teradata Database Space ............................................................................................................. 3-6 How Space is Managed in Teradata ............................................................................................ 3-8 How is Spool Space Used ............................................................................................................ 3-9 Spool Space Usage – Join Example ........................................................................................... 3-10 Spool Usage – “Derived” Table Example ................................................................................. 3-11 Spool Usage – Volatile Table Example ..................................................................................... 3-12 Why Do Users Have Spool Limits? ........................................................................................... 3-13 Module 3: Summary .................................................................................................................. 3-14 Module 3: Review Questions ..................................................................................................... 3-15

Page 5: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Module 4 - EXPLAINS and Working with Problem Queries

EXPLAIN Facility ....................................................................................................................... 4-3 A Note About Explain Times ....................................................................................................... 4-4 EXPLAIN Terminology ............................................................................................................... 4-5 Row and Time Estimates ............................................................................................................. 4-6 Common PNC Error Messages .................................................................................................. 4-10 Column/Parameter Does Not Exist ............................................................................................ 4-11 Float Precision Error .................................................................................................................. 4-12 Aggregation Error ...................................................................................................................... 4-13 Numeric Overflow Error ............................................................................................................ 4-14 Failed Conversion Error ............................................................................................................. 4-15 Invalid Timestamp ..................................................................................................................... 4-16 Invalid Operation on DateTime or Interval ................................................................................ 4-17 Object Errors – Column Not Found ........................................................................................... 4-18 Object Errors – Column is Ambiguous ...................................................................................... 4-19 Object Errors – Object Does Not Exist or User Does Not Have Access Right ......................... 4-20 Object Errors Example ............................................................................................................... 4-21 Workload Management Errors ................................................................................................... 4-22 Mistakes on Table Aliasing ........................................................................................................ 4-23 Table Aliasing Mistake Example ............................................................................................... 4-24 Data Conversion Example 1....................................................................................................... 4-25 Data Conversion Example 2....................................................................................................... 4-26 Matching Data Types ................................................................................................................. 4-27 ADAPT Best Practices – Filter by “Time_Period” .................................................................... 4-28 ADAPT Best Practices – Filter by “Time_Period” Range Example ......................................... 4-29 ADAPT Best Practices – Only Include Needed Columns ......................................................... 4-30 ADAPT Best Practices – Avoid Functions on Join Conditions and Where Clauses ................. 4-31 ADAPT Best Practices – Using Between .................................................................................. 4-32 ADAPT Best Practices – Options to Consider ........................................................................... 4-33 Common Syntax Differences – Hierarchical Joins .................................................................... 4-34 Common Syntax Differences – Outer Joins ............................................................................... 4-35 Common Syntax Differences – Nulls ........................................................................................ 4-36 Functions Common to Teradata and Oracle............................................................................... 4-37 Recommended Coding Conventions .......................................................................................... 4-38 Module 4: Summary ................................................................................................................... 4-39 Module 4: Review Questions ..................................................................................................... 4-40 Module 4: Lab Exercises ............................................................................................................ 4-41

Module 5 - Course Survey

http://www.tensupport.com/surveys/ ........................................................................................... 5-2

Appendix A: Review Question Solutions Appendix B: Lab Exercise Solutions

Page 6: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Notes:

Page 7: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Teradata Studio Features Page 1-1

Module 1

Teradata Studio Features

After completing this module, you should be able to:

• Logon and use Teradata Studio to submit SQL to Teradata.• Understand the basic components of Studio Window• Connect to Teradata and use Teradata Studio to submit SQL to

Teradata• Use the Data Source Explorer GUI to locate database objects and

assist with queries• Apply preferences to Teradata Studio to tailor its usage• Use Data Export options in Teradata Studio

Page 8: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Page 1-2 Teradata Studio Features

Table of Contents Teradata Studio Overview ........................................................................................................... 1-3 Creating Connections to Teradata ............................................................................................... 1-4 Modular Display .......................................................................................................................... 1-6 Teradata Studio Perspectives ....................................................................................................... 1-8 Customizing Studio Perspectives ................................................................................................ 1-9 Query Development Perspective ............................................................................................... 1-10 Data Source Explorer View ....................................................................................................... 1-11 Customizing Preferences in Data Source Explorer ................................................................... 1-12 Data Source Explorer – Display Options ................................................................................... 1-14 Data Source Explorer –User Choice Option .............................................................................. 1-15 Creating a SQL Statement (SQL Editor) ................................................................................... 1-16 Opening SQL Editor .................................................................................................................. 1-18 Working in SQL Editor ............................................................................................................. 1-20 SQL Query Builder .................................................................................................................... 1-21 Using Query Builder .................................................................................................................. 1-22 Adding Tables Using Query Builder ......................................................................................... 1-23 Creating Joins in Query Builder ................................................................................................ 1-24 Applying Query Filters and Other Options in Query Builder ................................................... 1-25 SQL Editor Preferences – Editor Font ....................................................................................... 1-26 SQL Editor Preferences – Text Editor Options ......................................................................... 1-27 SQL Editor Preferences – Customizing SQL Assistance .......................................................... 1-28 Commenting Lines of SQL ........................................................................................................ 1-29 Limiting the Number of Rows Returned (SAMPLE) ................................................................ 1-30 Result Set Viewer Overview ..................................................................................................... 1-31 Result Set Customization ........................................................................................................... 1-32 Result Set Customization (continued) ....................................................................................... 1-34 Exporting Result Sets ................................................................................................................ 1-35 Exporting Result Sets (continued) ............................................................................................. 1-36 SQL History View ..................................................................................................................... 1-37 SQL History Output ................................................................................................................... 1-38 Options to Save Files ................................................................................................................. 1-40 Module 1: Summary .................................................................................................................. 1-41 Module 1: Review Questions ..................................................................................................... 1-42 Module 1: Lab Exercises ........................................................................................................... 1-43 Module 1: Lab Exercises (continued) ........................................................................................ 1-44

Page 9: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Teradata Studio Features Page 1-3

Teradata Studio Overview Teradata Studio is a query tool written for users who submit SQL to Teradata and other JDBC compliant databases. It is intended for SQL-proficient developers who know how to formulate queries for processing on Teradata or other JDBC/ODBC-compliant Databases. Used as a discovery tool, Teradata Studio catalogs submitted instructions to arrive at a derived answer Teradata Studio is built on the Eclipse® Rich Client Platform (RCP), taking advantage of the RCP framework for building and deploying native GUI applications. It extends the Eclipse Data Tools Platform (DTP) to enhance Teradata Database access. Teradata Studio delivers the following capabilities.

• Display information about database objects • Modular display that allows you to move panels around, creating a custom look and feel. • Data Source Explorer:

◦ View database objects, such as databases, tables, macros, stored procedures, user-defined functions, user-defined types, views, and triggers.

◦ Menu options to run stored procedures, macros, and user-defined functions. ◦ Find objects.

• Maintain a historical record of submitted queries with timings and status information. • Export result sets to Excel, XML or text files. • Import data sets from Excel, XML or text files into Teradata.

Teradata Studio can be run on Windows, Linux, and Mac OSX operating systems.

Teradata Studio is a client-based graphical interface used to write and execute SQL.

Key Features include the following:

• Customizable modular display• Data Source Explorer• SQL Editor• SQL Query Builder• SQL History

Operating systems that Teradata Studio supports:• Windows• Linux• Mac OSX

Page 10: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Page 1-4 Teradata Studio Features

Creating Connections to Teradata When you start Teradata Studio, you will be prompted for a connection. You can create connections to your database using the Connection Profile Wizard. The wizard is invoked from the Data Source Explorer by right clicking on the Database Connections folder.

• Create connection profiles using the Teradata JDBC driver • LDAP, Kerberos, stored password optional

Teradata Studio provides an option to create connection profiles for additional database servers other than Teradata. You must provide the specific database server JDBC drivers for databases other than Teradata. The Connection Profile Wizard is based on a driver template that references an embedded Teradata JDBC driver. Enter the database server name, user name, password, and default database name into the wizard dialog screen.

Page 11: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Teradata Studio Features Page 1-5

Or right click on the Database Connections folder to create a new connection.

Select Teradata Database from the Connection Profile Types, enter a Name and click Next.

Enter the server name, user name and password.

Click New Connection Profile

Page 12: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Page 1-6 Teradata Studio Features

Modular Display The Teradata Studio display includes the Teradata Studio Toolbar along with six view panels:

• Data Source Explorer – contains the database object tree display. It is here that you can create connection profiles to your Teradata Database systems and interact with the database objects.

• Project Explorer – provides a hierarchical view of the Projects and their resource files.

It is here that you can select SQL files for editing or create new SQL files.

• SQL Editor – displayed when a SQL file is opened. Initially, the SQL Editor is opened to provide an area to enter SQL statements. The user must provide a connection profile in order to execute the SQL statements. Double clicking on a file in the Project Explorer will cause the file to be opened in the SQL Editor View.

• Teradata SQL History – contains the results when executing SQL statements and

running database routines, user-defined functions, and triggers. Each time SQL is executed, the command is saved in the History window. This allows you to run them again.

• Teradata View – displays detailed information about database object. Click the

Teradata View so that it is in focus. Then select an object from the Data Source Explorer and detailed information about that object is displayed in the Teradata View.

• Teradata Result Set Viewer – displays the result set data. Individual Result Set tabs are

created for each result set. The Result Set Viewer also contains a toolbar with actions that can occur on the result set data, such as saving, exporting, or printing the result set data.

You can rearrange the various panels and windows to fit your workflow preferences. Studio supports moving, hiding, docking, and detaching the different panel views. Panels can be moved by dragging the panel’s tab to any edge of the Studio window.

Page 13: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Teradata Studio Features Page 1-7

Teradata View

Studio Toolbar

SQL Editor

Data Source Explorer

Teradata SQL History

Teradata Result Set Viewer

Page 14: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Page 1-8 Teradata Studio Features

Teradata Studio Perspectives Studio uses the concept of perspectives as a part of its modular display. Each perspective is geared towards particular tasks. Teradata Studio provides four configured perspectives:

Perspective Description

Teradata Administrator Integrated work environment for managing data objects in Teradata Databases, Aster Databases, and Hadoop systems.

Data Transfer Integrated views, menus, and toolbars optimized for transferring data and monitoring the transfer jobs using progress and history views.

Query Development Integrated development environment for creating, editing, testing, and executing SQL/DDL query statements.

Database Development Integrated development environment for developing and managing databases such as Oracle, IBM DB2, and Microsoft SQl Server.

You can create additional custom perspectives by selecting Window > Customize Perspective from the top menu bar. To return to the default or initial view of Studio,select Window > Reset Perspective from the top menu bar. For this course, we will primarily use the Query Development perspective.

To customize which menus are viewable, select Customize Perspective.To return back to the default set up, select Reset Perspective.

Page 15: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Teradata Studio Features Page 1-9

Customizing Studio Perspectives Menu options can be customized based upon the needs of users. This can be helpful as unnecessary icons or menu options can clutter the users’ screens. To return to the default or initial view of Studio,select Window > Reset Perspective from the top menu bar.

The Menu Visibility options can be used to limit/customize the menu options visible in dialogs such as those seen when right clicking in Data Source Explorer.

The Tool Bar Visibility options allow users to uncheck or check items from the different tool bars in Studio to simplify each modular display

Page 16: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Page 1-10 Teradata Studio Features

Query Development Perspective The Query Development perspective focuses on SQL related tasks. The views displayed within the Query Development perspective include the Data Source Explorer, Project Explorer, SQL Editor, Result Set Viewer, and SQL History View. The topmost menus provide the user with tools to create and edit SQL statements.

Used to write SQL, import and export data, and other functions(This is the most commonly used perspective)

Page 17: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Teradata Studio Features Page 1-11

Data Source Explorer View Selecting folders or objects in the Data Source Explorer will enable the buttons in the toolbar or menu options. The options available will depend on the type of object and the tasks that are associated with that object.

The Data Source Explorer View panel contains the database object tree display.

By default, this is a hierarchical display but the view can be customized to suit users’ needs

From the Data Source Explorer, there are many shortcuts to common SQL tasks

Page 18: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Page 1-12 Teradata Studio Features

Customizing Preferences in Data Source Explorer The Data Source Explorer can be customized to make it more usable for different users. To make changes to the information displayed in the Data Source Explorer, Open the Window menu and select Preferences. When the Preferences options load, select Teradata Data Tools Preferences. Customizing Teradata Data Tools Preferences The options under the Teradata Data Tools Preferences choses where information displayed in the Data Source Explorer is gathered. Users can either use filtered or unfiltered data dictionary views to populate information displayed in Data Source Explorer. The default option uses views that end in the suffix V. Optionally, a filtered view (suffixed VX) may be used or a custom view defined for use. These views and their purposes will be discussed in more detail in Module 2. Customizing Data Source Explorer Load Preferences The options under the Data Source Explorer Load Preferences choses the information displayed and how it is presented in the Data Source Explorer. Load Options for Data Source Explorer Menu:

• Users may choose what types of objects are displayed (Note: Right click on the Databases Folder in DSE to add objects manually)

• By default, all users and databases are displayed • To reduce the information displayed, users may choose the User Choice option. This

option requires that the Hierarchical display be disables (discussed in the next section). What to Display Menu:

• Users may enable or disable a hierarchical display of objects in Teradata ◦ Hierarchical display shows objects nested based upon the underlying relationships

between databases and users ◦ Unchecking this option lists databases and users alphabetically

• Users may also choose what types of objects are included in the Data Source Explorer when drilling down to objects. The options include:

◦ Database Indexes ◦ Macros ◦ Stored Procedures ◦ Tables ◦ User-Defined Functions ◦ User-Defined Types ◦ Views Folder

• Additionally, menu at the bottom allows the user to choose which details will be loaded by default into Data Source Explorer. Options here may be unchecked to improve the startup time for Teradata Studio.

Page 19: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Teradata Studio Features Page 1-13

WINDOW>PREFERENCES>Teradata Datatools Preferences

• Show Databases and Users in Hierarchical Display

– Checked – shows databases and users based on ownership

– Unchecked – shows databases and users alphabetically

– Uncheck to enable User Choice Option

• Add Database – User Choice– Add one object at a time to Data

Source Explorer• What to Display

– Can add or remove objects of interest based upon user needs

Page 20: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Page 1-14 Teradata Studio Features

Data Source Explorer – Display Options In the following examples, Data Source Explorer is shown two ways. The first is the Hierarchical View and the second is Database View. The Database view is listed alphabetically rather than in parent/child order. The Data Source Explorer can be customized to make it more usable for different users. To make changes to the information displayed in the Data Source Explorer, Open the Window menu and select Preferences. When the Preferences options load, select Teradata Data Tools Preferences.

Hierarchical View

Database View (alphabetical)

Page 21: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Teradata Studio Features Page 1-15

Data Source Explorer –User Choice Option When a user wishes to customize their Data Source Explorer to only display a select list of objects, each database or user must be added back to Data Source Explorer manually. To add databases to a User Choice filtered Data Source Explorer, right click on the Databases folder below your user name. Right click and select the Teradata menu. Select the Load Database option. This will open a dialog that allows the addition of user preferred databases to the Data Source Explorer window. After each database name is typed, click the Add button. When finished, click the Close button. This only needs to be done once and the user selected options will appear every time Studio loads.

To Add databases:• Right click Databases

folder under your user name.

• Select Teradata menu then select Load Database.

• Type the name of each database to be displayed then click Add.

• When finished click Close.

To Remove databases:• Right click the database

name.• Select Teradata menu

then select Remove Database.

1

2

Page 22: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Page 1-16 Teradata Studio Features

Creating a SQL Statement (SQL Editor) When Teradata Studio is launched, a SQL Editor area is provided for you to enter your SQL statements. Once you have a connection to your Teradata database, you can begin to enter SQL statements in the SQL Editor window. Click the Save button to save the SQL Editor to a file. Another option to create a SQL file is through the Data Source Explorer. Within the Data Source Explorer, select your database connection and click the SQL Scrapbook action from the Data Source Explorer toolbar. This will create a SQL file in the SQL Editor with the name “SQL Scrapbook [n]” and the connection profile set to your database connection. You are now ready to enter your SQL statement. Refer to the section below entitled “SQL Editor” for information on executing your SQL statement. Click the Save icon to save your SQL file within your SQL Project. Queries are created by simply typing in the query text into the SQL Editor. It is not necessary to add a semi-colon at the end of a command. However, a semicolon between the statements is required for two or more statements.

Page 23: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Teradata Studio Features Page 1-17

Click SQL Editor icon to open the editor to start

Page 24: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Page 1-18 Teradata Studio Features

Opening SQL Editor The SQL Editor view is displayed when a SQL file is opened. Double clicking on a file in the Project Explorer will cause the file to be opened in the SQL Editor. The SQL Editor provides an area for you to enter your SQL statements. As an option, you can enable the auto activation of code assist by clicking the toolbar button. If this option is enabled, as you type, SQL code assist will pop up annotations (“spellcheck” for SQL) showing a list of expected keywords. If a database name is entered followed by a dot, the content assist will display the list of tables from your database for you to select from. (Note: By default, auto activation of code assist is disabled. Also, you must select a Teradata Database connection profile type in order for SQL code assist to work.) Code assist will also provide a list of SQL Templates. At the start of the SQL statement, hit the Ctrl+SPACE keys and a pop up list of templates is displayed. Scroll down until you find the template you want and double click on it to insert the template into your SQL Editor file. Anchor statements are provided that you can navigate through by tabbing. You can also edit or add your own SQL Templates via the SQL Editor Preferences page. Parameterized queries can also be created in the SQL Editor. The query contains parameters indicated by question marks. When the query is executed, the user is prompted for the parameter value(s). Named parameters allow the user to specify a name for the parameter. The SQL Editor also provides syntax coloring for your SQL statements. You can change the colors of comments, keywords, identifiers, types, or other syntax items via the SQL Editor Preferences page. When right clicking in the SQL Editor window, a SQL Editor Context menu appears with options to execute the SQL statement(s) in your SQL file. The following options are presented:

• Execute all of the SQL statements • Execute the selected SQL statement • Execute the SQL statements as individual statements (with a commit occurring between

statements) • Execute the selected statement as one statement • Execute the current statement where the cursor is located • Visual Query Builder

Teradata Studio provides a graphical interface for creating SQL statements, referred to as the SQL Query Builder. You can edit an existing statement using the SQL Query Builder by selecting the statement within the SQL Editor and choosing the “Edit in SQL Query Builder…” menu option. There are several ways to open a SQL editor and it is possible to have several open at once.

Page 25: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Teradata Studio Features Page 1-19

From Data Source Explorer

From the Toolbar

From the File menu

Page 26: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Page 1-20 Teradata Studio Features

Working in SQL Editor SQL Editor can be used to create and execute SQL statements. This display will also be displayed when a SQL file is opened in Studio. “Execute” actually executes the statements in the query one statement after the other and optionally stops if one of the statements returns an error. “Execute Parallel” executes all statements at the same time – and is only valid if all the statements are Teradata SQL/DML statements. This submits the entire query as a single request, allowing the database to execute all the statements in parallel. Multiple answer sets are returned in the Result Set Viewer window.

Connection Profile

Enter SQL here

Execute SQL

Clear QueryNew SQLEditor

Execute

Execute Parallel

Page 27: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Teradata Studio Features Page 1-21

SQL Query Builder Teradata SQL Query Builder provides a graphical interface to create, edit, or run SQL statements. It provides access to your database objects so that you can quickly create or edit SQL statements without actually typing any SQL code. You also have the flexibility to add or modify the SQL code in the editor window. You can open the Teradata SQL Query Builder in the SQL Editor by right-clicking an SQL file in the Project Explorer and choosing the Open With option, then selecting Teradata SQL Query Builder from the list of Editor choices. Note: Some syntactic variations might exist between Teradata SQL syntax and the SQL syntax that the Query Builder supports; consequently, some SQL Query Builder features might not work with Teradata SQL syntax. For example, Teradata allows for abbreviated keywords such as "sel" for "select", or "ins" for "insert", which the Query Builder syntax does not support. Note: Also, if multiple SQL statements exist in the file, SQL Query Builder uses the first statement and ignores the rest. It also does not handle nested SELECT statements. The Teradata SQL Query Builder supports four types of SQL statements:

SQL Query Builder provides the user to use menus to create SQL statements.

Right mouse click in the SQL Editor to open SQL Query Builder.

Page 28: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Page 1-22 Teradata Studio Features

Using Query Builder For the next frame, see the components of the Query Builder menu.

Right Click to Add Tables/Views.Select Template

Page 29: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Teradata Studio Features Page 1-23

Adding Tables Using Query Builder For the next frame, see how to add tables or view using the Query Builder menu. Also, see how columns are added to the query.

1

2

3

To Add Tables/Views:• Right click in the middle

pane to open a menu to select a database

• Select the database for the table/view

• Select the table/view to be added to the query

To Select Columns, click the check box for each column to be projected in the query output.

Page 30: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Page 1-24 Teradata Studio Features

Creating Joins in Query Builder Query Builder can be used to create queries from one or more tables. When using more than one table, the joins must be defined. The following frame walks through joining tables/views in Query Builder. Note: Query Builder can only build single column join conditions between two tables/views. If additional columns are needed to qualify the relationship, those will need to be added manually by editing the query manually.

1

2

3

Identify the join relationship between the tables by selecting the appropriate column from the source and target then indicating the join type.

Page 31: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Teradata Studio Features Page 1-25

Applying Query Filters and Other Options in Query Builder The SQL Query Builder provides tabs to edit the Columns, Conditions, Groups, and Group Conditions. Right-clicking the graphical area background provides options to add tables or create joins. Right-clicking a table provides options to add a table, create a join, replace a table, select all columns, deselect all columns, update an alias, and remove a table. You can also drag table objects from the Data Source Explorer and drop them in the graphical display of the SQL Query Builder to easily add tables to your SQL statement.

Page 32: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Page 1-26 Teradata Studio Features

SQL Editor Preferences – Editor Font The Teradata Studio product can be customized in many ways to suit different user needs. Several key areas are discussed in the next several pages. To change the fonts used in Studio, select the Window menu Preferences. Expand the General and then the Appearance menu and select Colors and Fonts. The Text Font option allows you to change the font and/or increase the font size. Select Text Font and click the Edit button to select a new font.

You can easily change the font size or type in the SQL EditorUse Windows > Preferences then expand the General and Appearance options

First, select Item to change

Second, Select Edit

Page 33: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Teradata Studio Features Page 1-27

SQL Editor Preferences – Text Editor Options To change preferences such as the number of ‘undo’ records in history or toggle line numbers on or off in SQL Editor, select the Window menu Preferences. Expand the General and then the Editors menu and select Text Editors. .

You can easily change the font size or type in the SQL EditorUse Windows > Preferences then expand the General, Appearance, and Text Editor options

Use check boxes toselect options to

enableor disable.

Text Editors

Page 34: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Page 1-28 Teradata Studio Features

SQL Editor Preferences – Customizing SQL Assistance The SQL Editor preferences customize the behaviors and look-and-feel of the SQL Editor. To set preferences for general editor appearance and behaviors, go to Window > Preferences > Data Management > SQL Development > SQL Editor.

You can easily change the font size or type in the SQL EditorUse Windows > Preferences then expand the Data Management menu and the SQL Editor options

Page 35: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Teradata Studio Features Page 1-29

Commenting Lines of SQL The next frame shows two different techniques you can use to comment lines of SQL. Commenting lines means they will not be included for execution by the database. You may also comment ending portions of a SQL request like this.

HELP DATABASE DBC; -- this is a comment

Note how the SQL Editor color codes the lines of code.

ANSI Standard method for commenting lines one at a time.

Teradata method for commenting blocks of lines.

Only the highlighted SQL gets submitted.(The “HELP DATABASE”)

Page 36: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Page 1-30 Teradata Studio Features

Limiting the Number of Rows Returned (SAMPLE) The next frame illustrates how to retrieve a subset of rows upon either a number of the table’s rows or a percentage of the table’s rows. By default:

• The sample is “AMP Proportionally” – that is, the database seeks to have each AMP contribute a proportional share of the sample.

• The sample is performed “without replacement” – that is, no column values from any specific row will appear more than once within the sample.

The fact that the employee numbers are unique reinforces the notion of “no replacement.” No employee number should appear more than once within the sample.

The SAMPLE function can be used to return only a subset of rows from a query

• There is “no replacement” of rows.• The sample is performed AMP proportionally.

Retrieve a sampled number of rows:

SELECT Employee_NumberFROM EmployeeSAMPLE 10ORDER BY 1;

Retrieve a sampled percentage of rows:

SELECT Employee_NumberFROM EmployeeSAMPLE .25ORDER BY 1;

employee_number---------------

1001100210031004100610111014101610191024

employee_number---------------

1003100410061007101110161019

0.25 * 26 = 6.5Fractional results greater than .4999 generate an added row.

Page 37: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Teradata Studio Features Page 1-31

Result Set Viewer Overview The Result Set Viewer displays the result set or parameter data from executing SQL statements and running database routines, such as stored procedures, macros, user-defined functions and triggers. The Result Set Viewer provides an enhanced display of the Result Set data. It displays the result sets as tabs within the Teradata Result Set Viewer, allowing the user to select rows or cells from the Result Set data table grid. The Result Set Viewer provides menu options to hide or show all columns, move and sort columns, find a value within the result set data, perform aggregate functions on selected numeric columns, filter column data, format cells, and copy cells. Select a row or cell and right click to display the menu options. Multiple result sets can be displayed as tabs or in a side-by-side sash display. LOBs (large objects) can also be displayed in the Result Set Viewer. When a query result is being processed that contains an LOB result column, a dialog appears for the user to specify where and how to store the LOB values on the file system. A toolbar is also provided with options to rename the Result Set tab, show the SQL associated with the result set, switch the display between text and grid display, switch the display between tabs and side-by-side sash display, and save, export, and print result set data. SQL result data can also be saved to a file within your Project or exported to an external file in the file system.

Allows the user to select rows or cells from the Result Set data table grid

Right click for menu options

Toolbar options are available as dropdown

Can be exported as:• Excel• XML• Text files

Page 38: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Page 1-32 Teradata Studio Features

Result Set Customization The Result Set Viewer can be customized to suit different user needs. To change how the Results Set Viewer is displayed or the options available, select the Window menu followed by the Preferences menu, then choose the Teradata Datatools Preferences. From the Teradata Datatools Preferences, select the Result Set Viewer menu.

Option Description

Alternating Row Colors Option

Display alternate Result Set rows in color — Displays the result set with alternating row background colors. This option is disabled by default.

Display alternate Result Set rows in color

Select the check box to display color in alternate rows of the result set. Click the color box to open the Color dialog and select a basic color or define a custom color to use.

Grid Display Options

Show Row Numbers — Displays row numbers. This option is enabled by default.

Show Column Headers — Displays column headers. This option is enabled by default.

Show Table Grid Lines — Displays lines between rows and columns on the table grid. This option is enabled by default.

(*) Display Column Titles rather than Column Names — Displays the column titles (if available) rather than the column names, in the column header area. This option is disabled by default. The query must be rerun in order for a change to this option to take effect.

(*) Max Display Row Count — Changes the maximum number of rows to display. The default for this option is 2000. The query must be rerun in order for a change to this option to take effect.

Display Null Value as — Changes the value that is displayed when the data is null. The default for this options is "null".

Format Options Long Text Boundary Size — Changes the long text boundary size. If a data value length is longer than the long text boundary size, only the first 20 characters are displayed. The user can view the entire data value by double-clicking the data in the display grid. A pop-up window is presented with the full data value. The default for this option is 150.

Display 1000 Separator in Numeric Columns — Displays a 1000 separator in numeric columns. This option is disabled by default.

Display Negative Numbers in Red — Displays negative numbers in red. This option is enabled by default.

Page 39: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Teradata Studio Features Page 1-33

Copy Column Delimiter

Delimiter Format — Changes the column delimiter when the Copy data option is used on the grid display. The default for this option is Comma Separated. Other possible choices are Tab Separated and User Defined.

Delimiter Value — Changes the column delimiter value when the User Defined format option is selected.

Copy Include Column Headers — Select to include column headers when the copy data option is used on the grid display.

Copy Include Grid Lines — Select to include cell borders when you use the Copy data option to copy cells from the grid display.

Viewer Window Options

Select the Reuse Result Set Viewer Window check box to cause the Result Set Viewer to refresh the current window with new result sets generated by the SQL Editor. If you deselect the box, the Result Set Viewer creates a new window for each generated result set.

Action Buttons Restore Defaults — Restores the default values for the options.

Apply — Applies any option changes. This might cause a refresh of the current result sets displayed in the Result Set Viewer.

OK — Applies option changes and closes the Preferences page. This might cause a refresh of the current result sets displayed in the Result Set Viewer.

Window > Preferences> Teradata Datatools Preferences >Result Set Viewer Preferences

Page 40: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Page 1-34 Teradata Studio Features

Result Set Customization (continued) If there are a large number of rows, using the SQL Editor menu export option (pictured in the next frame) is a helpful option.

right-clickcontext-menu

Short cut to Result Set Preferences

View Result Set SQL

Toggle Results as Textor Grid

Pin Result SetRight click in Result Set for formatting options

Page 41: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Teradata Studio Features Page 1-35

Exporting Result Sets One option that can easily export data from a Results set is to simply copy and paste the data from a Results Set Viewer window into another tool or program. This involves highlighting the rows and columns desired, copying them (Control-C), and pasting them into the desired program (Control-V). If there are a large number of rows, using the SQL Editor menu export option (the icon that looks like an outbox) is a helpful option.

Easy Data Export Options:• Simply highlight the columns and rows desired, copy and paste them to

another tool• Save or export result sets to Excel, XML, or text files

right-clickcontext-menu

Page 42: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Page 1-36 Teradata Studio Features

Exporting Result Sets (continued) When exporting larger results sets, using the Export Result Set menu is more effective. This tool allows you to choose what the exported file will be named, where it will be located, and what kind of file will be created.

• To export, use Browse menu to select destination for output file• Type File Name• Select a Format (.csv or generic .xls will be the most common)• Choose an Output Encoding and Output Format option(usually use

default)• Select a Delimiter option

right-clickcontext-menu

Page 43: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Teradata Studio Features Page 1-37

SQL History View The SQL History View provides an enhanced display of the SQL History information. As SQL statements are executed from the SQL Editor or functions are run from the Data Source Explorer, an entry is placed in the SQL History. The SQL History view displays the data in a grid format, allowing the user to select rows and cells. The SQL History provides extensive information about the SQL execution, such as execution elapsed time, DBMS time, fetch time, database server, row count, parameter display for macros and stored procedures, SQL statement, and SQL statement type. It provides options to copy cells or rows, edit the “Note” or SQL for a SQL History entry, search for result history data, and re-execute SQL statements. Choosing the “Edit SQL” option will open a SQL Scrapbook file in the SQL Editor with the selected SQL statement. This allows users to further fine tune their SQL statements. The SQL History view also provides a toolbar menu with options to delete rows or all of the history entries, filter columns, format cells, and sort columns. The SQL History uses an embedded Derby Database to manage the SQL History entries.

The SQL History provides extensive information about the SQL execution:• Execution elapsed time• DBMS time• Fetch time• Database server• Row count• Parameter displays• SQL statement• SQL statement type

To open previous SQL statements in the SQL Editor

Tool Options

Page 44: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Page 1-38 Teradata Studio Features

SQL History Output The SQL History View details of previously executed statements. The SQL History View includes information such as:

Column Options Description

Timestamp Date and time at which the query ended

Source Data source name

User ID User ID logged on

Destination Location of result set (Blank if error returned)

Result Result message which is blank unless an error occurs. To see full message, hover your mouse over a cell in the Result column.

Row Count Teradata Database — number of rows matching the query submitted. If you abort a query in progress or decline to continue after the output limit is reached, Rows shows only how many rows matched the query you submitted.

Other databases — number of rows returned or changed. If you abort, Rows shows only the number of rows returned.

Note User note attached to query

SQL Statement Query text

SQL Type Type of database accessed

Length Length of the query in characters

Statements Number of statements in the query

Statement Type List of statement types used in the query, such as, CREATE TABLE, INSERT, SELECT.

Elapsed Time Total elapsed time for the query

DBMS Time Time spent within the database

Fetch Time Time taken to return the data

Parameters Number of parameters used in the query

Sequence Order in which the query was submitted to the database

Page 45: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Teradata Studio Features Page 1-39

Error messages and information about previously executed queries may be located in the SQL History View

Error MessagesLogon used

SQL Executed

Page 46: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Page 1-40 Teradata Studio Features

Options to Save Files It is possible to save files to a Studio project or to a file system location. To save files to the location of your choosing, use the Save to File System option and then select the drive and folder location.

Files can be save to a Studio project file or locally to you file system location

Page 47: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Teradata Studio Features Page 1-41

Module 1: Summary

• Teradata Studio is a modular, perspective based tool for writing SQL, managing objects, and importing/exporting data from Teradata.

• Connection profiles can be customized based upon user logon needs.

• The Data Source Explorer can be customized and leveraged to assist in SQL development.

• The SQL Editor window is the primary tool for writing, editing, and submitting SQL.

• Data can be exported from Teradata locally to desktop using Teradata Studio functions.

Page 48: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Page 1-42 Teradata Studio Features

Module 1: Review Questions Check your understanding of the concepts discussed in this module by completing the review questions as directed by your instructor.

This is not a best practice for security reasons.

Reset Perspective

1. True or False: Teradata Studio is only a tool for database administrators.

2. True or False: The Data Source Explorer preferences can be customized to show only database objects specified by the user.

3. True or False: You should save your password in your connection profile.

4. The ___________________ menu option under the Window menu can be used to refresh a Teradata Studio perspective to a default view.

5. Which of the following are not modular displays in Teradata Studio?a. Results Viewer b. Teradata SQL Historyc. Data Source Explorerd. Data Source Editor

Page 49: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Teradata Studio Features Page 1-43

Module 1: Lab Exercises Check your understanding of the concepts discussed in this module by completing the lab exercise as directed by your instructor.

1. Use the existing default database connection as defined by the instructor. Update or enter the following information:a. Username: Username provided by instructorb. Password: Password provided by instructor

2. Go to the Preferences menu and change the following characteristics:a. Under Teradata Datatools Preferences (Data Source Explorer):

i. Uncheck the option to Show Databases and Users in HierchicalDisplay

ii. Select Load Options item User Choice

b. Under General:i. Change the Text Font Size to 16. (Appearance >Colors and

Fonts)ii. Edit the Undo History Size to 100 characters (Editors>Text

Editors)

Page 50: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Page 1-44 Teradata Studio Features

Module 1: Lab Exercises (continued) Check your understanding of the concepts discussed in this module by completing the lab exercise as directed by your instructor.

3. In the Data Source Explorer, add the following databases:a. Employee_Salesb. TM_Workshop

4. Browse the TM_Workshop database and locate the table ABC_Account.

5. Right click and from the Teradata menu select Row Count. How many rows are in the ABC_Account table? ______

6. Execute the following select statement:SELECTAccount_Nbr,Bank_Nbr,Account_Type_CodeFROM TM_WORKSHOP.ABC_ACCOUNTSAMPLE 100;

7. Export the rows returned to Excel (use the .xls format).

Page 51: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Locating and Using Object Information Page 2-1

Module 2

Locating and Using Object Information

After completing this module, you will be able to:

• Leverage the Data Source Explorer to locate tables or view as well as for quick SQL generation

• Use ‘HELP’ and ‘SHOW’ commands to get object information

• Understand helpful built-in functions such as USER, SESSION, and CURRENT_DATE

• Locate object and gather information about them using the Data Dictionary Filtered Views

Page 52: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Page 2-2 Locating and Using Object Information

Table of Contents Viewing Database Objects in Data Source Explorer ................................................................... 2-3 Viewing Database Objects in Data Source Explorer (continued) ............................................... 2-4 Teradata View .............................................................................................................................. 2-5 Searching for Objects in Data Source Explorer ........................................................................... 2-6 SQL Shortcuts in Data Source Explorer ...................................................................................... 2-7 Shortcuts to Typing Object Names .............................................................................................. 2-8 Help and Show Commands ......................................................................................................... 2-9 HELP View Command .............................................................................................................. 2-10 Locating View Information ....................................................................................................... 2-11 Viewing the SQL Source for a View ......................................................................................... 2-12 HELP Macro Command ............................................................................................................ 2-13 The Teradata “SHOW View” Command .................................................................................. 2-14 Other SQL SHOW Commands .................................................................................................. 2-15 Useful Built-In Functions .......................................................................................................... 2-16 Finding Tables, Views, Macros, and Stored Procedures ........................................................... 2-17 Finding and Understanding Columns ........................................................................................ 2-18 Module 2: Summary .................................................................................................................. 2-20 Module 2: Review Questions ..................................................................................................... 2-21 Module 2: Lab Exercise ............................................................................................................. 2-22

Page 53: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Locating and Using Object Information Page 2-3

Viewing Database Objects in Data Source Explorer The Data Source Explorer feature of Teradata Studio displays an alphabetical or hierarchical listing of databases and objects of the connected user. It further permits drilldown on individual objects to views, column names, indexes, and parameters as they apply. This is simply done by clicking on a database name to expand the tree display for that database. It can help reduce the time required to build a query and help reduce errors in object names. Initially, the following Teradata databases are loaded into the Data Source Explorer:

• The User ID that was used to connect to the database • The user’s default database • The database "DBC"

The Data Source Explorer allows you to drill down to show:

• Columns and indexes of tables • Columns of views • Parameters of macros • Parameters of stored procedures

Collapsed

Expanded

Drilled Down

Drilled Down Further

Page 54: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Page 2-4 Locating and Using Object Information

Viewing Database Objects in Data Source Explorer (continued) For each table or view, users can drill down to details about the columns, indexes, and other characteristics using the folders for each object type. Additionally, if a table or view name is right clicked this opens the Teradata menu and the user may leverage SQL shortcuts such as Generate SQL or Row Count.

Expand folders to get information about columns and indexes

Right Click to get more information or SQL shortcuts

Page 55: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Locating and Using Object Information Page 2-5

Teradata View Additionally, the Teradata View contains more detailed information about the database objects. When an object is selected in the Data Source Explorer, it is automatically displayed in the Teradata View. You can click on the tabs to display additional information on the database object. The Teradata View toolbar and menu also contains the dialog buttons to create or administer the database objects.

• Databases • Users • Tables • Views • Macros • Stored Procedures • UDFs • User Defined Types • Space and Skew

Right click a Table or View and Select Show in Object Viewer

Detailed information about database objects are displayed automatically

Page 56: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Page 2-6 Locating and Using Object Information

Searching for Objects in Data Source Explorer The Data Source Explorer can search for different objects. This will locate different object types based upon which kind of object (database, view, macros, etc.) is being identified. It specifies a value to search for. You can use the database 'LIKE' syntax to provide a more expanded search. The 'LIKE' syntax includes a percent sign ('%') and low line character ('_'). The percent sign character represents any string of zero or more arbitrary characters. The low line character represents exactly one arbitrary character. For example, the search value of 'emp%' finds the first object whose name starts with 'emp'. Note: You must use the backward slash character '\' as the escape character when entering the special search characters, '\', '_', and '%', in your search value. For example, 'emp\%' searches for the first object whose name equals 'emp%'.

Page 57: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Locating and Using Object Information Page 2-7

SQL Shortcuts in Data Source Explorer The Data Source Explorer can be used to assist with SQL development. For any object displayed, the Teradata menu and the Data menu may be used as shortcuts to write and view select details about a given database object. From the Teradata menu, the options include:

• Generate SQL (DELETE, INSERT, SELECT, and UPDATE statements) • Row Count • Show DDL

From the DATA menu, the options include:

• Sample Contents • Export Data • Load Data

Select Option

In order to insert objects (tablenames, columnnames, etc) into the SQL Editor, simply drag and drop

Page 58: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Page 2-8 Locating and Using Object Information

Shortcuts to Typing Object Names For the next frame, you can drag and drop tables from the Data Source Explorer to the SQL Editor (Scrapbook) to perform the action shown. This shortcut method can significantly aid you in the typing of queries whether lengthy or short.

Click and Drag from here to there.

You can drag objects from the Data Source Explorer to the Scrapbook to have the full name added quickly.

Page 59: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Locating and Using Object Information Page 2-9

Help and Show Commands Teradata’s data dictionary has several ‘shortcuts’ that can be used to get information about tables or views. These tools can provide information about the columns, attributes, and data definition language (DDL) used in the creation of an object. This can be helpful for troubleshooting incorrect column names or data type issues.

HELP can be used to review information about a table or view

Uses a 3 part syntax:

HELP [object type] [object name];

Example:HELP VIEW Emp_Views.Job;

SHOW can be used to view the DDL for a table or view

Both HELP and SHOW may be used to troubleshoot SQL issues such as incorrect column names or data type issues

Also uses a 3 part syntax:

SHOW [object type] [object name];

Example:SHOW VIEW Employee_Sales.Job;

Column Name Type …-------------------------------------- ---- …job_code ? … description ? … hourly_billing_rate ? … hourly_cost_rate ? …

Request Text --------------------------CREATE VIEW Emp_Views.job

(job_code,description,hourly_billing_rate

……

Page 60: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Page 2-10 Locating and Using Object Information

HELP View Command One way to retrieve information about a view and the columns in it is to use the SHOW VIEW syntax,

The “HELP VIEW” command returns information about the columns included in a view. Qualifications for SQL are databasename.viewname

Page 61: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Locating and Using Object Information Page 2-11

Locating View Information The next frame identifies another way to get information about the columns in a view or table.

Select the view

Right Click and Choose Show in Object Viewer

Select the Columns Tab

Page 62: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Page 2-12 Locating and Using Object Information

Viewing the SQL Source for a View The next frame identifies how to locate DDL (Data Definition Language) for a View using Studio. This can be helpful as it allows the user to identify how the view was created. To locate this information, follow the steps in the previous page to locate the column list for the view. Instead of selecting the Columns tab, select SQL. Below are some of the details than can be understood from this view:

• Tables or Views that source the View • Joins (if any) • Column aliasing (if any) • Column formatting (if any)

Note: Perspective is Teradata Administrator

The SQL tab shows the definition fora view. It can be used to identify which objects were used to sourcethe view

Page 63: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Locating and Using Object Information Page 2-13

HELP Macro Command It is also possible to leverage the HELP command with macros.

The “HELP MACRO” command returns information about the input variables required for a given Macro

Page 64: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Page 2-14 Locating and Using Object Information

The Teradata “SHOW View” Command The next frame contrasts the result of the SHOW command with similar information provided by the Teradata Studio Data Source Explorer. Note how the result of the SHOW command appears in the result window whereas the result of the “right-click” “show definition” method would display the result into the Query Window (not shown). Except for the location of the result, they display the same information.

Once the command is executed, the result will appear in the Result Set panel.

The “SHOW” command may be used to provide the definition of an object.In this example, we are getting the definition for a view in database Emp_Views.

Page 65: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Locating and Using Object Information Page 2-15

Other SQL SHOW Commands Other SHOW commands are shown on the next frame. Although the HELP commands are more plentiful than are those for the SHOW command, the SHOW command displays the syntax used for creating the object, and can provide much more information about the object. The definition shown includes any syntax acquired during the initial create by default and not by explicitly typing the syntax itself. In Views and Macros, this can be especially helpful as this will help identify details such as joins, formatting, and data types that might be part of the view or macro definition.

Database Objects:SHOW TABLE ABC_Account;SHOW VIEW ABC_Account_V;SHOW MACRO payroll_3;

Some other SQL SHOW commands.For a complete list of objects referenced by the SHOW command you can issue the following:

HELP 'SQL SHOW';

Page 66: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Page 2-16 Locating and Using Object Information

Useful Built-In Functions Teradata supports a wide array of built in functions that can display information in query output or be used to qualify queries in WHERE clauses.

Built in functions can be used to return date and time informationSELECT DATE, CURRENT_DATE, CURRENT_TIME, CURRENT_TIMESTAMP, PROFILE

Some information about the request and its result.• The DATE and TIME keywords are Teradata extensions to the ANSI

standard and are shown here for clarification.• Note that the values are considered numeric since they are all right

justified.• Those having “CURRENT_” in the name are ANSI standard.

Date Date Current Time(0) Current TimeStamp(6) Profile-------- -------- --------------- -------------------------------- ------------14/07/23 14/07/23 06:51:25-05:00 2014-07-23 06:51:25.600000-05:00 STUDENT_PROF

Page 67: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Locating and Using Object Information Page 2-17

Finding Tables, Views, Macros, and Stored Procedures The Tables view accesses the data dictionary table, DBC.TVM, which contains descriptions of tables, views, macros, journals, join indexes, triggers, and stored procedures. The view features a TableKind column that allows you to specify the kind of object to reference. A list of the TableKind options as well as other key columns from this view are included below:

Column Definition

Version A number incremented each time a user alters a table.

RequestText Returns the text of the most recent DDL statement that was used to CREATE or MODIFY the table.

TableKind T = Table

M = Macro

V = View

G = Trigger

P = Stored Procedure

F = User-defined Function

I = Join index

N = Hash Index

J = Journal table

O = No Primary Index

Use the Data Dictionary view DBC.TablesVX to search for objects

Page 68: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Page 2-18 Locating and Using Object Information

Finding and Understanding Columns The Columns view returns information from the DBC.TVFields table. This data dictionary table includes information about:

• Table and view columns • Macro and stored procedure parameters

To avoid unexpected data conversion, you may use this view to compare data types for columns with the same name. The following SELECT statement provides an example:

SELECT DatabaseName, TableName FROM DBC.TVFields WHERE ColumnTitle LIKE 'amount' AND ColumnType <> 'D' OR DecimalTotalDigits <> 7 OR DecimalFractionalDigits <> 2 ORDER BY 1,2;

Some of the common column types for this view include:

I = INTEGER F = FLOAT BF = BYTE Fixed CV = VARCHAR

I1 = BYTEINT DA = DATE BV = BYTE Variable BO = BLOB

I2 = SMALLINT D = DECIMAL CF = CHARACTER Fixed CO = CLOB

I8 = BYTEINTEGER GF = GRAPHIC GV = VARGRAPHIC UT = UDT Type

TS = TIMESTAMP TZ = TIME w ZONE SZ = TIMESTAMP w ZONE

Page 69: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Locating and Using Object Information Page 2-19

Page 70: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Page 2-20 Locating and Using Object Information

Module 2: Summary

• Teradata’s data dictionary tables and views keep track of all created objects:– Tables, views, macros, triggers, stored procedures, and user-defined

functions– Columns and indexes

• Information provided by the data dictionary, HELP, and SHOW commands may be used to resolve query issues such as incorrect column names or mis-matched data types.

• Teradata Studio has many menu options to provide short cuts to information helpful to users including cut and paste options

Page 71: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Locating and Using Object Information Page 2-21

Module 2: Review Questions Check your understanding of the concepts discussed in this module by completing the review questions as directed by your instructor.

1. True or False: Users accessing DBC.TablesVX will find information on tables, views, macros, and stored procedures rather than only tables.

2. True or False: The data dictionary views can be used to locate information about data types and formats for columns in tables and views.

3. Which two of the following ‘HELP’ SQL below is valid?

a. HELP TM_WORKSHOP;

b. HELP TM_WORKSHOP.ABC_Account;

c. HELP TABLE TM_WORKSHOP.ABC_Account;

d. HELP COLUMN TM_WORKSHOP.ABC_Account.Product_ID;

e. HELP TM_WORKSHOP.ABC_Account.Product_ID;

Page 72: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Page 2-22 Locating and Using Object Information

Module 2: Lab Exercise Check your understanding of the concepts discussed in this module by completing the lab exercise as directed by your instructor.

Use Teradata Studio and the data dictionary views (DBC.TablesVX and DBC.ColumnsVX) to answer the following questions.

1. Using the Data Source Explorer, how many of the following are in the TM_Workshop database (Hint: You will need to open the folder for each object type):Views _______________ Tables _______________ Macros _______________

2. How many tables/views in the TM_Workshop database have an Account_Nbrcolumn? __________

3. What is the data type for the Product_ID column in the TM_Workshop.ABC_Account table? __________

4. Execute the following HELP and SHOW statements. (Note which ones are successful and which ones fail)

a) HELP VIEW TM_Workshop.ABC_Accountb) HELP TM_Workshop.Aggreement_V;c) HELP VIEW TM_Workshop.Aggreement_V; d) SHOW MACRO TM_Workshop.Aggreement_By_Account_Nbr; e) SHOW VIEW TM_Workshop.ABC_Account_V;

Page 73: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Understanding Space in Teradata Page 3-1

Module 3

Understanding Space in Teradata

After completing this module, you will be able to:

• Identify and describe permanent, temporary, and spool space

• Understand how spool space is used

• Describe common space issues and potential solutions

Page 74: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Page 3-2 Understanding Space in Teradata

Table of Contents Databases and Users Defined ...................................................................................................... 3-4 Teradata Database Space ............................................................................................................. 3-6 How Space is Managed in Teradata ............................................................................................ 3-8 How is Spool Space Used ............................................................................................................ 3-9 Spool Space Usage – Join Example ........................................................................................... 3-10 Spool Usage – “Derived” Table Example ................................................................................. 3-11 Spool Usage – Volatile Table Example ..................................................................................... 3-12 Why Do Users Have Spool Limits? ........................................................................................... 3-13 Module 3: Summary .................................................................................................................. 3-14 Module 3: Review Questions ..................................................................................................... 3-15

Page 75: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Understanding Space in Teradata Page 3-3

Notes:

Page 76: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Page 3-4 Understanding Space in Teradata

Databases and Users Defined A Teradata Database provides a logical grouping of information. It is a repository used to store tables, views, macros, triggers, and stored procedures. A database could be considered a passive repository, because it is used solely to store other database objects. Databases are created with the SQL statement CREATE DATABASE. A Teradata User is also a repository for storing database objects. A user is the same as a database except that a user can actually log on to the RDBMS. To logon, a user must have a password. Users can access other databases and other database objects depending on the privileges or access rights that they have been granted. Users are created with the SQL statement CREATE USER. Access rights are privileges on database objects, which ensures security. Both a database and a user in the Teradata Database can contain a collection of objects such as:

Tables A table is the logical structure of data in a relational database. It is a two-dimensional structure made up of columns and rows.

Views A view is a pre-defined subset of one of more tables or other views. It does not exist as a real table, but serves as a reference to existing tables or views. One way to think of a view is as a virtual table. Views have definitions in the data dictionary, but do not contain any physical rows. Views can be used to implement security, control access, and simplify or standardize access techniques to the underlying tables. Views provide a well-defined and tested high performance access to data.

Macros A macro is a predefined, stored set of one or more SQL commands and optionally, report formatting commands. Macros are used to simplify the execution of frequently used SQL commands.

Stored Procedures A stored procedure is a program that is stored within Teradata and executes within the Teradata Database. A stored procedure uses permanent disk space and may be to manage different interactions with data.

Page 77: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Understanding Space in Teradata Page 3-5

• A Database is created with the CREATE DATABASE command.

• A User is created with the CREATE USER command.

• The only difference between a Database and a User is that a User has a password and may logon to the system.

• Databases and Users are repositories for Database Objects:- Tables - Views- Macros- Stored Procedures

Page 78: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Page 3-6 Understanding Space in Teradata

Teradata Database Space There are three types of space within the Teradata Database:

1. Perm Space All databases and users have a defined upper limit of Permanent Space. Permanent Space is used for storing the data rows of tables. Perm Space is not pre-allocated. It represents a maximum limit.

2. Spool Space

All databases also have an upper limit of Spool Space. If there is no limit defined for a particular database or user, limits are inherited from parents. Theoretically, a user could use all of the unallocated space in the system for their query. Spool Space is unused Perm Space that is used to hold intermediate query results or formatted answer sets for queries. Once the query is complete, the Spool Space is released.

Example: You have a database with total disk space of 100GB. You have 10GB of user data and an additional 10GB of overhead. What is the maximum amount of Spool Space available for queries?

Answer: 80GB. All of the remaining space in the system is available for spool.

3. Temp Space

The third type of space is Temporary Space. Temp Space is used for global temporary tables, and these results remain available to the user until their session is terminated. Tables created in Temp Space will survive a restart. Temp Space is permanent space currently not used.

Page 79: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Understanding Space in Teradata Page 3-7

Database objects require space in a Database or User as follows:• Tables - require Perm Space• Views - do not require Perm Space• Macros - do not require Perm Space• Stored Procedures - require Perm Space

Space limits are specified for each database and for each user:• Perm Space - maximum amount of space available for permanent

tables and index subtables• Spool Space - maximum amount of work space available for request

processing- holds intermediate query result sets.

• Temp Space - maximum amount of space available for global temporary tables

- available to the user until their session is terminated

A database or user with no perm space may not contain permanent tables but may contain views and macros.

Page 80: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Page 3-8 Understanding Space in Teradata

How Space is Managed in Teradata Database Administrators define perm, spool, and temp space limits at the database or user level, not at the table level.. Perm Space All permanent space in a system is owned and assigned to a particular user or database. This means that in order to increase the space of one database, space must be taken from another database. The permanent space is also managed logically. When databases or users are assigned storage, it is not defined as locations in storage. The permanent space assigned is subject to the maximum assigned but otherwise data placement is handled by the database file system. Perm space allocated to a user or database that is currently unused is available for use as spool or temporary space. Spool and Temp Space Spool and Temp space are also assigned at the user or database level. Unlike Perm space, the space used by Spool and Temp is fluid and based upon the work that is current in progress on the system in activities such as join processing, derived tables, or volatile tables. If a user exceeds their assigned spool or temp space allocation, their task or query will fail.

Teradata System (10 TB)

Database (0-200 GB) Users (no workspace)

Users (with workspace)

Page 81: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Understanding Space in Teradata Page 3-9

How is Spool Space Used Spool Space is work space that is assigned to a user or database for interim processing. This space is distributed across the amps similarly to Perm space. Interim processing such as joins, aggregations, and other steps are managed in spool without relocating the Perm data tables on which the data originates.

CREATE USER TM_Workshop_User FROM TM_WORKSHOP AS PERMANENT = 1E9 BYTES, SPOOL = 50E9 BYTES, PASSWORD = secret … ;

• Spool space is work space acquired automatically by the system for intermediate query results or answer sets.

• The Spool limit is divided by the number of AMPS in the system, giving a per-AMP limit that cannot be exceeded.

– "Insufficient Spool" errors often result from poorly distributed data or joins on columns with large numbers of non-unique values.

– Keeping Spool rows small and few in number reduces Spool I/O

• Spool space limits are enforced across all active sessions (connections) for a single user.

Spool Space Limit per AMP

5 GB

AMP

5 GB

AMP

5 GB

AMP

5 GB

AMP

5 GB

AMP

5 GB

AMP

5 GB

AMP

5 GB

AMP

5 GB

AMP

5 GB

AMP

Page 82: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Page 3-10 Understanding Space in Teradata

Spool Space Usage – Join Example Spool space is used for intermediate processing. One example of how spool is used is in joins. In the next frame, a product join strategy is illustrated. The optimizer chooses join plans based upon available indexes, data demographics, and the ‘cost’ of processing. The way a query uses a join as well as the filter criteria applied may greatly impact the amount of spool require to execute a particular query. For example, filter on date or location may reduce the size of intermediate spool files in join processing.

When joining tables with different distributions, often one or both tables will

need to ‘moved’ in spool to perform the join.

SELECT *FROM employee, departmentWHERE employee.dept= department.dept;

6 FOSTER 4008 BAKER 310

3 JONES 3104 CLAY 400

1 BROWN 2007 GRAY 310

2 SMITH 3105 PETERS 150

Employee rows hash distributed on Employee.Enum (UPI)

400 EDUCATION200 FINANCE310 MFG.150 PAYROLL

Department rows hash distributed on Department.Dept (UPI)

Spool file after duplicating the Department rows310 MFG.400 EDUCATION200 FINANCE150 PAYROLL

310 MFG.400 EDUCATION200 FINANCE150 PAYROLL

310 MFG.400 EDUCATION200 FINANCE150 PAYROLL

310 MFG.400 EDUCATION200 FINANCE150 PAYROLL

EMPLOYEEDEPARTMENTENUM NAME DEPT

PK FKUPI

1 BROWN 2002 SMITH 3103 JONES 3104 CLAY 4005 PETERS 1506 FOSTER 4007 GRAY 3108 BAKER 310

DEPT NAMEPKUPI150 PAYROLL200 FINANCE310 MFG.400 EDUCATION

In order to perform a join, data must be on the same amp. In this case, this is achieved by duplication of the smaller table.

Page 83: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Understanding Space in Teradata Page 3-11

Spool Usage – “Derived” Table Example “Derived” tables are those that are created automatically by the database for the life of the query. The database creates the table for us and then drops the table when it is no longer required. To do this we need to provide the database with information necessary to use it, namely: a table name, column names, and information on what to put in it. The example on the next frame illustrates how to accomplish this. The table is then materialized into spool and dropped when no longer needed. “Derived” tables are made out of spool. In a complex query, a large number of “Derived” tables may increase the likelihood of a query running out of spool.

Query to populate AvgT

Table Name – “AvgT”

Derived tables -• are “database created” tables that are for a single query only.• are discarded by the database when they are no longer required.• are materialized into a spool life.• are referenced and treated as any permanent table by the database.• must be defined by the author of the query, and require the following: a table name columns and their names a SELECT that is used to populate the table

SELECT Last_Name, Salary_Amount, AvgSal

FROM Employee e, (SELECT AVG(Salary_Amount)

FROM Employee) AS AvgT (AvgSal)WHERE e.Salary_Amount > AvgT.AvgSal;

Column Name(s) – “AvgSal”

Page 84: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Page 3-12 Understanding Space in Teradata

Spool Usage – Volatile Table Example Volatile tables can be created as needed for queries that need interim results sets that are manually maintained. Any volatile tables you create are dropped automatically as soon as your session logs off. Volatile tables are always created in the user’s spool space. Each user session can materialize as many as 1000 volatile tables at a time.

CREATE VOLATILE TABLE vt_deptsal(deptno SMALLINT,avgsal DEC(9,2),maxsal DEC(9,2),minsal DEC(9,2),sumsal DEC(9,2),empcnt SMALLINT)

ON COMMIT PRESERVE ROWS;

1) Create a volatile table. TABLE is created in SPOOL

INSERT INTO vt_deptsal SELECT dept ,

AVG(sal) ,MAX(sal) ,MIN(sal), SUM(sal), COUNT(emp)

FROM emp_views.empGROUP BY 1;

2) Populate the volatile table with computed aggregates.

ROWS are populated in SPOOL

SELECT * FROM vt_deptsal ORDER BY 3;

deptno avgsal maxsal minsal sumsal empcnt------ ----------- ----------- ----------- ----------- ------

301 29350.00 29450.00 29250.00 58700.00 3401 35545.83 46000.00 24500.00 213275.00 7403 38700.00 49700.00 31000.00 193500.00 6402 52500.00 52500.00 52500.00 52500.00 2

3)

Volatile tables are often used to store derived, interim results gathered from permanent table data.

Page 85: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Understanding Space in Teradata Page 3-13

Why Do Users Have Spool Limits? Each user in a Teradata database can be assigned an amount of spool for query processing. Database Administrators (DBAs) have to assess what type of work a user or group of users will submit to Teradata to determine the appropriate amount of spool. Users that submit OLTP type queries or quick lookup type queries will typically have lower amounts of spool assigned to them. Users that do complex queries that consume large numbers of rows will typically have higher amounts of spool assigned to them. Spool limits are leveraged as queries are executed. If a query does not have enough spool space to complete processing in any step, it will fail. Spool acts as a trip wire to ensure that there is a finite amount of space that can be used by each user before they negatively impact parallelism on the system.

• Groups of users are assigned spool limits based upon the type of work they will execute on the system.

• In the case of a ‘run-away’ or ‘bad’ query, spool space acts as a ‘trip wire’ or like a leash limiting how much resource may be consumed.

• When a user exceeds their spool limit at any point in query processing, the query will fail.

What happens when a user exceeds their spool allocation?

2646 No more spool space in %DBID • One possible cause is a poorly written query (i.e. product join or lack

of WHERE clauses)• Other causes include poorly distributed tables (uneven distribution

across the amps) or skewed processing that is causing one or more amp to not have available spool

Page 86: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Page 3-14 Understanding Space in Teradata

Module 3: Summary The next frame summarizes some important concepts regarding this module.

• Space in Teradata is made up of Permanent, Temporary, and Spool space.

• Spool space is used to store interim processing for users during queries.

• Activities like joining tables, using derived or volatile tables, or aggregations may use spool space.

• Each user is assigned spool space based upon the types of queries they need to execute and the overall sizing of the system.

• When a user exceeds their spool allocation in a query, the query fails and produces an error.

Page 87: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Understanding Space in Teradata Page 3-15

Module 3: Review Questions Check your understanding of the concepts discussed in this module by completing the review questions as directed by your instructor.

1. True or False: Space limits are enforced at the table level.

2. True or False: Users may use spool space in processing interim results sets such as join preparation or aggregation.

3. True or False: Users may update their own spool thresholds dynamically.

4. True or False: Spool space limits may be used as a ‘trip wire’ to prevent runaway or poorly written queries.

5. True or False: Spool errors are always an indicator that a user needs to be assigned additional spool space.

Page 88: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Page 3-16 Understanding Space in Teradata

Notes:

Page 89: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

EXPLAINS and Working with Problem Queries Page 4-1

Module 4

EXPLAINS and Working with Problem Queries

After completing this module, you should be able to:

• View the Teradata EXPLAIN for a given query

• Understand the time estimate in an EXPLAIN

• Identify key terms and their meanings in an EXPLAIN

• Describe common PNC errors and how to resolve them.

• Identify common issues that cause queries to be less efficient

Page 90: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Page 4-2 EXPLAINS and Working with Problem Queries

Table of Contents EXPLAIN Facility ....................................................................................................................... 4-3 A Note About Explain Times ...................................................................................................... 4-4 EXPLAIN Terminology .............................................................................................................. 4-5 Row and Time Estimates ............................................................................................................. 4-6 Row and Time Estimates (continued) .......................................................................................... 4-8 Common PNC Error Messages .................................................................................................. 4-10 Column/Parameter Does Not Exist ............................................................................................ 4-11 Float Precision Error .................................................................................................................. 4-12 Aggregation Error ...................................................................................................................... 4-13 Numeric Overflow Error ............................................................................................................ 4-14 Failed Conversion Error ............................................................................................................ 4-15 Invalid Timestamp ..................................................................................................................... 4-16 Invalid Operation on DateTime or Interval ............................................................................... 4-17 Object Errors – Column Not Found ........................................................................................... 4-18 Object Errors – Column is Ambiguous ..................................................................................... 4-19 Object Errors – Object Does Not Exist or User Does Not Have Access Right ......................... 4-20 Object Errors Example .............................................................................................................. 4-21 Workload Management Errors .................................................................................................. 4-22 Mistakes on Table Aliasing ....................................................................................................... 4-23 Table Aliasing Mistake Example ............................................................................................... 4-24 Data Conversion Example 1 ...................................................................................................... 4-25 Data Conversion Example 2 ...................................................................................................... 4-26 Matching Data Types ................................................................................................................. 4-27 ADAPT Best Practices – Filter by “Time_Period” ................................................................... 4-28 ADAPT Best Practices – Filter by “Time_Period” Range Example ......................................... 4-29 ADAPT Best Practices – Only Include Needed Columns ......................................................... 4-30 ADAPT Best Practices – Avoid Functions on Join Conditions and Where Clauses ................. 4-31 ADAPT Best Practices – Using Between .................................................................................. 4-32 ADAPT Best Practices – Options to Consider .......................................................................... 4-33 Common Syntax Differences – Hierarchical Joins .................................................................... 4-34 Common Syntax Differences – Outer Joins .............................................................................. 4-35 Common Syntax Differences – Nulls ........................................................................................ 4-36 Functions Common to Teradata and Oracle .............................................................................. 4-37 Recommended Coding Conventions ......................................................................................... 4-38 Module 4: Summary .................................................................................................................. 4-39 Module 4: Review Questions ..................................................................................................... 4-40 Module 4: Lab Exercises ........................................................................................................... 4-41 Module 4: Lab Exercises (continued) ........................................................................................ 4-42

Page 91: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

EXPLAINS and Working with Problem Queries Page 4-3

EXPLAIN Facility The EXPLAIN facility is a Teradata extension that provides you with an English translation of the steps chosen by the Optimizer to execute an SQL statement. It may be used on any valid Teradata SQL statement simply by preceding that statement with the keyword, EXPLAIN. The following is an example of how you would EXPLAIN a simple select statement:

EXPLAIN SELECT last_name, first_name, FROM employee; Using EXPLAIN during query development EXPLAIN parses the SQL statement, but doesn’t execute it. The EXPLAIN output provides the user with an execution strategy. This strategy provides direct feedback on what steps the Optimizer chooses to do. You may use EXPLAIN to analyse all joins and complex queries. It can save you lots of time and computing resources by pointing out problems with your SQL statements before you actually run them.

• May be used on any SQL statement, except EXPLAIN itself.

• Translates Optimizer’s query plan output into English.

• Can be used to analyze joins, long-running and complex queries.

• Run EXPLAIN during query development to:– Determine access paths– Show locking profiles– Validate the usage of indexes– Show activities for triggers and join index access– Identify if join conditions are missing

Page 92: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Page 4-4 EXPLAINS and Working with Problem Queries

A Note About Explain Times The EXPLAIN facility is a Teradata extension that provides you with an English translation of Although the times reported in the EXPLAIN output are presented in units of seconds, they are actually arbitrary units of time. These numbers are valuable because they permit you to compare alternate coding formulations of the same query with respect to their relative performance. They do not correlate with clock time. Save EXPLAIN results for future reference.

• Time in EXPLAIN output is relative.

• It is used as a way of comparing one query against another

• A query with a higher EXPLAIN time is more expensive to execute and a query with a lower EXPLAIN time is less expensive

• EXPLAIN output does not include the impact of Workload Management settings

• EXPLAIN output also does not take into account the current state of system resources in its estimates.

Page 93: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

EXPLAINS and Working with Problem Queries Page 4-5

EXPLAIN Terminology How much you can understand of what the EXPLAIN feature shows, depends upon your knowledge of Teradata features and concepts. There are some terms that are basic and useful in identifying how efficiently a given query performs.

. . . (Last Use) . . .A spool file is no longer needed and will be released after this step.

. . . eliminating duplicate rows . . .Doing a DISTINCT operation (process). (Duplicate rows that only exist in spool files.)

. . . by way of the sort key in spool Field 1 . . .Field 1 is created to allow a tag (value) sort.

. . . which is redistributed by hash code to all AMPs . . .Redistributing each of the data rows for a table to a new AMP based on the hash value for the column(s) involved in the join.

. . . which is duplicated on all AMPs . . .Copying all rows of a table onto each AMP in preparation for a join (e.g., a “Product Join”).

. . . which is built locally on the AMPs . . .Each Vproc builds a portion of a spool file from the data found on its local disk space.

. . . by way of a traversal of index #n extracting row IDs only . . .A spool file is built from the row-id values found in a secondary index (index #n). These row IDs will be used later for extracting rows from a table.

Page 94: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Page 4-6 EXPLAINS and Working with Problem Queries

Row and Time Estimates Confidence phrases are expressions that occur as result of whether statistics have been collected. This page illustrates how confidences can degrade even though statistics may have been collected on all required columns or column groups. As shown, ANDing and ORing several conditions in combination can degrade confidence. These phrases are from extracting rows from a table and are termed value access in our course terminology. High confidence row numbers should be considered for accuracy. EXPLAINs showing high confidence when the number of rows looks fairly or highly inaccurate is an indication that the statistics are old and should be recollected.

Page 95: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

EXPLAINS and Working with Problem Queries Page 4-7

These confidence statements indicate how accurate the database believes the demographics are based on statistics and other factors

Some phrases EXPLAINs use to express “confidence” for a retrieve from a table:

. . . with high confidence . . .Restricting conditions exist on an index or column that has collected statistics.

. . . with low confidence . . .• Restricting conditions exist on an index having no statistics, but estimates

can be based upon a sampling of the index.• Restricting conditions exist on an index or column that has collected

statistics, but is “ANDed” together with a non-indexed column having collected statistics.

• Restricting conditions exist on an index or column that has collected statistics, but is “ORed” together with another column or (indexed or not) collected statistics.

. . . with no confidence . . .Conditions outside the above.

Page 96: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Page 4-8 EXPLAINS and Working with Problem Queries

Row and Time Estimates (continued) Join cardinality and rows per value estimates nearly always have a lower confidence level than for a single-table retrieval under analogous conditions for several reasons, including:

• Join cardinality estimates only rate High confidence when there is only a single row in both the left and right relation in the join.

• The confidence level for a join operation never exceeds that of its input relations and assumes the confidence for the relation having the lower confidence.

The confidence phrases shown reflect those for join conditions. Note that the highest confidence that can be exhibited depends upon the table from which the lowest confidence exists. The definitions are as follows.

Confidence Level

Meaning Reason

No The system has neither Low nor High nor Index Join confidence in the estimated join cardinality.

One (or both) relation(s) in the join does not have statistics on the join columns.

Low The system is moderately certain that the estimated join cardinality is accurate.

• There are statistics on the join columns of both the left and right relations.

• One relation in the join has Low confidence and the other has one of these confidence levels: ◦ Low ◦ High ◦ Index Join

High The system is fairly certain that the estimated join cardinality is accurate.

One relation in the join has one of these confidence levels:

1. High 2. Index Join

Index Join The system is fairly certain that the estimated join cardinality is accurate because of a uniqueness constraint on the join columns.

• There is a unique index on the join columns.

• There is a foreign key relationship between the two relations in the join.

Page 97: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

EXPLAINS and Working with Problem Queries Page 4-9

The following are “confidence” phrases for a join:

. . . with index join confidence . . .A join condition via a unique index.

. . . with high confidence . . .One input relation has high confidence and the other has high or index join confidence.

. . . with low confidence . . .One input relation has low confidence and the other has low, high, or join index confidence.

. . . with no confidence . . .• One input relation has no confidence.• Statistics do not exist for either join field.

These confidence statements indicate how accurate the database believes the join demographics are based on statistics and other factors

Page 98: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Page 4-10 EXPLAINS and Working with Problem Queries

Common PNC Error Messages Error messages can be the result of many common SQL mistakes. In the following frame we have some examples of many of the most common error messages. The pages that follow will discuss what causes these errors and how to avoid them.

Common Error Messages:3810: Column/Parameter [Column_Name] does not exist. 3754: Precision error in FLOAT type constant or during implicit

conversions3504: Selected non-aggregate values must be part of an associated group5628: Column [Column_Name] not found in [VIEW_Name].

3523: The user does not have SELECT access to [View_Name].

3807: Object [View_Name] does not exist3733: The SELECT has more than one WHERE clause2616: Numeric Overflow occurred during computation3535: A character string failed conversion to a numeric value3149: TDWM Filter violation for Query Request: For Rule Name

[Workload_Management_Rule_Name]6760: Invalid timestamp5407: Invalid operation for DateTime or Interval3809: Column [column_name] is ambiguous.

Page 99: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

EXPLAINS and Working with Problem Queries Page 4-11

Column/Parameter Does Not Exist Explanation: An object referenced in the query cannot be resolved by the database.

Error Message:3810: Column/Parameter [Column_Name] does not exist.

SELECTCIF_CUSTOMER_FULL.CIF_KEY,CIF_CUSTOMER_FULL.CIF_PERMANENT_KEY,……..……..CIF_CUSTOMER_FULL.STATE,CIF_CUSTOMER_FULL.ZIP_CODEFROM ADATD01Q_ADAPT_DM.CIF_CUSTOMERWHERE TIME_PERIOD = '2016-02-29';

Cause:Often this is caused by a mistyped column name or by referring to a column that does not exist in a given view.

Solution:Verify the View column list and the spelling of the column name.

Page 100: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Page 4-12 EXPLAINS and Working with Problem Queries

Float Precision Error Explanation: Accuracy was lost while converting a number or when doing implicit conversions. Float numbers are representable to 15 digits of precision in the database. If more digits are used an error is reported.

Error Message:3754: Precision error in FLOAT type constant or during implicit

conversions

Example: select *from EDWTD01P_ETL_STAGE_V.ABC_CO_ACCT_ACTIVITYwhere ABC_CO_ACCT_ACTIVITY.ACCT_ID_NUM = 01060660390434927334;

Cause:Float data types can be used to represent data values that are beyond the ranges of the other numeric data types. Note, however, that they have only 15 digits of precision compared to the integer and decimal data types. Often, this happens as a result of an implicit conversion during a comparison of a numeric field to a character field. By default, for this conversion Teradata translates both to a FLOAT data type.

Resolution:Look at the DDL (e.g. do SHOW for the problem query) and figure out where which column is causing the implicit conversion.. Then do explicit CAST to a compatible datatype of higher numeric precision such as a DECIMAL (this can have up to 18 digits of precision)

Page 101: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

EXPLAINS and Working with Problem Queries Page 4-13

Aggregation Error This error occurs when an aggregate query includes a non-aggregate expression in the SELECT list, WHERE clause, ORDER BY clause, HAVING clause, or WITH list that is not also exactly specified in the corresponding GROUP BY or WITH...BY clause. Note that we also return this error when ORDER BY and WITH clauses contain aggregates but the query does not.

Error Messages:3504: Selected non-aggregate values must be part of an associated group

Sample User Query:

SELECT PROGRAM_KEY, count (LIST_KEY)as recordsFROM TCHTD01P_TCH_V.B_PROGRAM_LIST_FACTWHERE PROGRAM_KEY = 7709;

Corrected Sample User Query:

SELECT PGD.PROGRAM_KEY, PGD.PROG_SOURCE_CODE, COUNT(PLF.LIST_KEY) AS RECORDSLOADEDFROM TCHTD01P_TCH_V.B_PROGRAM_DIM PGD

LEFT OUTER JOIN TCHTD01P_TCH_V.B_PROGRAM_LIST_FACT PLF on PGD.PROGRAM_KEY = PLF.PROGRAM_KEY

--LEFT OUTER JOIN TCHTD01P_TCH_V.B_LIST_DIM PLD ON PLF.LIST_KEY = PLD.LIST_KEYWHERE PGD.PROGRAM_KEY = 8100 GROUP BY PGD.PROGRAM_KEY, PGD.PROG_SOURCE_CODE, PLF.LIST_KEYOrder by PLF.PROGRAM_KEY;

Page 102: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Page 4-14 EXPLAINS and Working with Problem Queries

Numeric Overflow Error This error indicates that an expression has produced a result that is too large to be expressed in the requested data type. (For example, a result of 100,000.00 and a data type of DECIMAL(5,2).)

Error Message:2616: Numeric Overflow occurred during computation

Example:select count(1)from EDWTD01P_ETL_STAGE_V.COR_POST_TRANSACTION_DLY;

The size of the result will exceed the default data type for this aggregation. For example, this count will output as an integer. Any value outside of the range -2,147,483,648 to +2,147,483,647 will overflow the integer data type.

Solution: Cast problem column(s) into a larger data type (such as a BigInt).

Page 103: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

EXPLAINS and Working with Problem Queries Page 4-15

Failed Conversion Error A character string constant in a query is in a context requiring that it represent a numeric value, and it does not.

Error Message:3535: A character string failed conversion to a numeric value

Example:SELECT *

FROM TCHTD01Q_TCH_V.B_PROGRAM_DIMWHERE PROG_CAMPAIGN_START_DT > '09/04/2015';

Reasoning: The query is being processed as a comparison between a DATE data type (Last_Statement_Date) and a string (‘01/01/1999).

Solution:Use the DATE keyword and the ‘YYYY-MM-DD’ format to avoid this issue:SELECT *

FROM TCHTD01Q_TCH_V.B_PROGRAM_DIMWHERE PROG_CAMPAIGN_START_DT > DATE '09/04/2015';

Page 104: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Page 4-16 EXPLAINS and Working with Problem Queries

Invalid Timestamp This error occurs when the specified timestamp is invalid. For instance, timestamp values like ’25-12-2001 26:18:22 AM’ (format ’DD-MM-YYYYBHH:MM:SSBT’) (26 hours is invalid) or ’32-01-2001 04:05:05’ (format ’DD-MMYYYYBHH:MM:SS’) (32 days are invalid) result in this error

Common Error Messages:6760: Invalid timestamp

Example:SELECT PROGRAM_KEY, PROG_SOURCE_CODE, PROG_PROGRAM_DESC, XXXX_ROW_INSERT_DT,

PROG_SEQUENCE_FIRST_OF_FIVE, PROG_SEQUENCE_FOUR_OF_FIVE, PROG_LIST_HAS_ROWS,

XXXX_ROW_INSERT_USER_IDFROM TCHTD01Q_TCH_V.B_PROGRAM_DIMWHERE XXXX_ROW_INSERT_DT > '2014-12-31'ORDER BY PROGRAM_KEY DESC

Cause:Query uses invalid timestamp value

Solution:YYYY-MM-DDBHH:MI:SS.S is the correct format for timestamp data

Page 105: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

EXPLAINS and Working with Problem Queries Page 4-17

Invalid Operation on DateTime or Interval DateTime or Interval error messages are often the result of improperly referencing a DateTime or Interval data type.

Error Message:5407: Invalid operation for DateTime or Interval

Example:SELECT PROGRAM_KEY, PROG_SOURCE_CODE, PROG_PROGRAM_DESC, XXXX_ROW_INSERT_DT,PROG_SEQUENCE_FIRST_OF_FIVE, PROG_SEQUENCE_FOUR_OF_FIVE,

PROG_LIST_HAS_ROWS,XXXX_ROW_INSERT_USER_IDFROM TCHTD01Q_TCH_V.B_PROGRAM_DIMWHERE XXXX_ROW_INSERT_DT > 2014-12-31

ORDER BY PROGRAM_KEY DESC;

Correction:SELECT PROGRAM_KEY, PROG_SOURCE_CODE, PROG_PROGRAM_DESC, XXXX_ROW_INSERT_DT,PROG_SEQUENCE_FIRST_OF_FIVE, PROG_SEQUENCE_FOUR_OF_FIVE,

PROG_LIST_HAS_ROWS,XXXX_ROW_INSERT_USER_IDFROM TCHTD01Q_TCH_V.B_PROGRAM_DIMWHERE XXXX_ROW_INSERT_DT > DATE ‘2014-12-31’

ORDER BY PROGRAM_KEY DESC;

Page 106: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Page 4-18 EXPLAINS and Working with Problem Queries

Object Errors – Column Not Found Error messages such as this usually result from referring to a column not referenced in either the tables or views included in the FROM clause. Often, this is the result of including a column in a SELECT statement that is referenced in a subquery.

Common Error Messages:5628: Column [Column_Name] not found in [VIEW_Name].

Cause:Often this is caused by a mistyped column name or by referring to a column that does not exist in a given view.

Solution:Verify the View column list and the spelling of the column name.

Page 107: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

EXPLAINS and Working with Problem Queries Page 4-19

Object Errors – Column is Ambiguous This error message originates when one or more views/tables include the same column name. To resolve this, qualify object names for columns in one or more objects.

Error Message:3809: Column [column_name] is ambiguous.

Cause:Typically this is the result of one or more columns having the same name in one or more tables/views.

Solution:Fully qualify column names by the table/view from which they originate.

Example: SELECT employee.Last_Name, First_Name, Employee.Department_Number, d.Manager_Employee_Number

FROM Employee, Department AS dWHERE Department_Number = d.Department_Number;

SELECT e.Last_Name, First_Name, e.Department_Number, d.Manager_Employee_Number

FROM Employee e, Department dWHERE e.Department_Number = d.Department_Number;

Qualification not required

Qualification required.

Page 108: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Page 4-20 EXPLAINS and Working with Problem Queries

Object Errors – Object Does Not Exist or User Does Not Have Access Right It is possible that users may either reference an object that they do not have the needed access. It is also possible that users may reference object incorrectly (typos, wrong database location, etc). Either of these scenarios may result in error messages such as the ones seen on the following frame. When this occurs, it recommended that PNC Teradata users contact the HELP DESK as they will be able to assist users more quickly.

Error Message:3523: The user does not have SELECT access to [View_Name].

3807: Object [View_Name] does not exist.

Cause:3523 – Most often, this is because the user has tried to perform SQL on an object that they for which they do not have the correct rights

3807 – Often this is the result of incorrect typing or incorrectly referencing the database where a view is located

Solution:Check the view, macro, and column names to make sure they are correct. If they are in fact correct and these errors still occur, contact the HELP DESK for assistance

Page 109: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

EXPLAINS and Working with Problem Queries Page 4-21

Object Errors Example Many object errors that return the 3807 ‘Object does not exist’ message are because an object is referred to incorrectly. This can be as simple as a misspelled column name or alias.

SELECTACCT.Account_Nbr, ACCT.Account_Nbr_Modifier, ACCT.Period_End_Date,AGR.Bank_Nbr,AGR.Account_Type_Code,AGR.Product_ID,AGR.Account_Product_Type_CodeFROM TM_Workshop.ABC_ACCOUNT ACCT LEFT OUTER JOINTM_Workshop.AGREEMENT AGRONACCT.Account_Nbr = AGR.Account_NbrAND ACCT.Account_Nbr_Modifier =AGR.Account_Nbr_ModifierAND ACCT.Period_End_Date=AGR.Period_End_DateAND ACCT.Account_Product_Type_Code =AGR.Acct_Status_Type_CodeWHERE ACC.Account_Nbr = 154777 order by ACC.Period_End_Date DESC; ACC is a typo of the alias ACCT

Executed as Single statement. Failed [3807 : 42S02] Object 'ACC' does not exist. Elapsed time = 00:00:00.047

Page 110: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Page 4-22 EXPLAINS and Working with Problem Queries

Workload Management Errors Workload Management setting are in place to ensure that performance goals are achieved. Database administrators can setup rules that reject queries that exceed certain criteria such as step time or row counts. If you receive this message, please contact the HELP DESK and they will assist you.

Error Message3149: TDWM Filter violation for Query Request: For Rule Name

[Workload_Management_Rule_Name].

Cause:A filter in Workload Management has caused this query to be rejected. Common reasons for rejection include excessive row counts and/or processing times.

Solution:Contact the HELP DESK.

Page 111: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

EXPLAINS and Working with Problem Queries Page 4-23

Mistakes on Table Aliasing When using aliases in writing joins, one must be careful to always use alias names when referencing, and not use the aliased table names. In our examples, the table Department has been aliased as “Dept” in the FROM, but the join condition references Department as a table name and does not reference the alias. The database will interpret this as having four tables being joined, namely: “Emp,” “Dept,” “Job,” and “Department.” To understand what is happening one must realize that the FROM clause is not required in a SQL request. For instance, the following is technically acceptable, but terribly repugnant.

SELECT Last_Name WHERE Employee.Department_Number = Department.Department_Number;

On the next frame, the join condition references a table called “Department” which is not referenced in a FROM clause, just like the previous example on this page. Also, a table called “Dept” (in a “FROM” clause alias) has no join condition. The optimizer will interpret this as a cross join between (likely) Dept and Department, the result of which will then be joined to Employee (in the immediate example). Ouch!

SELECT Last_Name, First_Name,Department_Name, Description

FROM Employee AS Emp JOIN Department DeptON Emp.Department_Number = Department.Department_Number

JOIN JobON Emp.Job_Code = Job.Job_Code;

SELECT Last_Name, First_name,Department_Name, Description

FROM Employee Emp, Department AS Dept, JobWHERE Emp.Department_Number = Department.Department_NumberAND Emp.Job_Code = Job.Job_Code;

• Be careful! Do not alias a table and then use the name instead of the alias.• In the examples below, the first one will fail due to a syntax error (ANSI 92).• The second will cause a four-table join, one of which is a self join between Dept

(the aliased Department table) and Department!

Page 112: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Page 4-24 EXPLAINS and Working with Problem Queries

Table Aliasing Mistake Example Another example of an aliasing mistake appears in the next frame. Note: many unintended and costly product joins result from using aliases and table names interchangeably in the same query.

SELECT Account.ACCOUNT_NUMBER,Account.ICDM_COMPANY,Account.BALANCE,Account.DATE_CLOSED,Account.DATE_OPENED,CIF_CUST.CIF_KEY,CIF_CUST.STATE,

CIF_CUST.ZIP_CODEFROM ADATD01p_ADAPT_DM.ACCOUNT Acct

ADATD01p_ADAPT_DM.CUST_ACCOUNT_RELATIONSHIP CUST_ACCOUNT,ADATD01p_ADAPT_DM.CIF_CUSTOMER CIF_CUST

WHERE Acct.account_number=CUST_ACCOUNT.account_numberAND Acct.time_period=CUST_ACCOUNT.time_periodAND Acct.SERVICE_TYPE= CUST_ACCOUNT.SERVICE_TYPE AND CUST_ACCOUNT.time_period=CIF_CUST.time_period

AND CUST_ACCOUNT.cif_permanent_key=CIF_CUST.cif_permanent_keyAND CIF_CUST.time_period=Account.time_periodAND Acc.TIME_PERIOD = TRUNC(TO_DATE($${1},$${2})) AND Acc.SERVICE_TYPE = $${3} AND CUST_ACCOUNT.CUST_ACCT_RELATIONSHIP_CODE=$${4} ;

Uses Account (full name)

Uses Alias Acct

As a result of this aliasing mistake, ACCOUNT will be joined an extra time!

Page 113: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

EXPLAINS and Working with Problem Queries Page 4-25

Data Conversion Example 1 The example on the next frame illustrates how data conversion adversely affects system performance.

• You can see the results of the first EXPLAIN. Note that total estimated time to perform this SELECT is minimal. The system can process this request quickly because the data type of the literal value matches the column type. A character column value (col1) is being compared to a character literal (‘8’) which allows TERADATA to use the UPI defined on c1 for access and for maximum efficiency. The query executes as a UPI SELECT.

• In the second SELECT statement, the character column value (col1) is compared with a

numeric value (8). You should notice that the total “cost” for this SELECT is nearly 30 times the estimate for the preceding SELECT. The system must do a Full Table Scan and convert the character values in col1 to numeric to compare them against the numeric literal (8).

If the column was numeric and the literal value was character, the literal would convert to numeric and the result could be hashed, allowing unique primary index (UPI) access.

CREATE SET TABLE TFACT01.Table1(col1 CHAR(12) NOT NULL)

UNIQUE PRIMARY INDEX (col1);

EXPLAIN SELECT * FROM Table1 WHERE col1 = '8';

1) First, we do a single-AMP RETRIEVE step from TFACT01.Table1 by way of the unique primary index "TFACT01.Table1.col1 = '8' " with no residual conditions. The estimated time for this step is 0.01 seconds.

-> The row is sent directly back to the user as the result of statement 1. The total estimated time is 0.01 seconds.

EXPLAIN SELECT * FROM Table1 WHERE col1 = 8;

1) First, we lock a distinct TFACT01."pseudo table" for read on a RowHash to prevent global deadlock for TFACT01.Table1.

2) Next, we lock TFACT01.Table1 for read. 3) We do an all-AMPs RETRIEVE step from TFACT01.Table1 by way of an all-rows

scan with a condition of ("(TFACT01.Table1.col1 (FLOAT, FORMAT '-9.99999999999999E-999')UNICODE)= 8.00000000000000E 000") into Spool 1, which is built locally on the AMPs. The size of Spool 1 is estimated with no confidence to be 1,001 rows. The estimated time for this step is 0.28 seconds.

4) Finally, we send out an END TRANSACTION step to all AMPs involved in processing the request.

-> The contents of Spool 1 are sent back to the user as the result of statement 1. The total estimated time is 0.28 seconds.

Page 114: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Page 4-26 EXPLAINS and Working with Problem Queries

Data Conversion Example 2 In this example, we have two tables with a column that is defined in one table as a character data type and in another table the same column is defined as a numeric data type. Character values are enclosed in single quotes in expressions. Numeric values should not be enclosed in quotes. In the example, we see the impact of referring to a character as a numeric as well as the reverse. Both of these actions result in unnecessary data conversions.

In this example, two tables have been created with the same columns – but one is character and one is numeric (integer).

Case 1

CREATE TABLE Emp1(Emp_no CHAR(6),Emp_name CHAR(20))

UNIQUE PRIMARY INDEX (Emp_no);

Statement 1SELECT *FROM Emp1WHERE Emp_no = '1234';

Statement 2SELECT *FROM Emp1WHERE Emp_no = 1234;

Statement 1SELECT *FROM Emp2WHERE Emp_no = 1234;

Statement 2SELECT *FROM Emp2WHERE Emp_no = '1234';

Case 2Issue:If referencing a character data type in a WHERE clause, make sure to use quotes on the value you are comparingIf referencing a numeric data type in a WHERE clause, make sure you do NOT use quotes on the value you are comparing

Bottom Line:When a character is compared to a numeric or vice versa, an effective query can become a more costly full table scan.

Results in Full Table Scan Results in unnecessary conversion

CREATE TABLE Emp2(Emp_no INTEGER,Emp_name CHAR(20))

UNIQUE PRIMARY INDEX (Emp_no);

Page 115: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

EXPLAINS and Working with Problem Queries Page 4-27

Matching Data Types There are a few data types that are treated identically. When designing tables, views, and macros the developers and administrators will try to manage the data type assignments at the Domain level. For example, if Account_Nbr is CHAR(20) in one table, it will be a CHAR(20)) in every table. Designing a system around domains helps ensure that queries do not have unnecessary or unintentional conversions. Conversions may also reduce the query’s likelihood of performing well using index based access.

The following data types are considered identical to Teradata for purposes of avoiding unnecessary conversions:

BYTEINT = SMALLINT = INTEGER = BIGINT = DATE = DECIMAL (x,0)CHAR = VARCHAR = LONG VARCHARBYTE = VARBYTEGRAPHIC = VARGRAPHIC

Data Types should be maintained consistently across all columns representing the same data element or domain.

Page 116: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Page 4-28 EXPLAINS and Working with Problem Queries

ADAPT Best Practices – Filter by “Time_Period” Many tables you will access in ADAPT have large numbers of rows. Not applying the correct filters may result in your query failing because it runs out of spool before completing. To avoid this, use columns like TIME_PERIOD to filter queries to a specific date or range of dates.

Sample User Query: Corrected Sample User Query: SELECT ACC.ACCOUNT_NUMBER, CIF_CUST.DATE_CUSTOMER_ON_FILE, CIF_CUST.STATE, CIF_CUST.ZIP_CODE, ACC.COMPANY_ORG FROM ADATD01P_ADAPT_DM.ACCT ACC, ADATD01P_ADAPT_DM.CUST_ACCOUNT_RELATIONSHIP CUST_ACCOUNT, ADATD01P_ADAPT_DM.CIF_CUSTOMER CIF_CUST WHERE ACC.ACCOUNT_NUMBER = CUST_ACCOUNT.ACCOUNT_NUMBER AND ACC.TIME_PERIOD = CUST_ACCOUNT.TIME_PERIODAND ACC.SERVICE_TYPE = CUST_ACCOUNT.SERVICE_TYPE AND CUST_ACCOUNT.TIME_PERIOD = CIF_CUST.TIME_PERIOD AND CUST_ACCOUNT.CIF_PERMANENT_KEY = CIF_CUST.CIF_PERMANENT_KEY;

SELECT ACC.ACCOUNT_NUMBER, CIF_CUST.DATE_CUSTOMER_ON_FILE, CIF_CUST.STATE, CIF_CUST.ZIP_CODE, ACC.COMPANY_ORG FROM ADATD01P_ADAPT_DM.ACCT ACC, ADATD01P_ADAPT_DM.CUST_ACCOUNT_RELATIONSHIP CUST_ACCOUNT, ADATD01P_ADAPT_DM.CIF_CUSTOMER CIF_CUST WHEREACC.ACCOUNT_NUMBER = CUST_ACCOUNT.ACCOUNT_NUMBER AND ACC.TIME_PERIOD = CUST_ACCOUNT.TIME_PERIOD

AND ACC.TIME_PERIOD = ‘2015-12-31’AND ACC.SERVICE_TYPE = CUST_ACCOUNT.SERVICE_TYPE AND CUST_ACCOUNT.TIME_PERIOD = CIF_CUST.TIME_PERIOD AND CUST_ACCOUNT.CIF_PERMANENT_KEY = CIF_CUST.CIF_PERMANENT_KEY;

Page 117: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

EXPLAINS and Working with Problem Queries Page 4-29

ADAPT Best Practices – Filter by “Time_Period” Range Example For some queries, it may be appropriate to use an upper bound and a lower bound TIME_PERIOD to identify required rows for analysis.

Sample User Query:SELECT AL3.SOCIAL_SECURITY_NUMBER, MAX (AL1.TIME_PERIOD) as LAST_CLOSED_DATE FROM ADATD01P_ADAPT_DM.ACCT AL1, ADATD01P_ADAPT_DM.CUST_ACCOUNT_RELATIONSHIP AL2, ADATD01P_ADAPT_DM.CIF_CUSTOMER_FULL AL3 WHERE (AL2.TIME_PERIOD=AL1.TIME_PERIOD AND AL2.ACCOUNT_NUMBER=AL1.ACCOUNT_NUMBER AND AL2.SERVICE_TYPE=AL1.SERVICE_TYPE AND AL2.CIF_PERMANENT_KEY=AL3.CIF_PERMANENT_KEYAND AL2.TIME_PERIOD=AL3.TIME_PERIOD) AND (AL1.TIME_PERIOD>=$${0} AND AL1.SERVICE_TYPE=$${1} AND AL1.MONTH_END_STATUS=$${2} AND AL1.RCB_CONSUMER_METRIC_FLAG=$${3}) GROUP BY AL3.SOCIAL_SECURITY_NUMBER;

Corrected Sample User Query:SELECT AL3.SOCIAL_SECURITY_NUMBER, MAX (AL1.TIME_PERIOD ) as LAST_CLOSED_DATE FROM ADATD01P_ADAPT_DM.ACCT AL1, ADATD01P_ADAPT_DM.CUST_ACCOUNT_RELATIONSHIP AL2, ADATD01P_ADAPT_DM.CIF_CUSTOMER_FULL AL3 WHERE (AL2.TIME_PERIOD=AL1.TIME_PERIOD AND AL2.ACCOUNT_NUMBER=AL1.ACCOUNT_NUMBERAND AL2.SERVICE_TYPE=AL1.SERVICE_TYPEAND AL2.CIF_PERMANENT_KEY=AL3.CIF_PERMANENT_KEYAND AL2.TIME_PERIOD=AL3.TIME_PERIOD) AND (AL1.TIME_PERIOD>=$${0} AND (AL1.TIME_PERIOD<=$${4}AND AL1.SERVICE_TYPE=$${1} AND AL1.MONTH_END_STATUS=$${2} AND AL1.RCB_CONSUMER_METRIC_FLAG=$${3}) GROUP BY AL3.SOCIAL_SECURITY_NUMBER;

Page 118: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Page 4-30 EXPLAINS and Working with Problem Queries

ADAPT Best Practices – Only Include Needed Columns In many cases, you may use derived or volatile tables for different query needs. In these cases it is best to only carry forward the columns used in your analysis. Additional columns may reduce the efficiency of interim processing and increase the size of spool files unnecessarily.

Sample User Query: Corrected Sample User Query: SELECT *FROM ( SELECT * FROM BASE_ACCOUNT WHERE TIME_PERIOD=(SELECT R1_DATE FROM SUP_MONTHLY_TIME_PERIOD) AND MONTH_END_STATUS = $${4} AND UNMATCHED_ACCOUNT_FLAG = $${5} AND RELATED_ACCT_FLAG = $${6} ) BA INNER JOIN ( SELECT * FROM BASE_CUST_ACCOUNT_RELATIONSHIP WHERE TIME_PERIOD = (SELECT R1_DATE FROM SUP_MONTHLY_TIME_PERIOD) AND CUST_ACCT_RELATIONSHIP_CODE = $${7} ) BCAR ON BA.SERVICE_TYPE = BCAR.SERVICE_TYPE AND BA.ACCOUNT_NUMBER = BCAR.ACCOUNT_NUMBERAND BA.TIME_PERIOD = BCAR.TIME_PERIOD;

SELECT *FROM ( SELECT SERVICE_TYPE, ACCOUNT_NUMBER,TIME_PERIODFROM BASE_ACCOUNT WHERE TIME_PERIOD=(SELECT R1_DATE FROM SUP_MONTHLY_TIME_PERIOD) AND MONTH_END_STATUS = $${4} AND UNMATCHED_ACCOUNT_FLAG = $${5} AND RELATED_ACCT_FLAG = $${6} ) BA INNER JOIN ( SELECT CIF_PERMANENT_KEY, SERVICE_TYPE, ACCOUNT_NUMBER,TIME_PERIOD FROM BASE_CUST_ACCOUNT_RELATIONSHIPWHERE TIME_PERIOD = (SELECT R1_DATE FROM SUP_MONTHLY_TIME_PERIOD) AND CUST_ACCT_RELATIONSHIP_CODE = $${7} ) BCAR ON BA.SERVICE_TYPE = BCAR.SERVICE_TYPE AND BA.ACCOUNT_NUMBER = BCAR.ACCOUNT_NUMBERAND BA.TIME_PERIOD = BCAR.TIME_PERIOD;

Page 119: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

EXPLAINS and Working with Problem Queries Page 4-31

ADAPT Best Practices – Avoid Functions on Join Conditions and Where Clauses When comparing tables during a join or qualifying rows to be returned, using functions like SUBSTRING, concatenation, and partial string searches (LIKE) can result in poor performing queries. The reasoning behind this is that these functions often result in full table scans which are less efficient on larger tables.

SELECT Acc.ACCOUNT_NUMBER,CIF_CUST.DATE_CUSTOMER_ON_FILE,CIF_CUST.STATE,CIF_CUST.ZIP_CODE,ACC.COMPANY_ORG

FROM ADATD01P_ADAPT_DM.ACCT Acc,ADATD01P_ADAPT_DM.CUST_ACCOUNT_RELATIONSHIP

CUST_ACCOUNT,ADATD01P_ADAPT_DM.CIF_CUSTOMER CIF_CUST

WHERE Acc.account_number = CUST_ACCOUNT.account_numberAND Acc.time_period = CUST_ACCOUNT.time_periodAND Acc.SERVICE_TYPE || CUST_ACCOUNT.time_period= CUST_ACCOUNT.SERVICE_TYPE || CIF_CUST.time_period

AND CUST_ACCOUNT.cif_permanent_key = CIF_CUST.cif_permanent_keyAND acc.time_period = ‘2015-12-31’AND SUBSTRING(CIF_CUST.ZIP_CODE FROM 1 FOR 3) = ‘902’

Using concatenation or functions on joins and filter criteria will usually result in full table scans rather than index based access. This can make queries less efficient and more likely to have spool errors.

Page 120: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Page 4-32 EXPLAINS and Working with Problem Queries

ADAPT Best Practices – Using Between When comparing or filtering on a long sequential IN list, it is generally more efficient to use the BETWEEN operator.

Example 1ORIGINAL Query:SELECT * FROM adatd01p_adapt_dm.CIF_CUSTOMER_FULLWHERE time_period='2015-12-31‘ AND age IN (20,21,22,23,24,25,26,27,28,29,30 );

CORRECTED Query:SELECT * FROM adatd01p_adapt_dm.CIF_CUSTOMER_FULLWHERE time_period='2015-12-31'AND age BETWEEN 20 and 30;

Example 2ORIGINAL Query:SELECT * FROM adatd01p_adapt_dm.ACCTWHERE time_period IN ('2015-12-31',2015-11-30’,2015-10-31,’2015-09-30’);

CORRECTED Query:SELECT * FROM adatd01p_adapt_dm.ACCTWHERE time_period BETWEEN '2015-09-30' and ‘2015-12-31’;

Page 121: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

EXPLAINS and Working with Problem Queries Page 4-33

ADAPT Best Practices – Options to Consider Many queries can be written numerous ways. In the next frame are suggestions on some techniques that have better performing options. As always, an EXPLAIN plan can be used to compare and contrast the efficiency of two queries to determine which is better.

• Use filter criteria wisely – filters reduce the number of rows processed and can increase query efficiency.

• Instead of ‘OR-ed’ conditions, use the UNION ALL syntax.

• When evaluating NOT IN conditions, consider using a LEFT OUTER JOIN

• To remove duplicate records in a query consider compare using DISTINCT and GROUP BY– DISTINCT may be more effective where columns have a low number

of rows per value. – GROUP BY may be more effective for columns with a large number

of rows per value. – An EXPLAIN can allow you to verify which option works best in a

particular query without executing the query

Page 122: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Page 4-34 EXPLAINS and Working with Problem Queries

Common Syntax Differences – Hierarchical Joins A recursive query is a way to query hierarchies of data, such as an organizational structure, bill-of-materials, and document hierarchy. Recursion is typically characterized by three steps:

1. Initialization 2. Recursion, or repeated iteration of the logic through the hierarchy 3. Termination

Similarly, a recursive query has three execution phases:

1. Create an initial result set. 2. Recursion based on the existing result set. 3. Final query to return the final result set.

The following recursive query retrieves the employee numbers of all employees who directly or indirectly report to the manager with employee_number 801:

Oracle Hierarchical Join:SELECT employee_number , last_name, manager_employee_numberFROM employeesCONNECT BY PRIOR employee_number = manager_employee_number;

Teradata Hierarchical Join:WITH RECURSIVE temp_table (employee_number) AS( SELECT root.employee_number FROM employee root

WHERE root.manager_employee_number = 801UNION ALLSELECT indirect.employee_numberFROM temp_table direct, employee indirectWHERE direct.employee_number=indirect.manager_employee_number

)SELECT * FROM temp_table ORDER BY employee_number;

Page 123: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

EXPLAINS and Working with Problem Queries Page 4-35

Common Syntax Differences – Outer Joins The outer join is an extension of the inner join. The difference between outer joins and inner joins is the existence of an nonmatching row set in the outer join result. Outer joins of two or more tables perform an inner join of those tables according to a specified join condition and also return rows from the left join table, the right join table, or both, that do not match the inner join condition, extending the results rows with nulls in the nonmatching fields. The nonmatching rows are for those rows in the outer table that do not have a match in the inner table, while the matching rows in the outer join result are the rows that satisfy the join condition, which are exactly the same as those in the inner join result.

• Oracle Outer Join:Select …From TableA A

,TableB BWhere A.ColumnX = B.ColumnX (+)

• Teradata Outer Join:Select …From TableA A

LEFT OUTER JOIN TableB BON A.ColumnX = B.ColumnX

Page 124: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Page 4-36 EXPLAINS and Working with Problem Queries

Common Syntax Differences – Nulls NULL represents something of unknown quantity or value. NULL is an SQL keyword. NULL is not a data type but rather the representation of an unknown value for a column with a specified data type. For example, both columns defined as INTEGERS and columns defined as CHARACTERS may have null values. For an ascending sort, NULLs sort before numbers and characters. Teradata does not treat NULL the same as a space (blank) or a zero (0).

• Null character value is not a null string• Example:

INSERT INTO T VALUES ('', 'bb') ;INSERT INTO T VALUES (NULL, 'bb') ;SELECT A, B FROM T ;

• Oracle returns:? bb? bb

• Teradata returns:bb

? bb

Page 125: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

EXPLAINS and Working with Problem Queries Page 4-37

Functions Common to Teradata and Oracle Many industry compatible functions exist as extensions to ANSI SQL syntax. Teradata support many of these functions natively as of version 14.0 and 14.10.

String Functions

• ASCII• CHR• LENGTH• DECODE• DIFFERENCE• DIGITS• ENCODE• INITCAP• INSERT• LEFT• LPAD• RPAD• LTRIM• RTRIM• REPLACE• INSTR• TRANSLATE• TOKENIZE• EDITDISTANCE• NGRAM• TO_CHAR

Date Functions

• LAST_DAY• MONTHS_BETWEEN• OADD_MONTHS• TO_DATE

Regular Expressions

• REGEXP_SUBSTR• REGEXP_REPLACE• REGEXP_INSTR• REGEXP_SIMILAR• REGEXP_SPLIT

Numeric Functions

• POWER• SIGN• TRUNC• ROUND• GREATEST• LEAST• TO_NUMBER

Misc. Functions

• NVL

Ordered Analytic Functions

• Dense Rank• Cumulative Distribution• First Value/Last Value• Percentile_Cont• Percentile_Disc

Page 126: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Page 4-38 EXPLAINS and Working with Problem Queries

Recommended Coding Conventions The recommended coding convention represents a style of coding that many people use and has become (somewhat) a defacto standard. We say “somewhat” because SQL is a free-form language and can be written in what is termed “paragraph style” as shown. The query discussed will fail because there should be a space between the WHERE clause and the table named “department_number.”

select last_name,first_name,hire_date salary_amount from employee wheredepartment_number = 401 order by last_name;

What was probably meant:

SELECT last_name, first_name, hire_date, salary_amount FROM employee WHERE department_number = 401 ORDER BY last_name;

Although SQL is considered a “free-form” language, the following represents a commonly used convention for SQL coding.

SELECT last_name,first_name,hire_date,salary_amount

FROM employeeWHERE department_number = 401ORDER BY last_name;

The convention below, often referred to as “paragraph-style,” can be difficult to debug. Identify two potential problems with the following query.

select last_name,first_name,hire_date salary_amount from employee wheredepartment_number = 401 order by last_name;

Page 127: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

EXPLAINS and Working with Problem Queries Page 4-39

Module 4: Summary

• The EXPLAIN facility should be used when writing complex SQL.

• Understanding EXPLAIN terminology will make interpreting the plans easier.

• Problem queries may have indicators in their explains such as extreme step times or row counts.

• Many common performance issues and error messages can be prevented by using good formatting and syntax techniques

Page 128: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Page 4-40 EXPLAINS and Working with Problem Queries

Module 4: Review Questions Check your understanding of the concepts discussed in this module by completing the review questions as directed by your instructor.

False – it provides information about the relative "cost" of a particular query

False

False

False – relates to demographics and confidence in those demographics

False – An empty string is a string with no characters and a null is an unknown value

False – only the explicit syntax using the ON clause is valid in Teradata

True – this avoids unneeded product joins

True or False:

1. The EXPLAIN provides an estimate of how long a user can expect a query to execute.

1. Mistakes in table in aliasing will always result in an error message.

2. If a user does not have access to an object, an error message will advise that the object does not exist.

3. Explain terms regarding confidence reflect the user’s spool amount and resource priority.

4. Teradata supports empty strings (‘’) as nulls.

5. Outer joins may be written in Teradata with the join conditions included in the WHERE clause (implicit syntax).

6. When aliasing view or table names in a query, all aliases must be used consistently.

Page 129: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

EXPLAINS and Working with Problem Queries Page 4-41

Module 4: Lab Exercises Check your understanding of the concepts discussed in this module by completing the lab exercise as directed by your instructor.

For each of these queries, use Data Source Explorer to help you locate the errors.1. Locate and correct the three errors in the query below.

SELECT Period_End_Dat ,Part_ID ,Account_Number ,Account_Nbr_Modifier

Primary_Account_IndFROM TM_Workshop.PARTY_ACCOUNT;

2. Identify and correct the aggregation error below.SELECT Account_Nbr,Account_Nbr_Modifier,SUM(Total_Interest_Expense_Amt)FROM TM_Workshop.ABC_Account_SummaryWHERE Total_Interest_Expense_Amt > 100Group by Account_Nbr;

3. Which column in the following query is causing the error?SELECT cast(Account_Nbr as INTEGER),cast(Account_Nbr_Modifier as CHAR(2)),cast(Period_End_Date as VARCHAR(20)),cast(Account_Type_Code as INTEGER),cast(Acct_Close_Reason_Code as INTEGER)FROM tm_workshop.agreementsample 1000;

Page 130: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Page 4-42 EXPLAINS and Working with Problem Queries

Module 4: Lab Exercises (continued) Check your understanding of the concepts discussed in this module by completing the lab exercise as directed by your instructor.

4. Answer the questions using the explain text below.1) First, we lock a distinct PD."pseudo table" for read on a RowHash to prevent

global deadlock for PD.Employee. 2) Next, we lock a distinct PD."pseudo table" for read on a RowHash to prevent global

deadlock for PD.Department. 3) We lock PD.Employee for read, and we lock PD.Department for read. 4) We do an all-AMPs RETRIEVE step from PD.Department by way of an all-rows scan with

no residual conditions into Spool 2 (all_amps), which is duplicated on all AMPs. The size of Spool 2 is estimated with low confidence to be 144 rows (5,904 bytes). The estimated time for this step is 0.01 seconds.

5) We do an all-AMPs JOIN step from Spool 2 (Last Use) by way of an all-rows scan, which is joined to PD.Employee by way of an all-rows scan with no residual conditions. Spool 2 and PD.Employee are joined using a product join, with a join condition of ("(PD.Employee.Dept_Number = Dept_Number) OR(PD.Employee.Employee_Number = Dept_Mgr_Number)"). The result goes into Spool 1 (all_amps), which is built locally on the AMPs. The size of Spool 1 is estimated with index join confidence to be 1,067 rows (73,623 bytes). The estimated time for this step is 0.02 seconds.

6) Finally, we send out an END TRANSACTION step to all AMPs involved in processing the request.

-> The contents of Spool 1 are sent back to the user as the result of statement 1. The total estimated time is 0.04 seconds.

a. Which table is duplicated on all amps? b. What type of join is performed in this explain?c. What does the phrase ‘index join confidence’ indicate?

Page 131: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Course Survey Page 5-1

Module 5

Course Survey

Congratulations on completing this course.

Now it’s your turn to be heard. • What did you think of the class?• How was your experience?• What could be improved?• How was the instructor?

We value your opinion and we consistently make improvements to our courses based on student feedback.

Please take a few minutes and complete the survey at the link provided below.

http://www.tensupport.com/surveys/

Thank you for participating.

Page 132: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Page 5-2 Course Survey

http://www.tensupport.com/surveys/ As we near the end of the course, please go to the link above and provide your feedback about this course. Your feedback is helpful in refining the course and making necessary changes in how the course is delivered. This is especially important because your review is the best indicator of what worked and what needs improvement. The survey should take no more than five minutes to complete. The survey is used to improve the following areas:

• Course content • Courses offered • Course impact • Instructor skill set • Classroom environment

Some additional questions will allow you to provide greater detailed information, which helps even more with improvements. Thank you for attending this course and we appreciate your business!

Page 133: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Appendix A: Review Question Solutions Page A-1

Appendix A

Appendix A: Review Question Solutions

This Appendix contains answers to the review questions for the course modules.

Teradata Corporation Copyright ©2007–2016. All Rights Reserved.

Page 134: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Page A-2 Appendix A: Review Question Solutions

Module 1: Review Questions

This is not a best practice for security reasons.

Reset Perspective

1. True or False: Teradata Studio is only a tool for database administrators.

2. True or False: The Data Source Explorer preferences can be customized to show only database objects specified by the user.

3. True or False: You should save your password in your connection profile.

4. The ___________________ menu option under the Window menu can be used to refresh a Teradata Studio perspective to a default view.

5. Which of the following are not modular displays in Teradata Studio?a. Results Viewer b. Teradata SQL Historyc. Data Source Explorerd. Data Source Editor

Module 2: Review Questions

1. True or False: Users accessing DBC.TablesVX will find information on tables, views, macros, and stored procedures rather than only tables.

2. True or False: The data dictionary views can be used to locate information about data types and formats for columns in tables and views.

3. Which two of the following ‘HELP’ SQL below is valid?

a. HELP TM_WORKSHOP;

b. HELP TM_WORKSHOP.ABC_Account;

c. HELP TABLE TM_WORKSHOP.ABC_Account;

d. HELP COLUMN TM_WORKSHOP.ABC_Account.Product_ID;

e. HELP TM_WORKSHOP.ABC_Account.Product_ID;

Page 135: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Appendix A: Review Question Solutions Page A-3

Module 3: Review Questions

1. True or False: Space limits are enforced at the table level.

2. True or False: Users may use spool space in processing interim results sets such as join preparation or aggregation.

3. True or False: Users may update their own spool thresholds dynamically.

4. True or False: Spool space limits may be used as a ‘trip wire’ to prevent runaway or poorly written queries.

5. True or False: Spool errors are always an indicator that a user needs to be assigned additional spool space.

Module 4: Review Questions

False – it provides information about the relative "cost" of a particular query

False

False

False – relates to demographics and confidence in those demographics

False – An empty string is a string with no characters and a null is an unknown value

False – only the explicit syntax using the ON clause is valid in Teradata

True – this avoids unneeded product joins

True or False:

1. The EXPLAIN provides an estimate of how long a user can expect a query to execute.

1. Mistakes in table in aliasing will always result in an error message.

2. If a user does not have access to an object, an error message will advise that the object does not exist.

3. Explain terms regarding confidence reflect the user’s spool amount and resource priority.

4. Teradata supports empty strings (‘’) as nulls.

5. Outer joins may be written in Teradata with the join conditions included in the WHERE clause (implicit syntax).

6. When aliasing view or table names in a query, all aliases must be used consistently.

Page 136: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Page A-4 Appendix A: Review Question Solutions

Page 137: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Appendix B: Lab Exercise Solutions Page B-1

Appendix B

Solutions to Lab Exercises

This Appendix contains possible solutions to the lab exercises.

Page 138: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Page B-2 Appendix B: Lab Exercise Solutions

Table of Contents Module 1: Teradata Studio Features ............................................................................................ B-3 Module 2: Using the Data Dictionary and Built in Functions ................................................... B-11 Module 4: EXPLAINS and Working with Problem Queries .................................................... B-13

Page 139: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Appendix B: Lab Exercise Solutions Page B-3

Module 1: Teradata Studio Features 1. Use the existing default database connection as defined by the instructor. Update or

enter the following information: a. Username: Username provided by instructor b. Password: Password provided by instructor

Once the Database Server Name, User Name, and Password are entered, click the ADD button beside the JDBC Connection Properties box.

Page 140: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Page B-4 Appendix B: Lab Exercise Solutions

Using the drop down menus, select the JDBC property TMODE and the property value of TERA. Click OK. You will be asked to confirm if you want to replace the TMODE property. Click OK. Once the TMODE property is set, your screen should look like the following:

Page 141: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Appendix B: Lab Exercise Solutions Page B-5

1. Go to the Preferences menu and change the following characteristics: To Access the Preferences menu, click the Window menu and select Preferences.

a. Under Teradata Datatools Preferences (Data Source Explorer):

Page 142: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Page B-6 Appendix B: Lab Exercise Solutions

i. Uncheck the option to Show Databases and Users in Hierarchical Display ii. Select Load Options item User Choice

b. Under General:

i. Change the Text Font to 16. (Appearance >Colors and Fonts)

Click on Text Font to highlight it, then click the Edit button. Select the Size option and update the font size.

Page 143: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Appendix B: Lab Exercise Solutions Page B-7

ii. Edit the Undo History Size to 100 characters (Editors>Text Editors)

Click Ok to close the Preferences.

2. In the Data Source Explorer, add the following databases: 1. Employee_Sales 2. TM_Workshop

In the Data Source Explorer, expand the drop down next to your user name. You should see a folder labeled Databases. Expand that folder (by clicking). You will likely only see your username’s space and DBC in this folder. Right click the Databases folder and select the Teradata menu option. From the Teradata menu, select Load Database.

Page 144: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Page B-8 Appendix B: Lab Exercise Solutions

Add each of the databases listed above.

4. Browse the TM_Workshop database and locate the table ABC_Account.

Page 145: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Appendix B: Lab Exercise Solutions Page B-9

5. Right click and from the Teradata menu select Row Count. How many rows are in the ABC_Account table? ______

6. Execute the following select statement: SELECT Account_Nbr ,Bank_Nbr ,Account_Type_Code FROM TM_WORKSHOP.ABC_ACCOUNT SAMPLE 100;

7. Export the rows returned to Excel. Click on the icon shown above the arrow in the screenshot below:

Page 146: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Page B-10 Appendix B: Lab Exercise Solutions

Select your desktop as a destination and name the file results. Select the first Excel Option.

Page 147: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Appendix B: Lab Exercise Solutions Page B-11

Module 2: Using the Data Dictionary and Built in Functions

1. Using the Data Source Explorer, how many of the following are in the TM_Workshop database:

Views ________________ Tables ________________ Macros ________________

Expand the TM_Workshop folder if it is not already. Expand the tables,, views and macros folders.

Count the objects. Views: 13 Tables: 13 Macros: 1

2. How many tables/views in the TM_Workshop database have an Account_Nbr column? ________________ Execute the following SQL: SELECT tablename from DBC.ColumnsVX WHERE Columnname = 'account_number' and Databasename = 'TM_Workshop' Answer should be 0.

3. What is the data type for the Product_ID column in the TM_Workshop.ABC_Account table? ________________ Execute the following SQL:

Page 148: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Page B-12 Appendix B: Lab Exercise Solutions

SHOW TABLE TM_Workshop.ABC_Account; Answer should be INTEGER

4. Execute the following HELP and SHOW statements. (Note which ones are successful and which ones fail)

a. HELP VIEW TM_Workshop.ABC_Account (this will fail as ABC_Account is a table)

b. HELP TM_Workshop.Aggreement_V; (this will fail, object type not referenced

c. HELP VIEW TM_Workshop.Aggreement_V; (successful) d. SHOW MACRO TM_Workshop.Aggreement_By_Account_Nbr; (successful) e. SHOW VIEW TM_Workshop.ABC_Account_V; (successful)

Page 149: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Appendix B: Lab Exercise Solutions Page B-13

Module 4: EXPLAINS and Working with Problem Queries 1. Locate and correct the 3 errors in the query below:

SELECT Period_End_Dat ,Part_ID ,Account_Number ,Account_Nbr_Modifier

Primary_Account_Ind FROM TM_Workshop.PARTY_ACCOUNT;

Errors: Period_End_Dat should be Period_End_Date Account_Number should be Account_Nbr A comma should be between Account_Nbr_Modifier and Primary_Account_ind

2. Identify and correct the aggregation error below. SELECT Account_Nbr ,Account_Nbr_Modifier ,SUM(Total_Interest_Expense_Amt) FROM TM_Workshop.ABC_Account_Summary WHERE Total_Interest_Expense_Amt > 100 Group by Account_Nbr;

Error: The group by needs to also include Account_Nbr_Modifier.

3. Which column in the following query is causing the error? SELECT cast(Account_Nbr as INTEGER) ,cast(Account_Nbr_Modifier as CHAR(2)) ,cast(Period_End_Date as VARCHAR(20)) ,cast(Account_Type_Code as INTEGER) ,cast(Acct_Close_Reason_Code as INTEGER) FROM tm_workshop.agreement sample 1000;

Answer: Acct_Close_Reason_Code causes an error because this field includes character data.

4. Answer the questions using the explain text below. 1) First, we lock a distinct PD."pseudo table" for read on a

RowHash to prevent global deadlock for PD.Employee. 2) Next, we lock a distinct PD."pseudo table" for read on a

RowHash to prevent global deadlock for PD.Department. 3) We lock PD.Employee for read, and we lock PD.Department for

read. 4) We do an all-AMPs RETRIEVE step from PD.Department by way of

an all-rows scan with no residual conditions into Spool 2 (all_amps), which is duplicated on all AMPs. The size of Spool 2 is estimated with low confidence to be 144 rows

Page 150: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Page B-14 Appendix B: Lab Exercise Solutions

(5,904 bytes). The estimated time for this step is 0.01 seconds.

5) We do an all-AMPs JOIN step from Spool 2 (Last Use) by way of an all-rows scan, which is joined to PD.Employee by way of an all-rows scan with no residual conditions. Spool 2 and PD.Employee are joined using a product join, with a join condition of ("(PD.Employee.Dept_Number = Dept_Number) OR (PD.Employee.Employee_Number = Dept_Mgr_Number)"). The result goes into Spool 1 (all_amps), which is built locally on the AMPs. The size of Spool 1 is estimated with index join confidence to be 1,067 rows (73,623 bytes). The estimated time for this step is 0.02 seconds.

6) Finally, we send out an END TRANSACTION step to all AMPs involved in processing the request.

-> The contents of Spool 1 are sent back to the user as the result of statement 1. The total estimated time is 0.04 seconds.

a. Which table is duplicated on all amps?

Answer: Department

b. What type of join is performed in this explain? Answer: Product Join

c. What does the phrase ‘index join confidence’ indicate? Answer: The system is fairly certain that the estimated join cardinality is accurate because of a uniqueness constraint on the join columns.

Page 151: Teradata SQL and Studio for ADAPT - TEN.Supporttensupport.com/toc/54986PNC - SQL Studio ADAPT... · Table of Contents Teradata SQL and Studio for ADAPT Version 15.10.2 Module 1 -

Appendix B: Lab Exercise Solutions Page B-15