create a select query with all columns of below teradata table with filter on partition…
- User
create a select query with all columns of below teradata table with filter on partition ranges CREATE MULTISET TABLE RETAIL_DB.NewYorkTaxifhv_final ,FALLBACK , NO BEFORE JOURNAL, NO AFTER JOURNAL, CHECKSUM = DEFAULT, DEFAULT MERGEBLOCKRATIO, MAP = TD_MAP1 ( dispatching_base_num VARCHAR(20) CHARACTER SET LATIN NOT CASESPECIFIC, pickup_datetime TIMESTAMP(0), dropOff_datetime TIMESTAMP(0), PUlocationID DECIMAL(18,10), DOlocationID DECIMAL(18,10), SR_Flag VARCHAR(20) CHARACTER SET LATIN CASESPECIFIC, Affiliated_base_number VARCHAR(20) CHARACTER SET LATIN CASESPECIFIC) PRIMARY INDEX ( dispatching_base_num ,pickup_datetime ,dropOff_datetime ) PARTITION BY ( RANGE_N(pickup_datetime BETWEEN TIMESTAMP '2015-01-01 00:00:00+00:00' AND TIMESTAMP '2023-12-31 23:59:59+00:00' EACH INTERVAL '1' HOUR , UNKNOWN),RANGE_N(dropoff_datetime BETWEEN TIMESTAMP '1989-01-01 00:00:00+00:00' AND TIMESTAMP '1990-12-31 00:00:00+00:00' EACH INTERVAL '1' DAY , NO RANGE, UNKNOWN) );
- Assistant
When working with Teradata and filtering results based on partitioned columns, you can significantly optimize your query's performance by making use of partition elimination. In the case of the
NewYorkTaxifhv_finaltable, it appears to be partitioned by bothpickup_datetimeanddropOff_datetimeacross specified ranges.If, for example, you wanted to query all records with a
pickup_datetimein January 2020, you could write the select query optimized for partition ranges like this: - User
Write the select statement using below teradata table to leverage partition pruning CREATE SET TABLE CDMTDFMGR.sample_WA_sales_csv_multipartition_order ,FALLBACK , NO BEFORE JOURNAL, NO AFTER JOURNAL, CHECKSUM = DEFAULT, DEFAULT MERGEBLOCKRATIO, MAP = TD_MAP1 ( product_id INTEGER, Seller_Country VARCHAR(14) CHARACTER SET LATIN NOT CASESPECIFIC, Channel_Type VARCHAR(11) CHARACTER SET LATIN NOT CASESPECIFIC, Store_Type VARCHAR(22) CHARACTER SET LATIN NOT CASESPECIFIC, Product_Category VARCHAR(24) CHARACTER SET LATIN NOT CASESPECIFIC, Product_Line VARCHAR(20) CHARACTER SET LATIN NOT CASESPECIFIC, Product VARCHAR(33) CHARACTER SET LATIN NOT CASESPECIFIC, Years INTEGER, Quarter VARCHAR(7) CHARACTER SET LATIN NOT CASESPECIFIC, Sales_Value FLOAT, Units_Sold INTEGER, Margin FLOAT) PRIMARY INDEX ( product_id ) PARTITION BY CASE_N( Years > 2013 , Years > 2012 , Years > 2011 , NO CASE, UNKNOWN);
- Assistant
To leverage partition pruning on the table, you'd focus on the column, as that's how the table is partitioned according to the statement provided. Partition pruning helps reduce the amount of data scanned by limiting the query to specific partitions that meet the criteria.
- User
Check the below explain plan of Teradata and confirm if partition pruning is happening or not Explanation
- First, we lock CDMTDFMGR.sample_WA_sales_csv_multipartition_order in TD_MAP1 for read on a reserved RowHash in a single partition to prevent global deadlock.
- Next, we lock CDMTDFMGR.sample_WA_sales_csv_multipartition_order
in TD_MAP1 for read on a single partition. - We do an all-AMPs RETRIEVE step in TD_MAP1 from a single partition
of CDMTDFMGR.sample_WA_sales_csv_multipartition_order with a
condition of (
"CDMTDFMGR.sample_WA_sales_csv_multipartition_order.Years = 2013") with a residual condition of (
"CDMTDFMGR.sample_WA_sales_csv_multipartition_order.Years = 2013") into Spool 1 (group_amps), which is built locally on the AMPs.
The size of Spool 1 is estimated with no confidence to be 8 rows ( 1,184 bytes). The estimated time for this step is 0.01 seconds.
-> The contents of Spool 1 are sent back to the user as the result of statement 1. The total estimated time is 0.01 seconds.
- Assistant
Based on the provided explain plan from Teradata, it is clear that partition pruning is indeed happening. This can be inferred from several key statements within the explain plan:
-
"... for read on a single partition." - This part of the explanation specifies that the query operation is being performed on a single partition of the table
CDMTDFMGR.sample_WA_sales_csv_multipartition_order. This is a direct indication that partition pruning is occurring because the database engine is targeting a specific partition rather than scanning the entire table. -
"We do an all-AMPs RETRIEVE step ... from a single partition of CDMTDFMGR.sample_WA_sales_csv_multipartition_order with a condition of ("CDMTDFMGR.sample_WA_sales_csv_multipartition_order.Years = 2013")" - This statement provides further evidence of partition pruning. It mentions that the RETRIEVE operation is from a "single partition" based on the condition that the column equals 2013. Since the operation is limited to a single partition that matches the query's condition, it is an explicit confirmation that the DBMS has effectively eliminated other partitions from consideration, thereby pruning them out and optimizing the query's execution.
-
- User
use below teradata ddl and create a select query to get latest year data CREATE SET TABLE CDMTDFMGR.sample_WA_sales_csv_multipartition_order ,FALLBACK , NO BEFORE JOURNAL, NO AFTER JOURNAL, CHECKSUM = DEFAULT, DEFAULT MERGEBLOCKRATIO, MAP = TD_MAP1 ( product_id INTEGER, Seller_Country VARCHAR(14) CHARACTER SET LATIN NOT CASESPECIFIC, Channel_Type VARCHAR(11) CHARACTER SET LATIN NOT CASESPECIFIC, Store_Type VARCHAR(22) CHARACTER SET LATIN NOT CASESPECIFIC, Product_Category VARCHAR(24) CHARACTER SET LATIN NOT CASESPECIFIC, Product_Line VARCHAR(20) CHARACTER SET LATIN NOT CASESPECIFIC, Product VARCHAR(33) CHARACTER SET LATIN NOT CASESPECIFIC, Years INTEGER, Quarter VARCHAR(7) CHARACTER SET LATIN NOT CASESPECIFIC, Sales_Value FLOAT, Units_Sold INTEGER, Margin FLOAT) PRIMARY INDEX ( product_id ) PARTITION BY CASE_N( Years > 2013 , Years > 2012 , Years > 2011 , NO CASE, UNKNOWN);
- Assistant
To create a SELECT query to retrieve the latest year's data from the
CDMTDFMGR.sample_WA_sales_csv_multipartition_ordertable, you need to know what the latest year in the dataset is. Assuming you do not already have this information, you first need to find the maximumYearsvalue.Here's how to first find the latest year:
SELECT MAX(Years) AS LatestYear FROM CDMTDFMGR.sample_WA_sales_csv_multipartition_order;Once you have the latest year (let's assume that the latest year in the dataset turns out to be 2023 for the sake of this example), you can then construct a query to select all data for that year.
Here's the query to retrieve all data for the latest year (assuming the latest year is 2023):
SELECT * FROM CDMTDFMGR.sample_WA_sales_csv_multipartition_order WHERE Years = 2023;