title: SQL Server index for cursors
body: <p>I have an application that sometimes runs slow and I think it is related to database cursors. </p>
  
  <p>Don't have any access to the application source so I can't control the database calls but the database is open so I can add index where needed.</p>
  
  <p>Problem is that I don't know really know how to speed up cursors with index.</p>
  
  <p>The cursor queries are simple and look like</p>
  
  <pre><code>SELECT * FROM table WHERE field1=1 AND field2=2 ORDER BY field3, field4
  </code></pre>
  
  <p>(Table contains about a 1M rows. Sometimes there is a single left join as well)</p>
  
  <p>If I run the query directly in SSMS it takes less than a second but when it is run from the application in a cursor it can take 30 seconds to fetch the first row (verified with sql-trace).</p>
  
  <p>The fields in WHERE and ORDER BY clauses are all indexed separately.</p>
  
  <p>I guess a combined index on field1,field2,field3,field4 would make it faster. Is there a way to speed it up without creating an index for each combination and order of fields?</p>
  
  <p>(Just to repeat: I have no influence on how the application accesses the database. Performance can only be tuned via index)</p>