title: Performing "Optimized" set Operations on java.util.ArrayList or in SQL
body: <p>This is the first time I need to ask question to get my query solved before today previous threads were able to solve my queries.</p>
  
  <p><strong>Scenario:</strong>
  I have two tables(lets say A &amp; B) in a database with some same rows and some different rows. Point to note is that both tables have millions of rows or at least more than a million.</p>
  
  <p>Now I have to perform set operations on them like no. of same rows (intersection) , no. of new rows(A-B) and no. of old rows (B-A).</p>
  
  <p>I have two choices:
  1) I can perform set operations as query it-self on the database.</p>
  
  <p>2) Or I can fetch the records and perform operations on java.util.ArrayLIst in memory.</p>
  
  <p>But in both the cases its taking too long around 5 to 10 mins what could be the best(<strong>OPTIMIZED</strong>) approach for this.</p>
  
  <p>Please help.</p>