Solved

Order of WHERE statements in Sybase SQL Server

Posted on 2000-02-16
6
209 Views
Last Modified: 2010-04-15
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
Comment
Question by:betsywr
6 Comments
 

Author Comment

by:betsywr
ID: 2527383
Whoops...I didn't mean to enter this under the C topic.  Sorry for any confusion!
0
 
LVL 4

Expert Comment

by:abancroft
ID: 2527417
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
 
LVL 1

Expert Comment

by:aperdon
ID: 2528431
Reording can optimise your query.
The best result you get when using sub-queries.
0
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 

Author Comment

by:betsywr
ID: 2528823
How can I find out the order that the optimiser is using?
0
 

Author Comment

by:betsywr
ID: 2528889
How can I find out the order that the optimiser is using?
0
 
LVL 3

Accepted Solution

by:
knel1234 earned 70 total points
ID: 2530036
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

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How do I test for current date? 9 109
Socket Programming (Unix) 8 142
How to creat good software interface ? 1 69
Windows 2012 R2 -- create 3.5TB GPT from unallocated space ? 1 56
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question