Tuesday, May 14, 2013

Sybase Query Optimizer


What is Sybase query optimizer?
The Sybase query optimizer is in charge of evaluating the available join orders and choosing the best one. You know that we can determine what method it used using showplan:

set showplan on

But did you also know that we can use certain DBA tracing parameters to see the various plans it evaluated, and the estimated costs of each one? Here’s how:

dbcc traceon(3604, 302, 310, 317)

The 3604 sends the output to the monitor, or to a specified file. That prevents it from filling up your error log (3605).

Trace flag 302 prints the optimizer’s index costing and selection phase but 310 and 317 are the trace commands that can tell you which join orders were considered and rejected, and what the costs were. Together they’ll show you ALL the plans, so be prepared for a lot of output. Otherwise use 310 by itself.

There is a way to force the optimizer to choose a certain plan. Specifically, the join order specified by the query itself, in the “from” clause.

set forceplan [on|off]

The advantage is that it saves you the time the optimizer would have spent choosing a join order. The bad news is, that time is negligible anyway.

The disadvantage is that there is usually a reason the optimizer chooses its plan, and it can vary from execution to execution depending on the data and the variables of the query. Instead of forcing a plan you think is best, you may want to consider why the optimizer is choosing the “wrong” plan. That’s where the aforementioned tracing can help.

As a quick fix, though, make sure your statistics and stored procedures are up to date! The optimizer depends on its accuracy for the best results.

update statistics table_name [index_name]
exec proc_name with recompile.

I also found a really good article from Sybase entitled “Analyzing and Resolving Optimizer Problems/Symptoms”:

http://www.sybase.com/detail?id=2602

Tune a Query or Stored Procedure in Sybase


Getting information about the tables and indexes
sp_help
sp_helpdb
sp_tables
Diagnosing the Problem:
Execute the query again, with some of the below options to get more information:
See the query plan Sybase ASE is using, look for table scans in the results
set showplan on
(optional) set noexec on (this negates statistics io)
Gather some statistics looks for I/O, which usually takes a lot of time.
set statistics io on
Gather time statistics so you can see where the time is spent
set statistics time on
If you think the problem is with an index, here is how to see they are chosen
dbcc traceon(302) and dbcc traceon(310)
Best way to determine size of a table
sp_spaceused
This tool will take samples studying all areas of database activity
sp_sysmon
Sybase ASE has an advanced tool for studying statistics
optdiag
You can get additional network information using sp_monitor, here are some examples
sp_monitor
@@pack_sent – Number of packets sent by Adaptive Server
@@pack_received – Number of packets received
@@packet_errors – Number of errors
Solving the Problem:
Often the solution is to create an index
create index [name] on [table] ([column])
Clean-up tables that have been updated frequently
rebuild reorg
The optimizer needs up-to-date statistics
update statistics [table]
After updating statistics, consider re-compiling the stored procedures
sp_recompile [table]