Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Order of WHERE statements in Sybase SQL Server

Posted on 2000-02-16
6
Medium Priority
?
232 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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 280 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

721 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