Unix Technical Forum

Calculate Statistics after insert in a trigger

This is a discussion on Calculate Statistics after insert in a trigger within the Oracle Database forums, part of the Database Server Software category; --> Hi all and sorry for my english I have a table usually empty (0 rows) and the statistics know ...


Go Back   Unix Technical Forum > Database Server Software > Oracle Database

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-25-2008, 07:26 AM
Roberto Nenni
 
Posts: n/a
Default Calculate Statistics after insert in a trigger

Hi all and sorry for my english

I have a table usually empty (0 rows) and the statistics know that
In a job i insert into it many rows with an 'insert into table select from
......' for example 2000 rows
This table is after joined with many others tables to obtain a results
The optimizer, believing the table empty, produce an access plan that
doesn't work correctly (18 minutes)

I try to do this:
insert into table select from...
exec dbms_stats.gather_table_stats(ownname=> NULL, tabname=> 'TABLE',
partname=> NULL); /*calculate statistics */
execute query that work fine (15 seconds)

So i think: 'i put the calculation of statistic in a trigger':
CREATE OR REPLACE TRIGGER TABLE_STAT
after insert on TABLE
DECLARE PRAGMA AUTONOMOUS_TRANSACTION;
begin
dbms_stats.gather_table_stats(ownname=> NULL, tabname=> 'TABLE',
partname=> NULL);
end;

but at this time the statistics are not correct because the commit is not
issued and the table seems empty

is it possible to invoke a trigger after the commit operation?

any other ideas?

tia
Roberto


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-25-2008, 07:26 AM
Mark D Powell
 
Posts: n/a
Default Re: Calculate Statistics after insert in a trigger


Roberto Nenni wrote:
> Hi all and sorry for my english
>
> I have a table usually empty (0 rows) and the statistics know that
> In a job i insert into it many rows with an 'insert into table select from
> .....' for example 2000 rows
> This table is after joined with many others tables to obtain a results
> The optimizer, believing the table empty, produce an access plan that
> doesn't work correctly (18 minutes)
>
> I try to do this:
> insert into table select from...
> exec dbms_stats.gather_table_stats(ownname=> NULL, tabname=> 'TABLE',
> partname=> NULL); /*calculate statistics */
> execute query that work fine (15 seconds)
>
> So i think: 'i put the calculation of statistic in a trigger':
> CREATE OR REPLACE TRIGGER TABLE_STAT
> after insert on TABLE
> DECLARE PRAGMA AUTONOMOUS_TRANSACTION;
> begin
> dbms_stats.gather_table_stats(ownname=> NULL, tabname=> 'TABLE',
> partname=> NULL);
> end;
>
> but at this time the statistics are not correct because the commit is not
> issued and the table seems empty
>
> is it possible to invoke a trigger after the commit operation?
>
> any other ideas?
>
> tia
> Roberto


For tables with data pattens like you describe, 0 - X rows, analyze the
table when it has X rows and the resulting plans should work just fine
when the table is empty. Do not allow the table to be reanalyzed.

HTH -- Mark D Powell --

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-25-2008, 07:27 AM
Scott
 
Posts: n/a
Default Re: Calculate Statistics after insert in a trigger

In case you are on 10G with the automatic stats collection enabled you can
use the command

dbms_stats.lock_table_stats(ownname, tablename ) to stop stats from being
collected on this table

HTH
Scott Watson


"Mark D Powell" <Mark.Powell@eds.com> wrote in message
news:1159535661.875615.57470@c28g2000cwb.googlegro ups.com...
>
> Roberto Nenni wrote:
>> Hi all and sorry for my english
>>
>> I have a table usually empty (0 rows) and the statistics know that
>> In a job i insert into it many rows with an 'insert into table select
>> from
>> .....' for example 2000 rows
>> This table is after joined with many others tables to obtain a results
>> The optimizer, believing the table empty, produce an access plan that
>> doesn't work correctly (18 minutes)
>>
>> I try to do this:
>> insert into table select from...
>> exec dbms_stats.gather_table_stats(ownname=> NULL, tabname=> 'TABLE',
>> partname=> NULL); /*calculate statistics */
>> execute query that work fine (15 seconds)
>>
>> So i think: 'i put the calculation of statistic in a trigger':
>> CREATE OR REPLACE TRIGGER TABLE_STAT
>> after insert on TABLE
>> DECLARE PRAGMA AUTONOMOUS_TRANSACTION;
>> begin
>> dbms_stats.gather_table_stats(ownname=> NULL, tabname=> 'TABLE',
>> partname=> NULL);
>> end;
>>
>> but at this time the statistics are not correct because the commit is
>> not
>> issued and the table seems empty
>>
>> is it possible to invoke a trigger after the commit operation?
>>
>> any other ideas?
>>
>> tia
>> Roberto

>
> For tables with data pattens like you describe, 0 - X rows, analyze the
> table when it has X rows and the resulting plans should work just fine
> when the table is empty. Do not allow the table to be reanalyzed.
>
> HTH -- Mark D Powell --
>



Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 10:20 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com