title: How to optimize the SQL query to reduce the runtime?
body: <p>Below is a sample of the table structure I am using. The table almost contains 200 million records.</p>
  <p>The <code>query_1</code> returns the distinct combination of col_1, col_2 and col_3. Then it is iterated over these combinations to perform another query.</p>
  <p>Given that the table is huge, the run time for this execution is very high. I am looking to optimize this. Can someone assist me to optimize this?</p>
  <pre><code>query_1 = select distinct col_1, col_2, col_3 from my_table
  
  query_2 = select count (distinct col_4) from my_table mt
            where mt.col_1 = {} and mt.col_2 = {} and mt.col_3 = {} and mt.col_4 &gt; {}
  </code></pre>
  <pre><code>combinations = #output_of_query_1, this contains nearly 200K rows
  
  for val1, val2, val3 in combinations:
      # execute query_2
      # query_2 uses the the val1, val2, and val3 for to the my_table
  
  </code></pre>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>col_1</th>
  <th>col_2</th>
  <th>col_3</th>
  <th>col_4</th>
  <th>...</th>
  <th>col_n</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>a</td>
  <td>c</td>
  <td>e</td>
  <td>1</td>
  <td>...</td>
  <td>.....</td>
  </tr>
  <tr>
  <td>a</td>
  <td>c</td>
  <td>e</td>
  <td>2</td>
  <td>...</td>
  <td>.....</td>
  </tr>
  <tr>
  <td>a</td>
  <td>c</td>
  <td>f</td>
  <td>3</td>
  <td>...</td>
  <td>.....</td>
  </tr>
  <tr>
  <td>a</td>
  <td>d</td>
  <td>e</td>
  <td>1</td>
  <td>...</td>
  <td>.....</td>
  </tr>
  <tr>
  <td>a</td>
  <td>d</td>
  <td>e</td>
  <td>2</td>
  <td>...</td>
  <td>.....</td>
  </tr>
  <tr>
  <td>a</td>
  <td>d</td>
  <td>e</td>
  <td>3</td>
  <td>...</td>
  <td>.....</td>
  </tr>
  <tr>
  <td>a</td>
  <td>d</td>
  <td>e</td>
  <td>1</td>
  <td>...</td>
  <td>.....</td>
  </tr>
  <tr>
  <td>b</td>
  <td>c</td>
  <td>f</td>
  <td>1</td>
  <td>...</td>
  <td>.....</td>
  </tr>
  <tr>
  <td>b</td>
  <td>c</td>
  <td>e</td>
  <td>1</td>
  <td>...</td>
  <td>.....</td>
  </tr>
  <tr>
  <td>b</td>
  <td>c</td>
  <td>e</td>
  <td>2</td>
  <td>...</td>
  <td>.....</td>
  </tr>
  <tr>
  <td>b</td>
  <td>c</td>
  <td>e</td>
  <td>3</td>
  <td>...</td>
  <td>.....</td>
  </tr>
  </tbody>
  </table>
  </div>