Обсуждение: Triggers and locking

Поиск
Список
Период
Сортировка

Triggers and locking

От
William Temperley
Дата:
Dear all,

I have a single "source" table that is referenced by six
specialization tables, which include:
"journal_article"
"report"
4 more....

There is a "citation" column in the source, which is what will be
displayed to users. This is generated by a trigger function on each
specialization table that calls a function to generate the citation,
and saves it on the "source" table.

Running the function get_report_citation(<source>, <report>) takes
~2ms, but when run as a trigger it takes ~5000 ms!

e.g.:
"""
update source set citation = get_report_citation(
    (select source from source where id = NEW.source_ptr_id),
    NEW
);
"""

If I take the citation out of line, this time is reduced to ~300ms,
but that's still v slow:
"""
cit = get_report_citation((select source from source where id =
NEW.source_ptr_id), NEW);
update source set citation = cit;
"""

If I use the following hack in this trigger, which forces the source
row to update itself (with a separate trigger) it takes >6000ms!
"""
update source set citation = '';
"""

Looking in the server monitor, there appears to be a lot of locking
happening that I don't fully understand.
Would anyone know of a better method here?

I'm on 8.3.11.

Thanks

Will Temperley

Re: Triggers and locking

От
Alban Hertroys
Дата:
On 21 Sep 2010, at 16:13, William Temperley wrote:

> Dear all,
>
> I have a single "source" table that is referenced by six
> specialization tables, which include:
> "journal_article"
> "report"
> 4 more....
>
> e.g.:
> """
> update source set citation = get_report_citation(
>    (select source from source where id = NEW.source_ptr_id),
>    NEW
> );
> """

Well, depending on how many rows are in source, updating them all can take a while.
Eventually those changes will have to go to disk, so it's probably pretty much I/O-bound.

I get the impression you're missing a WHERE clause on that UPDATE statement though, or otherwise I can't understand why
you'dwant to update all citations every time one source record changes. 


Alban Hertroys

--
If you can't see the forest for the trees,
cut the trees and you'll see there is no forest.


!DSPAM:737,4c98edf010251425489017!



Re: Triggers and locking

От
William Temperley
Дата:
On 21 September 2010 18:39, Alban Hertroys
<dalroi@solfertje.student.utwente.nl> wrote:
> On 21 Sep 2010, at 16:13, William Temperley wrote:
>
>> Dear all,
>>
>> I have a single "source" table that is referenced by six
>> specialization tables, which include:
>> "journal_article"
>> "report"
>> 4 more....
>>
>> e.g.:
>> """
>> update source set citation = get_report_citation(
>>    (select source from source where id = NEW.source_ptr_id),
>>    NEW
>> );
>> """
>
> Well, depending on how many rows are in source, updating them all can take a while.
> Eventually those changes will have to go to disk, so it's probably pretty much I/O-bound.
>
> I get the impression you're missing a WHERE clause on that UPDATE statement though, or otherwise I can't understand
whyyou'd want to update all citations every time one source record changes. 
>
>
> Alban Hertroys
>

Indeed you're right, I was just missing a WHERE clause. Failed at the
last hurdle there.

All works as intended now, thankyou.

Will Temperley