views:

53

answers:

3

Say I have a query that returns 10,000 records. When the first record has returned what can I assume about the state of my query?

  1. Has it finished and is just returning records from the server to my instance of SSMS?
  2. Is the query itself still being executed on the server?
  3. What is it that causes the 10,000 records to be slowly returned for one query and nearly instantly for another?
A: 

You can not make a generic assumption, a query's plan is composed of a number of different types of operations, or iterators. Some of these are Navigational based, and work like a pipeline, whilst others are set based operations, such as a sort.

If any query contains a set based operation, it requires all the records before it could output the results (i.e an order by clause within your statement.) But if you have no set based iterators you could expect the rows to be streamed to you as they become available.

Andrew
Could you explain to me what "set based iterators" are? Are you talking about using Joins as opposed to using Cursors or loops? Lets assume that the query I'm talking about only uses joins.
Abe Miessler
Set based needs all the data to be able process, a sort is an obvious example, a navigational can work on a row at a time, such as a nested loop / index seek. These only affect whether you will see results streamed to you in the SSMS window for example, or they all arrive enmass.
Andrew
+1  A: 

There is potentially some mix of progressive processing on the server side, network transfer of the data, and rendering by the client.

If one query returns 10,000 rows quickly, and another one slowly -- and they are of similar row size, data types, etc., and are both destined for results to grid or results to text -- there is little we can do to analyze the differences unless you show us execution plans and/or client statistics for each one. These are options you can set in SSMS when running a query.

As an aside, switching between results to grid and results to text you might notice slightly different runtimes. This is because in one case Management Studio has to work harder to align the columns etc.

Aaron Bertrand
A: 

The answer to each of your individual questions is "it depends."

For example, consider if you include an order by clause, and there isn't an index for the column(s) you're ordering by. In this case, the server has to find all the records that satisfy your query, then sort them, before it can return the first record. This causes a long pause before you get your first record, but you (should normally) get them quite quickly once you start getting any.

Without the order by clause, the server will normally send each record as its found, so the first record will often show up sooner, but you may see a long pause between one record and the next.

As as far simply "why is one query faster than another", a lot depends on what indexes are available, and whether they can be used for a particular query. For example, something like some_column like '%something' will almost always be quite slow. The leading '%' means this won't be able to use an index, even if some_column has one. A search for something% instead of %something% might easily be 100 or 1000 times faster. If you really need the former, you really want to use full-text searching instead (create a full-text index, and use contains() instead of like.

Of course, a lot can also depend simply on whether the database has an index for a particular column (or group of columns). With a suitable index, the query will usually be quite a lot faster.

Jerry Coffin