<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
		<id>http://petersap.nl/SybaseWiki/index.php?action=history&amp;feed=atom&amp;title=Update_statistics_script</id>
		<title>Update statistics script - Revision history</title>
		<link rel="self" type="application/atom+xml" href="http://petersap.nl/SybaseWiki/index.php?action=history&amp;feed=atom&amp;title=Update_statistics_script"/>
		<link rel="alternate" type="text/html" href="http://petersap.nl/SybaseWiki/index.php?title=Update_statistics_script&amp;action=history"/>
		<updated>2026-04-04T21:44:09Z</updated>
		<subtitle>Revision history for this page on the wiki</subtitle>
		<generator>MediaWiki 1.24.2</generator>

	<entry>
		<id>http://petersap.nl/SybaseWiki/index.php?title=Update_statistics_script&amp;diff=1724&amp;oldid=prev</id>
		<title>Psap at 15:10, 24 August 2007</title>
		<link rel="alternate" type="text/html" href="http://petersap.nl/SybaseWiki/index.php?title=Update_statistics_script&amp;diff=1724&amp;oldid=prev"/>
				<updated>2007-08-24T15:10:37Z</updated>
		
		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;To make sure the ASE optimizer generates efficient query plans, you need to run update statistics regularly. Most DBA's run it as a weekly job.&lt;br /&gt;
Below is a sample of SQL code that does this job. It has the following features:&lt;br /&gt;
&lt;br /&gt;
* Will process all databases except tempdb and user defined temporary databases.&lt;br /&gt;
* Processes system and user tables.&lt;br /&gt;
* Skips proxy tables.&lt;br /&gt;
* Uses &amp;quot;update index statistics&amp;quot;.&lt;br /&gt;
* Will work on ASE 15, some very minor changes need to be made for older versions.&lt;br /&gt;
* Does an sp_recompile.&lt;br /&gt;
* Has a build in algorithm to calculate a sampling percentage.&lt;br /&gt;
* Does not use the datachange() function, since the value returned is not always reliable.&lt;br /&gt;
&lt;br /&gt;
Before you run the SQL code, you need to install a loopback server into the server. Do it like this:&lt;br /&gt;
 sp_addserver &amp;quot;loopback&amp;quot;,null,@@servername&lt;br /&gt;
&lt;br /&gt;
To start the update statistics task, just fire the following SQL code as a complete batch.&lt;br /&gt;
&lt;br /&gt;
 set nocount on&lt;br /&gt;
 go&lt;br /&gt;
 &lt;br /&gt;
 use tempdb&lt;br /&gt;
 go&lt;br /&gt;
 &lt;br /&gt;
 create proxy_table tempdb..pxy_sysobjects&lt;br /&gt;
        external table&lt;br /&gt;
        at &amp;quot;loopback.master.dbo.sysobjects&amp;quot;&lt;br /&gt;
 go&lt;br /&gt;
 &lt;br /&gt;
 create proc update_statistics @dbname varchar(30)&lt;br /&gt;
 as&lt;br /&gt;
 &lt;br /&gt;
 declare @user_name      varchar(30),&lt;br /&gt;
         @table_name     varchar(255),&lt;br /&gt;
         @rowcnt         bigint,&lt;br /&gt;
         @statement      varchar(1024),&lt;br /&gt;
         @min_sampling   tinyint,&lt;br /&gt;
         @work_sampling  int,&lt;br /&gt;
         @sampling       tinyint,&lt;br /&gt;
         @type           char(2)&lt;br /&gt;
 &lt;br /&gt;
 declare c1 cursor for&lt;br /&gt;
         select  user_name(uid),name,type,row_count(db_id(@dbname),id)&lt;br /&gt;
                 from    tempdb..pxy_sysobjects&lt;br /&gt;
                 where   type    in (&amp;quot;S&amp;quot;,&amp;quot;U&amp;quot;)            -- System, User tables&lt;br /&gt;
                 and not (sysstat2 &amp;amp; 1024 = 1024 or      -- Remote&lt;br /&gt;
                         sysstat2 &amp;amp; 2048 = 2048)         -- Proxy&lt;br /&gt;
                 and     id not in (8,14,32,34,38,39,42,43,46,47,50,53,57) -- Same fake system tables&lt;br /&gt;
 &lt;br /&gt;
 -- Set the mininum amount of sampling&lt;br /&gt;
 select  @min_sampling   = 2&lt;br /&gt;
 &lt;br /&gt;
 open c1&lt;br /&gt;
 &lt;br /&gt;
 fetch c1 into @user_name,@table_name,@type,@rowcnt&lt;br /&gt;
 &lt;br /&gt;
 while @@sqlstatus = 0&lt;br /&gt;
 begin&lt;br /&gt;
        select @work_sampling = round(100 - (sqrt(@rowcnt) / 10),0)&lt;br /&gt;
        if @work_sampling  &amp;lt; @min_sampling&lt;br /&gt;
           select @sampling = @min_sampling&lt;br /&gt;
        else&lt;br /&gt;
           select @sampling = @work_sampling&lt;br /&gt;
 &lt;br /&gt;
        print &amp;quot;%1!.%2!.%3! (%4! rows -&amp;gt; %5! %%)&amp;quot;,&lt;br /&gt;
              @dbname,@user_name,@table_name,@rowcnt,@sampling&lt;br /&gt;
 &lt;br /&gt;
        select @statement = &amp;quot;update index statistics &amp;quot; + @dbname + &amp;quot;.&amp;quot; + @user_name + &amp;quot;.&amp;quot; + @table_name&lt;br /&gt;
                          + &amp;quot; with sampling = &amp;quot; + convert(varchar(3),@sampling) + &amp;quot; percent&amp;quot;&lt;br /&gt;
 &lt;br /&gt;
        exec (@statement)&lt;br /&gt;
 &lt;br /&gt;
        if @type = &amp;quot;U&amp;quot;&lt;br /&gt;
        begin&lt;br /&gt;
          select @statement = @dbname + &amp;quot;..sp_recompile &amp;quot; + &amp;quot;'&amp;quot; + @user_name + &amp;quot;.&amp;quot; + @table_name + &amp;quot;'&amp;quot;&lt;br /&gt;
 &lt;br /&gt;
          exec (@statement)&lt;br /&gt;
        end&lt;br /&gt;
 &lt;br /&gt;
        fetch c1 into @user_name,@table_name,@type,@rowcnt&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 close  c1&lt;br /&gt;
 &lt;br /&gt;
 deallocate cursor c1&lt;br /&gt;
 go&lt;br /&gt;
 &lt;br /&gt;
 drop table tempdb..pxy_sysobjects&lt;br /&gt;
 go&lt;br /&gt;
 &lt;br /&gt;
 declare c1 cursor for&lt;br /&gt;
         select name&lt;br /&gt;
                from    master..sysdatabases&lt;br /&gt;
                where   name    != &amp;quot;tempdb&amp;quot;&lt;br /&gt;
                and not status3 &amp;amp; 256 = 256&lt;br /&gt;
 go&lt;br /&gt;
 &lt;br /&gt;
 declare @dbname    varchar(255),&lt;br /&gt;
         @statement varchar(255)&lt;br /&gt;
 &lt;br /&gt;
 open c1&lt;br /&gt;
 &lt;br /&gt;
 fetch c1 into @dbname&lt;br /&gt;
 &lt;br /&gt;
 while @@sqlstatus = 0&lt;br /&gt;
 begin&lt;br /&gt;
        select @statement = &amp;quot;create proxy_table tempdb..pxy_sysobjects&amp;quot;&lt;br /&gt;
                          + &amp;quot; external table &amp;quot; + &amp;quot; at 'loopback.&amp;quot; + @dbname + &amp;quot;.dbo.sysobjects'&amp;quot;&lt;br /&gt;
 &lt;br /&gt;
        exec (@statement)&lt;br /&gt;
 &lt;br /&gt;
        exec tempdb..update_statistics @dbname&lt;br /&gt;
 &lt;br /&gt;
        select @statement = &amp;quot;drop table tempdb..pxy_sysobjects&amp;quot;&lt;br /&gt;
 &lt;br /&gt;
        exec (@statement)&lt;br /&gt;
 &lt;br /&gt;
        fetch c1 into @dbname&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 close c1&lt;br /&gt;
 go&lt;br /&gt;
 &lt;br /&gt;
 deallocate cursor c1&lt;br /&gt;
 go&lt;br /&gt;
 drop proc update_statistics&lt;br /&gt;
 go&lt;br /&gt;
&lt;br /&gt;
[[Category:ASE]]&lt;/div&gt;</summary>
		<author><name>Psap</name></author>	</entry>

	</feed>