Oracle FAQ | Your Portal to the Oracle Knowledge Grid |
Home -> Community -> Usenet -> c.d.o.server -> Re: using sequences in triggers to generate identities
if wrote:
> Hi,
>
> ';m coming from a db2 background.
> I was wondering if in Oracle there is any performance (or other)
> difference between using a sequence in insert statements and using an
> insert trigger to do the same job.
CREATE TABLE t (
testcol NUMBER(10));
ALTER TABLE t
ADD CONSTRAINT pk_t
PRIMARY KEY (testcol)
USING INDEX
PCTFREE 0;
CREATE SEQUENCE seq_t;
set timing on
BEGIN
FOR i IN 1 .. 100000 LOOP
INSERT INTO t (testcol) VALUES (seq_t.NEXTVAL);
set timing off
DROP SEQUENCE seq_t;
TRUNCATE TABLE t;
CREATE SEQUENCE seq_t;
CREATE OR REPLACE TRIGGER bi_t
BEFORE INSERT
ON t
FOR EACH ROW
BEGIN
SELECT seq_t.NEXTVAL
INTO :NEW.testcol
FROM dual;
END;
/
set timing on
BEGIN
FOR i IN 1 .. 100000 LOOP
INSERT INTO t (testcol) VALUES (1);
set timing off
-- Daniel A. Morgan University of Washington damorgan_at_x.washington.edu (replace x with u to respond) Puget Sound Oracle Users Group www.psoug.orgReceived on Mon Apr 23 2007 - 17:34:52 CDT