title: Joining more table is reducing execution time
body: <p>I was under the impression that the more number of tables you join in your query the more its execution time increases, but something really opposite is happening in my case.</p>
  
  <p>Here is the query where I am joining two tables <b>task_group_attempt</b> and <b>task_group</b> -</p>
  
  <pre><code>select count(*) from (
  select ga.id, ga.task_group_id from task_group_attempt ga 
  where ga.started_on &gt;= '2015-05-30 18:30:00' and ga.started_on &lt; '2015-06-30 18:30:00'
  ) tab1 
  inner join task_group tg on tg.id = tab1.task_group_id and tg.task_group_type_id != 6
  </code></pre>
  
  <p>The value it is returning is <b><i>585856</i></b> and the execution time is <b><i>17.6 secs</i></b>.</p>
  
  <p>For the table <code>task_group_attempt</code> <b>id</b> is the primary key and indexing has been done to the column <b>task_group_id</b>.</p>
  
  <p>For the table <code>task_group</code> <b>id</b> is the primary key and indexing has been done to the column <b>task_group_type_id</b>.</p>
  
  <p>Now the second query which is just the extension of the first one and here we are joining one more table named <b>task_attempt</b> -</p>
  
  <pre><code>select count(*) from (
  select ga.id, ga.task_group_id from task_group_attempt ga 
  where ga.started_on &gt;= '2015-05-30 18:30:00' and ga.started_on &lt; '2015-06-30 18:30:00'
  ) tab1 
  inner join task_group tg on tg.id = tab1.task_group_id and tg.task_group_type_id != 6
  inner join task_attempt ta on tab1.id = ta.task_group_attempt_id
  </code></pre>
  
  <p>The value it is returning is <b><i>1109682</i></b> and the execution time is <b><i>7.88 secs</i></b>.</p>
  
  <p>For the table <code>task_attempt</code> indexing has been done to the column <b>task_group_attempt_id</b>.</p>
  
  <p>I have checked it multiple times and every time the query execution time is almost or close to the above mentioned time.</p>
  
  <p><b>ADDITION INFO -</B></p>
  
  <p>Number of rows in <code>task_group_attempt</code> is 8565011 <br />
  Number of rows in <code>task_group</code> is 1057 <br />
  Number of rows in <code>task_attempt</code> is 15156598</p>
  
  <p>It would be really helpful is somebody can help me understand how is this possible?</p>