2

I want to create a trigger in Oracle 11g. The problem is that I want a trigger which runs every time when there is a SELECT statement. Is this possible or is there other way to achieve the same result. This is the PL/SQL block:

CREATE TRIGGER time_check
   BEFORE INSERT OR UPDATE OF users, passwd, last_login ON table
   FOR EACH ROW
   BEGIN
      delete from table where last_login < sysdate - 30/1440;
   END;

I'm trying to implement a table where I can store user data. I want to "flush" the rows which are old than one hour. Are there other alternatives to how I could implement this?

p.s Can you tell me is this PL/SQL block is correct. Are there any mistakes?

BEGIN
sys.dbms_scheduler.create_job(
job_name => '"ADMIN"."USERSESSIONFLUSH"',
job_type => 'PLSQL_BLOCK',
job_action => 'begin
   -- Insert PL/SQL code here
   delete from UserSessions where last_login < sysdate - 30/1440;
end;',
repeat_interval => 'FREQ=MINUTELY;INTERVAL=2',
start_date => systimestamp at time zone 'Asia/Nicosia',
job_class => '"DEFAULT_JOB_CLASS"',
comments => 'Flushes expired user sessions',
auto_drop => FALSE,
enabled => FALSE);
sys.dbms_scheduler.set_attribute( name => '"ADMIN"."USERSESSIONFLUSH"', attribute => 'job_priority', value => 5);
sys.dbms_scheduler.set_attribute( name => '"ADMIN"."USERSESSIONFLUSH"', attribute => 'logging_level', value => DBMS_SCHEDULER.LOGGING_FAILED_RUNS);
sys.dbms_scheduler.enable( '"ADMIN"."USERSESSIONFLUSH"' );
END;
2
  • I'm trying to implement a table where I can store user data. I want to "flush" the rows which are old than one hour. Can you propose other alternatives how I can implement this? Commented Dec 17, 2011 at 15:42
  • I edited your question to add what you posted in your comment, since it is relevant. (You can do that yourself next time - make sure your question contains all the relevant information.) Commented Dec 17, 2011 at 15:51

1 Answer 1

3

I'm not aware of a way of having a trigger on select. From the documentation, the only statements you can trigger on are insert/delete/update (and some DDL).

For what you want to do, I would suggest a simpler solution: use the DBMS_SCHEDULER package to schedule a cleanup job every so often. It won't add overhead to your select queries, so it should have less performance impact globally.

You'll find lots of examples in: Examples of Using the Scheduler

Sign up to request clarification or add additional context in comments.

7 Comments

OK, I agree. I can use SQL statements every time before the pages are loaded or DBMS_SCHEDULER. Which one of then will use less resources?
It depends on how frequently you need the data purged. If it's only "about an hour", and you don't care if there's a bit more in there, a DBMS_SCHEDULER job (maybe every 15min or so) is going to be relatively cheap. If you need the data purged strictly, then you need to pay the per-page query cost or something similar.
I need to run the DBMS_SCHEDULER every minute. Is this heavy operation?
The part that is potentially expensive is the code you run every minute, the scheduler itself is pretty cheap as long as you don't scheduled too frequently. There are no hard and fast rules to this, you need to try and measure impact on your specific use case.
Ok, so I create the PL/SQL block and add it to DBMS_SCHEDULER to execute it every minute.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.