title: Pig Optimization on Group by
body: <p>Lets assume that i have a large data set as per below schema layout</p>
  
  <pre><code>id,name,city
  ---------------
  100,Ajay,Chennai
  101,John,Bangalore
  102,Zach,Chennai
  103,Deep,Bangalore
  ....
  ...
  </code></pre>
  
  <p>I have two style of pig code giving me the same output.</p>
  
  <p>Style 1 :</p>
  
  <pre><code>records = load 'user/inputfiles/records.txt' Using PigStorage(',') as (id:int,name:chararray,city:chararray);
  records_grp = group records by city;
  records_each = foreach records_grp generate group as city,COUNT(records.id) as emp_cnt;
  dump records_each;
  </code></pre>
  
  <p>Style 2 :</p>
  
  <pre><code>records = load 'user/inputfiles/records.txt' Using PigStorage(',') as (id:int,name:chararray,city:chararray);
  records_each = foreach (group records by city) generate group as city,COUNT(records.id) as emp_cnt;
  dump records_each ;
  </code></pre>
  
  <p>In second style i used a nested Foreach. Does it style 2 code run faster than style 1 code or not.</p>
  
  <p>I Would like to reduce the total time taken to complete that pig job..</p>
  
  <p>So the Style 2 code achieve that ?  Or there is no impact in total time taken?</p>
  
  <p>If somebody confirms me then i can run similar code in my cluster with very large dataset </p>