SQL Scripts For Detecting Skew
Notice that several different measures of skew can be made.
Each type of skew can have a different effect on the query plan the Optimizer chooses, as does the concentration of the relevant rows within the data blocks.
If you are analyzing the demographics of an existing table, whether production or prototype, you can use the following set of useful scripts written by the Teradata technical support team to check for data skew. You can adjust the details of these statement to suit the needs of your site.
This is a good practice to undertake when new applications are being loaded on the system. It is also good practice to run these queries regularly if there are many data changes.
The following query identifies tables that are not evenly distributed. Ideally, variance should be less than 5%. In this query, variance is set to 1000%, which generally indicates that some or many AMPs have no rows from the table in question. You can use the BTEQ command RETLIMIT to limit the number of rows returned.
SELECT (MAX(CurrentPerm) - MIN(CurrentPerm)) * 100
(FORMAT ‘zzzzz9.99%’),MAX(CurrentPerm)(TITLE ‘Max’)
(TITLE ‘Min’)(FORMAT ‘zzz,zzz,zzz,999’),
TRIM(DatabaseName)||’.’||TableName (NAMED Tables)
GROUP BY DatabaseName, TableName
HAVING SUM(CurrentPerm) > 1000000
AND variance > 1000
WHERE DatabaseName NOT IN(‘CrashDumps’,’DBC’)
ORDER BY Tables;
Use the following query to display the detailed distribution of a table that has been identified as having a skewed distribution:
SELECT vproc, CurrentPerm
WHERE DatabaseName = ‘<databasename>’
AND TableName = ‘<tablename>’
ORDER BY 1;
The following query reports the row distribution of a table by AMP:
SELECT dt1.a (TITLE ’AMP’), dt1.b (TITLE ’Rows’),
((dt1.b/dt2.x (FLOAT)) - 1.0)*100 (FORMAT’+++9%’,
FROM (SELECT HASHAMP(HASHBUCKET(HASHROW(<index>))),COUNT(*)
GROUP BY 1) AS dt1 (a,b),
(SELECT (COUNT(*) / (HASHAMP()+1)(FLOAT))
FROM <databasename>.<tablename>) AS dt2(x)
ORDER BY 2 DESC,1;
The following query reports the distribution by AMP of the specified index or column.
SELECT HASHAMP(HASHBUCKET(HASHROW(<index or column>))) ,COUNT(*)
GROUP BY 1
ORDER BY 2 DESC;
The following query reports the number of row hash collisions for the specified index or column.
SELECT HASHROW(index or column), COUNT(*)
GROUP BY 1
ORDER BY 1
HAVING COUNT(*) > 10;
The following query reports the number of AMPs and the number of rows a given query accesses.
LOCKING TABLE <tablename> FOR ACCESS
SELECT COUNT(dt.ampNum)(TITLE ‘#AMPS’),
FROM (SELECT HASHAMP(HASHBUCKET(HASHROW(<index>))), COUNT(*)
WHERE <selection criteria>
GROUP BY 1)AS dt (ampNum, numRows);