Sie sind auf Seite 1von 18

OOPS

Solutions for Common Mistakes

Dell DDW / Teradata Project Documentation Dell Confidential


The LIKE statement

OOPS I used LIKE and did not find a


match
Problem Statement
COL1 LIKE %x
Reason
COL1 contains trailing blanks
Solutions
TRIM(COL1) LIKE %x
COL1 LIKE %x%

Dell DDW / Teradata Project Documentation Dell Confidential 2


The LIKE statement

OOPS I cant find % sign in a string


Problem Statement
COL1 LIKE %95%%
Reason
COL1 contains a % sign in the data
A % represents any string of zero or more arbitrary
characters.
Solution
Use an escape character
COL1 LIKE %95Z%%ESCAPE Z
Note
UNDERSCORE represents exactly one arbitrary
character.
To find UNDERSCORE, use an escape character

Dell DDW / Teradata Project Documentation Dell Confidential 3


ROWID in Teradata

OOPS I get error 5691 using ROWID


in joins
Problem
Teradata does not support using ROWID
Reason
ROWID has a different meaning and usage in Teradata
than in ORACLE
ROWID in Teradata is intended for internal and
debugging use only
In joins, the ROWID can change throughout a query, so
the place where you select it can change your result
Solution
Use the logical PK instead of ROWID

Dell DDW / Teradata Project Documentation Dell Confidential 4


Error Codes

OOPS - I got an error and I dont know


what it means
Problem
Go ask the DBA or NCR Consultant
Reason
Cant find the manual
Solution
It is hiding in the Teradata Manuals under the covert
name Messages
Teradata Manual/General Reference/Messages
Most of your errors will be in Chapter 6 RDBMS messages
If you read the error message in the manual and still
need help, then ask the DBA or NCR Consultant

Dell DDW / Teradata Project Documentation Dell Confidential 5


Error Codes

OOPS I got a 3610 error


The error states
PLEASE DO NOT SUBMIT LAST REQUEST
The SQL encountered a system bug
DO NOT resubmit the SQL
DO NOT run an Explain on the SQL
Save the SQL and notify the DBA
RISK of resubmitting
SQL causes Crash Dumps
SQL may cause system restart
If enough of these occur in a short time, the NODE will
drop out of the configuration

Dell DDW / Teradata Project Documentation Dell Confidential 6


Handling NULL Data

OOPS I thought I loaded NULL data but it is


not NULL
Problem
Loaded
Reason
In Teradata, two single ticks together denotes a character
string of length zero. It does not denote NULL
Solution
Load NULL
NULLIF()
Note
NULL indicates no value, nothing, nada
Join of NULL data results in no match, including NULL to NULL

Dell DDW / Teradata Project Documentation Dell Confidential 7


Handling NULL Data

OOPS- I concatenated some columns


and got nothing (NULL)
Problem Statement
Select COL1 || COL2 || COL3 || COL4
Reason
One or more of the columns contains NULL data
Solution
Convert the column that contains NULL to empty string
For this example, COL3 contains the NULL data
Select COL1 || COL2 || coalesce(COL3,) || COL4

Dell DDW / Teradata Project Documentation Dell Confidential 8


Handling NULL Data

OOPS I get an error aggregating a column


defined in a view with a NULL literal
Problem Symptom
Error 3647 - NULL may not be used as the argument for an aggregate
function
Reason
The view contains a column defined with a NULL literal
Solution
Select but do not aggregate the column containing the NULL
literal
Option
NULL + 0 as COL1
Do this in SQL or in the View

Note
Columns containing NULL DATA may be aggregated

Dell DDW / Teradata Project Documentation Dell Confidential 9


Handling NULL Data
DDD
Problem View Alternate View
REPLACE VIEW FINANCE.ORDER_SUMMARY REPLACE VIEW FINANCE.ORDER_SUMMARY
AS AS
SELECT SELECT
BUSINESS_UNIT_ID BUSINESS_UNIT_ID
,ORDER_NUM ,ORDER_NUM
,NULL AS TEST_QTY ,NULL + 0 AS TEST_QTY
,ORDER_QTY ,ORDER_QTY
FROM FINANCE_T.ORDER_DETAIL FROM FINANCE_T.ORDER_DETAIL

SQL Solution Use Original SQL


SELECT SELECT
BUSINESS_UNIT_ID BUSINESS_UNIT_ID
,ORDER_NUM ,ORDER_NUM
,TEST_QTY , SUM(TEST_QTY)
,SUM(ORDER_QTY) ,SUM(ORDER_QTY)
FROM FINANCE.ORDER_SUMMARY FROM FINANCE.ORDER_SUMMARY
GROUP BY 1,2 GROUP BY 1,2

Dell DDW / Teradata Project Documentation Dell Confidential 10


Mixed use of Alias with Table Name

OOPS this query ran out of spool


Problem
A Cartesian Product join of the aliased and non-
aliased tables

Example:
SELECT
OD.BUSINESS_UNIT_ID
,OD.ORDER_NUM
,SUM(OD.ORDER_QTY)
FROM FINANCE.ORDER_DETAIL OD
WHERE FINANCE.ORDER_DETAIL.BUSINESS_UNIT_ID = 11
GROUP BY 1,2

Dell DDW / Teradata Project Documentation Dell Confidential 11


Mixed use of Alias with Table name

Solution 1
Use ALIAS only
Recommended solution
Example:
SELECT
OD.BUSINESS_UNIT_ID
,OD.ORDER_NUM
,SUM(OD.ORDER_QTY)
FROM FINANCE.ORDER_DETAIL OD
WHERE OD.BUSINESS_UNIT_ID = 11
GROUP BY 1,2

Solution 2
Do not use an ALIAS
Use Table Name only
Not recommended

Dell DDW / Teradata Project Documentation Dell Confidential 12


Access Locks

OOPS Rows are missing when table is being


updated
Problem
Selecting from a table using an Access Lock while the table is
maintained by an Update statement with join(s).
Including selecting from Views that contain the Access Lock
Reason
Update processes by doing a delete of all affected rows
followed by an insert
The Select runs during the delete phase
Solution 1
Select using a read lock
Waits for the update to complete
Solution 2
Update using an exclusive lock
Blocks access locks from Read

Dell DDW / Teradata Project Documentation Dell Confidential 13


Create Table AS

OOPS The table is skewed


Problem
CREATE TABLE AS (Select) with [NO] DATA
Primary Index was not specified
Reason
A Primary Index will be defined by default
The Default Primary Index is the first column in the Select
Solution 1
Specify a primary index
Follow the guidelines for choosing a primary index
Solution 2
Use CREATE TABLE AS tablename
Indices are preserved

Dell DDW / Teradata Project Documentation Dell Confidential 14


BTEQ Retry Option

OOPS- Teradata Transaction control broke


when I ran my BTEQ script
Explanation
After certain failures BTEQ will Retry if the RETRY option is set
to ON
Explicit Transaction Pseudo Example
BT
Delete from DB01.TBL01
Insert into DB01.TBL01
ET
If the transaction was in the Insert step and the request is
automatically retried after a failure
The Delete is rolled back (data replaced back into the table)
The Delete is not reprocessed
The Insert is reprocessed
Results in Duplicate Rows

Dell DDW / Teradata Project Documentation Dell Confidential 15


BTEQ Retry Option

OOPS- Teradata Transaction control broke


when I ran my BTEQ script continued
Solution
Set Retry OFF
Default is ON, so must be explicitly set to OFF
Set to OFF for
BT/ET
Volatile and Global Temp tables
After a failure, manually restart the script

Implicit Transaction
Either option is acceptable
Default is ON

Dell DDW / Teradata Project Documentation Dell Confidential 16


Query Performance

OOPS my query is performing poorly


Tips on what to look for
Using GROUP BY instead of DISTINCT?
Are the primary indexes unique or strongly unique?
Are the joins PK to FK?
Are statistics collected on all indexes, and join and
constraint columns?
Do the join columns have the same data type?
Are there any calculated columns used in joins?
Is aggregation pushed as deep as possible?
Is there SELECT * in subqueries?
Did you review the Explain plan?

Dell DDW / Teradata Project Documentation Dell Confidential 17


Query Performance

DISTINCT vs. GROUP BY


STRONGLY RECOMMEND GROUP BY
Distinct Usage
Syntax
SELECT DISTINCT MFG_ITEM_NUM FROM
FINANCE.ORDER_DETAIL
Takes 2 minutes 33 seconds
Reason
Data is redistributed to all Amps then sorted, eliminating
duplicates

GROUP BY Usage
Syntax
SELECT MFG_ITEM_NUM FROM FINANCE.ORDER_DETAIL
GROUP BY MFG_ITEM_NUM
Takes 10 seconds
Reason
First level of aggregation occurs at the AMP level. The aggregated
data is then redistributed for the final level of aggregation.
Dell DDW / Teradata Project Documentation Dell Confidential 18

Das könnte Ihnen auch gefallen