Does using an order by clause affect the index that will be used?
If so, can I get poiible improved performance by always using these when a
known index is available?
TIA
JeffP....
Does using an order by clause affect the index that will be used?
If so, can I get poiible improved performance by always using these when a
known index is available?
TIA
JeffP....
The index may be used but it depends on the sql statement and the table DDL.
Do you have a real example?
--
Andrew J. Kelly
SQL Server MVP
Quote:> Does using an order by clause affect the index that will be used?
> If so, can I get poiible improved performance by always using these when a
> known index is available?
> TIA
> JeffP....
Perhaps to just expand a bit on Andrew's reply ...
The SQL Server query optimizer uses a number of sophisticated algorithms to
try to determine an optimal query plan. Many factors influence which
indexes, if any, the query optimizer will use. The mere existence of an
index for example does not in any guarantee that SQL Server will choose to
use that index. Similiarly the mere existence of an ORDER BY clause does not
guarantee that SQL Server will use a particular index, although the
existence of an ORDER BY clause can definitely affect the choice of indexes.
So, as Andrew posted, the general answer to your question is, "Yes ... an
ORDER BY clause **can** affect the index that will be used, but having an
ORDER BY clause does not guarantee that a particular index will be used."
-------------------------------------------
BP Margolin
Please reply only to the newsgroups.
When posting, inclusion of SQL (CREATE TABLE ..., INSERT ..., etc.) which
can be cut and pasted into Query Analyzer is appreciated.
Quote:> Does using an order by clause affect the index that will be used?
> If so, can I get poiible improved performance by always using these when a
> known index is available?
> TIA
> JeffP....
I hope I wouldn't be wrong in saying that whenever possible I can choose my
order by as the column that I have an index on [comma column the index is
with, ...n]. If performance improves, that's good.
I was also conerned that the order by would impede performance by trying to
satisfy the order by in favor of a better index; what I heard was that MSSQL
will use the "best" plan to retrieve the data regardless of the order by.
JeffP....
> Perhaps to just expand a bit on Andrew's reply ...
> The SQL Server query optimizer uses a number of sophisticated algorithms
to
> try to determine an optimal query plan. Many factors influence which
> indexes, if any, the query optimizer will use. The mere existence of an
> index for example does not in any guarantee that SQL Server will choose to
> use that index. Similiarly the mere existence of an ORDER BY clause does
not
> guarantee that SQL Server will use a particular index, although the
> existence of an ORDER BY clause can definitely affect the choice of
indexes.
> So, as Andrew posted, the general answer to your question is, "Yes ... an
> ORDER BY clause **can** affect the index that will be used, but having an
> ORDER BY clause does not guarantee that a particular index will be used."
> -------------------------------------------
> BP Margolin
> Please reply only to the newsgroups.
> When posting, inclusion of SQL (CREATE TABLE ..., INSERT ..., etc.) which
> can be cut and pasted into Query Analyzer is appreciated.
> > Does using an order by clause affect the index that will be used?
> > If so, can I get poiible improved performance by always using these when
a
> > known index is available?
> > TIA
> > JeffP....
You choose your ORDER BY based on how you need the data sorted in your
returning result set, not on how the table is structured. If you don't need
it sorted then don't use an ORDER BY.
--
Andrew J. Kelly
SQL Server MVP
> I hope I wouldn't be wrong in saying that whenever possible I can choose
my
> order by as the column that I have an index on [comma column the index is
> with, ...n]. If performance improves, that's good.
> I was also conerned that the order by would impede performance by trying
to
> satisfy the order by in favor of a better index; what I heard was that
MSSQL
> will use the "best" plan to retrieve the data regardless of the order by.
> JeffP....
> > JeffP,
> > Perhaps to just expand a bit on Andrew's reply ...
> > The SQL Server query optimizer uses a number of sophisticated algorithms
> to
> > try to determine an optimal query plan. Many factors influence which
> > indexes, if any, the query optimizer will use. The mere existence of an
> > index for example does not in any guarantee that SQL Server will choose
to
> > use that index. Similiarly the mere existence of an ORDER BY clause does
> not
> > guarantee that SQL Server will use a particular index, although the
> > existence of an ORDER BY clause can definitely affect the choice of
> indexes.
> > So, as Andrew posted, the general answer to your question is, "Yes ...
an
> > ORDER BY clause **can** affect the index that will be used, but having
an
> > ORDER BY clause does not guarantee that a particular index will be
used."
> > -------------------------------------------
> > BP Margolin
> > Please reply only to the newsgroups.
> > When posting, inclusion of SQL (CREATE TABLE ..., INSERT ..., etc.)
which
> > can be cut and pasted into Query Analyzer is appreciated.
> > > Does using an order by clause affect the index that will be used?
> > > If so, can I get poiible improved performance by always using these
when
> a
> > > known index is available?
> > > TIA
> > > JeffP....
--
Tibor Karaszi, SQL Server MVP
Archive at: http://groups.google.com/groups?oi=djq&as_ugroup=microsoft.public.sql...
> I hope I wouldn't be wrong in saying that whenever possible I can choose my
> order by as the column that I have an index on [comma column the index is
> with, ...n]. If performance improves, that's good.
> I was also conerned that the order by would impede performance by trying to
> satisfy the order by in favor of a better index; what I heard was that MSSQL
> will use the "best" plan to retrieve the data regardless of the order by.
> JeffP....
> > JeffP,
> > Perhaps to just expand a bit on Andrew's reply ...
> > The SQL Server query optimizer uses a number of sophisticated algorithms
> to
> > try to determine an optimal query plan. Many factors influence which
> > indexes, if any, the query optimizer will use. The mere existence of an
> > index for example does not in any guarantee that SQL Server will choose to
> > use that index. Similiarly the mere existence of an ORDER BY clause does
> not
> > guarantee that SQL Server will use a particular index, although the
> > existence of an ORDER BY clause can definitely affect the choice of
> indexes.
> > So, as Andrew posted, the general answer to your question is, "Yes ... an
> > ORDER BY clause **can** affect the index that will be used, but having an
> > ORDER BY clause does not guarantee that a particular index will be used."
> > -------------------------------------------
> > BP Margolin
> > Please reply only to the newsgroups.
> > When posting, inclusion of SQL (CREATE TABLE ..., INSERT ..., etc.) which
> > can be cut and pasted into Query Analyzer is appreciated.
> > > Does using an order by clause affect the index that will be used?
> > > If so, can I get poiible improved performance by always using these when
> a
> > > known index is available?
> > > TIA
> > > JeffP....
1. I am getting this message when i am tring to export or import anything using
I am getting this message when i am tring to export or import anything using
the DTS Import & Export wizard. Has anyone else encountered this and know
what the fix is?
3. Result set order depends on the index used?
4. Ingres Goes Slow After abnormal shudown.
5. Using index instead of order by
6. use trigger to write a text file
7. Using index with order desc
8. To SmartObject or not To Smartobject ?
9. alternate order drop downs using indexes?
10. Will Index be Used - ORDER BY MYFIELD DESC?
11. ANY IDEA ON USING INDEX FOR ORDER BY