• C

Order of WHERE statements in Sybase SQL Server

I'm working on tuning some SQL in a Sybase DB and have found a number of queries which don't seem to be utilizing the indexes efficiently on the DB tables.  It seems to me that in a join statement, the most logical order for the WHERE statments would be an order which first checks fields that are indexed on each table.  For example:

SELECT a.column1,
       b.column2
FROM   table1 a,
       table2 b
WHERE  a.indexed_column1 = :hstvariable1
AND    a.indexed_column2 = :hstvariable2
AND    b.indexed_column1 = a.indexed_column1
AND    b.unindexed_column1 = :hstvariable3

(I hope that this is making sense.)  In other words, can reordering the WHERE conditions help performance by eliminating some reads on the tables?
betsywrAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
knel1234Connect With a Mentor Commented:
betswer,

The optimizer in Sybase chooses the order of tables and the indexes to be used.  The best way to see what it happening is to do a "set showplan on" and then "set noexec on" and then run your query(the query does not actually run as per the noexec).  This will show you exactly what indexes are being used.  Sybase decides what it thinks is best and a lot goes into this decision.  This includes cardinality of the data (figure roughly 12.5% of the table... so if you use an index on a table that has 100 rows and you would get 15 rows back then the "optimizer" decides that a table scan is required.  In addition, if this is a stored procedure you need to monitor (or have a DBA monitor) the table's "statistics" and ensure that the query tree and query plan for the SP are up to date.  Furthermore, you can "force" indexes, buffer pools, and such, but this depends on the versions of SQL server.  Please do a select @@version so that more info can be provided.
knel
0
 
betsywrAuthor Commented:
Whoops...I didn't mean to enter this under the C topic.  Sorry for any confusion!
0
 
abancroftCommented:
In a simple query like this, I would think that the query optimiser would already be taking this information into account.

When it comes to designing a RDBMS engine, the SQL parser/optimiser is given a lot of attention. If you think about it, one of the primary functions of the engine is to process SQL.
0
Simplify Active Directory Administration

Administration of Active Directory does not have to be hard.  Too often what should be a simple task is made more difficult than it needs to be.The solution?  Hyena from SystemTools Software.  With ease-of-use as well as powerful importing and bulk updating capabilities.

 
aperdonCommented:
Reording can optimise your query.
The best result you get when using sub-queries.
0
 
betsywrAuthor Commented:
How can I find out the order that the optimiser is using?
0
 
betsywrAuthor Commented:
How can I find out the order that the optimiser is using?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.