Remove Redundant Projections

To improve memory usage and performance costs, remove unnecessary projections from your queries and load only the data required. If you have to perform an operation, include pre-projection statements as needed.

Here’s an example of a query with an unnecessary projection.

1q = load "Superstore";
2q = foreach q generate 'Category';
3q = group q by 'Category';
4q = foreach q generate 'Category', count() as 'count';

The first foreach statement projects the Category field, which is already included in the dataset. Since we’re not performing any operation on the field, we can remove it.

1q = load "Superstore";
2q = group q by 'Category';
3q = foreach q generate 'Category', count() as 'count';

Here’s an example with an implicit cogroup.

1a = load "Customer_Data";
2a = foreach a generate 'Customer_Name';
3b = load "Superstore";
4b = foreach b generate 'Customer_Name';
5a = group a by 'Customer_Name' full, b by 'Customer_Name';
6a = foreach a generate coalesce(a.'Customer_Name', b.'Customer_Name') as 'Customer_Name', count('a') as 'Superstore', count('b') as 'Customer_data';

In this example, the foreach statements that follow loading the “Customer_Data” and the “Superstore” datasets are unnecessary, since they’re projecting the Customer_Name fields without any additional action. You can group the fields pre-projection.

1a = load “Customer_Data”;
2b = load “Superstore”;
3a = group a by ‘Customer_Name’ full, b by 'Customer_Name';
4a = foreach a generate coalesce(a.'Customer_Name', b.'Customer_Name') as 'Customer_Name', count('a') as 'Superstore', count('b') as 'Customer_data';