Solved

Order of WHERE statements in Sybase SQL Server

Posted on 2000-02-16
6
177 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

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…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

707 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now