[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 238
  • Last Modified:

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?
0
betsywr
Asked:
betsywr
1 Solution
 
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
 
aperdonCommented:
Reording can optimise your query.
The best result you get when using sub-queries.
0
Free tool for managing users' photos in Office 365

Easily upload multiple users’ photos to Office 365. Manage them with an intuitive GUI and use handy built-in cropping and resizing options. Link photos with users based on Azure AD attributes. Free tool!

 
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
 
knel1234Commented:
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

Featured Post

[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now