title: How can I make MS-Access choose a different/the right execution plan for my query
body: <p>I have a problem with a relatively simple query and the execution plan Access choose for it.</p>
  
  <p>The query is of this form</p>
  
  <pre><code>SELECT somethings
  FROM A INNER JOIN (B INNER JOIN (C INNER JOIN D ON ...) ON ...) ON ...
  WHERE A.primaryKey= 1 AND D.d = 2;
  </code></pre>
  
  <p>C and D have relatively few rows. A and B have a few thousands rows.</p>
  
  <p>The query, which returns 2 rows (not sure if this is pertinent) is really slow. It runs in 17 seconds. If I remove the <code>AND D.d = 2</code> part of the where clause, the query now returns 4 rows and run instantly.</p>
  
  <p>So my understanding is that the JET engine could run the query without the filter on D.d instantly, then execute the said filter instantly (only 4 rows to filter). Therefor it should not be too much longer to run the query with the <code>D.d = 2</code> filter.</p>
  
  <p>I tried to create a sub query without the filter and include this in another query that would just filter the result, but it's still slow. My guess is that the JET engine is smart enough to "flatten" the sub-queries so the result is the same.</p>
  
  <p>Since I was unable to make the query run as I wished I used the JETSHOWPLAN thingy so that Access would output it's execution plan. Here is what I found:</p>
  
  <p>For the fast query (the one without <code>D.d = 2</code>) the first step of the query plan is to apply the <code>A.primaryKey = 1</code> filter on the A table. This result in a data set of 1 row out of more than 30000. Then the joins seems to be executed from A to D using index with a data set that never goes over 4 rows.</p>
  
  <p>The slow query seems to be executed in the revers order. D and C are joined first then <code>D.d = 2</code> is tested. After that, the joins from C to A are executed. By doing this this way the data that needs to be joined from D to C, from C to B and from B to A is much larger. When all the JOIN are executed and before <code>A.primaryKey=1</code> is executed the data set will have 120K rows.</p>
  
  <p>Is there a way I could force the right query plan on Access?</p>
  
  <p>I hope I was clear. Let me know if I should post the query plans. I did not because they are quite large.</p>
  
  <p>Thanks in advance,</p>
  
  <p>mp</p>