title: SQL Query Optimization on joining 2 views
body: <p>I'm trying to join 2 views which have around 800k records. When I try to run the below query it really takes long time(more than 20 mins) to run..</p>
  
  <p>So there are 2 views..</p>
  
  <p>View A:  </p>
  
  <pre><code> colA  colB  colC
  =====================  
  valueA valueB valueC 
  </code></pre>
  
  <p>View B:  </p>
  
  <pre><code>colD  colE  colA
  =====================  
  valueD valueE valueA  
  </code></pre>
  
  <p>need to join these 2 views on colA. SO the query I have used is:</p>
  
  <pre><code>select  
  count(*)  
  from  
     (  
       select   colA,  colB,  colC  
          from viewA a
       inner join   
          viewB b on a.colA=b.colA and a.colA in (select distinct colA from viewA) 
          and
           b.colA in (select distinct colA from viewB
     );
  </code></pre>
  
  <p>I need to get the count of the rows and as there are duplicates in the views I have used 'distinct' keyword.<br>
  Can you plz crct me and tell me why the query is taking so long time? </p>