title: Why does my Query in Sybase database take too much time?
body: <p>The execution time of my query takes about 27 minutes. I really don't know why it takes so long. Especially I have an other query with the same output, but this one takes only 1,4 Seconds. 
  I want to know what the mean difference between both queries are and why the first one takes so long to execute.</p>
  
  <p>First one, which takes about 27 minutes:</p>
  
  <pre><code>select ATTACHMENT_ID from PES_ESB_ATTACHMENT where ATTACHMENT_ID in(                                                                        
              select distinct ATTACHMENT_ID from PES_ESB_ATTACHMENT A 
                  left outer join PES_ESB_SHIFTDOCUMENTATION SD on A.MOMENT=SD.ATTACHMENT_MOMENT 
                  left outer join PES_ESB_SHIFTTASK T on A.MOMENT=T.ATTACHMENT_MOMENT
                  where 
                                                                             SD.ATTACHMENT_MOMENT is null
                                                                            and 
                                                                             T.ATTACHMENT_MOMENT is null
                                                                            And ( 163697831 - A.MOMENT ) &gt; 86400 
              )
  </code></pre>
  
  <p>Second one with 1,4 seconds :</p>
  
  <pre><code>select ATTACHMENT_ID from PES_ESB_ATTACHMENT where ATTACHMENT_ID in(                                                                        
              select ATTACHMENT_ID from (
                             select distinct ATTACHMENT_ID, A.MOMENT
                                             from PES_ESB_ATTACHMENT A 
                                 left outer join PES_ESB_SHIFTDOCUMENTATION SD on A.MOMENT=SD.ATTACHMENT_MOMENT 
                                 left outer join PES_ESB_SHIFTTASK T on A.MOMENT=T.ATTACHMENT_MOMENT
                                 where 
                                                                                             SD.ATTACHMENT_MOMENT is null
                                                                                            and 
                                                                                             T.ATTACHMENT_MOMENT is null
                             )tbl1
              )
              and 
                             ( 163697831 - MOMENT ) &gt; 86400
  </code></pre>
  
  <p>How there can be such a big gap ?</p>