Improve Performance with Teradata Partition Elimination: The Stored Procedure Approach

Teradata Partition Elimination – the Stored Procedure Approach

Occasionally, we may want to select rows in a table’s character column that begin with a specific prefix, yet we lack prior knowledge of all the values contained in that column. This could include newly introduced codes in the future.

If we are lucky, there might be a convention for these codes, such as: “All code values have to start with an ‘A’ character”.

With SQL, this problem can be resolved using the following approach:

SELECT * FROM The_Table WHERE CODE_COLUMN LIKE ‘A%’;

or

SELECT * FROM The_Table WHERE SUBSTR(CODE_COLUMN,1,1) = ‘A’;

While the SQL statement above resolves our issue, accessing a table with numerous rows could potentially result in performance problems.

Even if the column CODE_COLUMN were to partition the table, we might end up with a full table scan (FTS), as partition elimination cannot be applied if a LIKE or SUBSTR function is defined on the partition column(s):

CREATE TABLE The_Table
(
PK INTEGER NOT NULL,
Code_Column CHAR(100)
) PRIMARY INDEX (PK)
PARTITION BY (Code_Column);

Fortunately, a Teradata Stored Procedure can resolve this issue.

  1. All distinct codes are extracted from the table and written into a variable.
  2. The list of code values is dynamically inserted into a SQL statement and executed:

REPLACE PROCEDURE MY_SP()
BEGIN
DECLARE CODE_LIST VARCHAR(3200);
SET MySQL = ‘CREATE VOLATILE MULTISET TABLE MY_CODES AS
(
SELECT CODE_COLUMN FROM THE_TABLE WHERE CODE_COLUMN LIKE ”A%” GROUP BY 1
) WITH DATA PRIMARY INDEX (CODE_COLUMN) ON COMMIT PRESERVE ROWS;’;

CALL DBC.SysExecSQL(MySQL);
SET CODE_LIST=”’Ax”’;
FOR TheRow AS CODES_CURSOR CURSOR
FOR
  SELECT
  CODE_COLUMN,RANK(CODE_COLUMN) AS RNK
  FROM MY_CODES
  DO
  IF TheRow. RNK = 1
  THEN
    SET CODE_LIST = ”” || TheRow. CODE_COLUMN || ””;
  ELSE
    SET CODE_LIST = CODE_LIST || ‘,”’ || TheRow. CODE_COLUMN || ””;
  END IF;
END FOR;

SET MySQL =’

INSERT INTO TARGET_TABLE
SELECT PK,COUNT(*)
FROM THE_TABLE
WHERE
CODE_COLUMN IN (”’ || CODE_LIST || ”’)
GROUP BY 1
;’

CALL DBC.SysExecSQL(MySQL);
END;

Related Services

⚡ Need Help Optimizing Your Data Platform?

We cut data platform costs by 30–60% without hardware changes. 25+ years of hands-on tuning experience.

Explore Our Services →

📋 Considering a Move From Teradata?

Get a personalized migration roadmap in 2 minutes. We have migrated billions of rows from Teradata to Snowflake, Databricks, and more.

Free Migration Assessment →

📊 Data Platform Migration Survey

Help us map where the industry is heading. Results are public — see what others chose.

1. What is your current data platform?

2. Where are you migrating to (or evaluating)?

Migrating FROM
Migrating TO

Thanks for voting! Share this with your network.

Follow me on LinkedIn for daily insights on data warehousing and platform migrations.

Stay Ahead in Data Warehousing

Get expert insights on Teradata, Snowflake, BigQuery, Databricks, Microsoft Fabric, and modern data architecture — delivered to your inbox.

Leave a Comment

DWHPro

Expert network for enterprise data platforms. Senior consultants, project teams built for your challenge — across Teradata, Snowflake, Databricks, and more.

📍Vienna, Austria & Jacksonville, Florida

Quick Links
Services Team Teradata Book Blog Contact Us
Connect
LinkedIn → [email protected]
Newsletter

Join 4,000+ data professionals.
Weekly insights on Teradata, Snowflake & data architecture.