views:

34

answers:

1

Say I have a table X with 100 records in it and that running a select * from X takes 100 seconds.

How long should I expect the query select top 10 * from X to take?

I'd expect that the relationship is more or less linear so 10 seconds. Is this correct, or is the relationship non-linear in some way?

A: 

Your performance cost is in two different areas:

  • How long it takes to perform the query
  • How long it takes to return the results

Often, a query will be fast, but returning results will be slow, since it's I/O-bound. If this is the case, then you will see an approximately linear speedup by returning fewer results.

However, if the query itself is complicated, things are different. If it's not just select * from X, but select * from X where [complicated-expression], then results may vary widely between database implementations. In that case, your performance might be dominated by query complexity, in which case you won't see as much benefit by merely returning fewer results.

John Feminella
And of course, you never want to use select * in any event. For performance resons, never return any columns except those you need and if you have a join there are always columns you don't need. For maintainibilty, never rely on select * as it may cause things to break when database changes are made. For instance suppose you used select * in a UNION query and only one of the union branches had tables where an additonal column was added. Suppose you are displaying data to the user and a column was added that you didn't want the user to see?
HLGEM
@John fair enough, thanks. @HLGEM good tip.
StevenWilkins