从你的业务逻辑看,a与b1是主从表,应包含主键与附键的约束,即先insert a表的数据,后insert b1。如此,你的想法不能实现。
如果你去掉主键与附键的约束,先insert b1表的数据,后insert a,我想trigger你也因该会了。但你要注意出现的其他问题。

解决方案 »

  1.   

    是先insert a表的数据,后insert b1。触发器远不如写程序方便。我想不出办法。主要就是因为b1表太大,上千万的数据。客户查询觉得太慢。我现在想加index都加不上。一加数据库就死。郁闷!!
      

  2.   

    在a表建立一个after insert 的行级触发器不行吗?
      

  3.   

    create or replace trg_mytable   
      after insert on b1
      for each row
    declare
      mykey char(x);
      myflag char(x);
    begin
      If :new.flag2 <> '0' then Return; end if;  select key,nvl(flag1,'*') into mykey,myflag
        from a
       where a.key = :new.key 
         and a.flag1 = '0';  if myflag='*' then Return; end if;  insert into b2 values (:new.x,:new.y,:new.z.....);
    end trg_mytable;
      

  4.   

    create or replace trg_mytable   
      after insert on b1
      for each row
    declare
      mykey char(x);
      myflag char(x);
    begin
      --假设插入值不为0,则返回
      If :new.flag2 <> '0' then Return; end if;  select key,nvl(flag1,'*') into mykey,myflag
        from a
       where a.key = :new.key 
         and a.flag1 = '0';  --假设a表中,相关flag1不为0,则返回
      if myflag='*' then Return; end if;  --插入b2
      insert into b2 values (:new.x,:new.y,:new.z.....);
    end trg_mytable;
      

  5.   

    不是这个意思拉。sanoul(垃圾)的办法也是一样,对b1做全部触发。因为每插入一条都会触发,所以效率较低。根本不考虑a就可以用触发实现。
    If :new.flag2 = '0' then
    insert into b2 values ...
    我想怎么能提高效率。举例,a表插入10条,b1就会插入1000条。a表10条中只有一条flag1 = '0',b1中就是1000条中才有一条flag2 = '0'。  我想减少判断次数。
      

  6.   

    从技术上(程序操作的程序),能否在insert b1时,对flag2=0的记录先设flag2=999 insert 到b1表内,然后再把flag2 update 为0。这样,可以去掉insert 触发器,不必每条都判断。而在b1上加update触发器。
    性能能否改善,还需要试验。
      

  7.   

    谢谢onejune4450(中文字符),比较新颖的思路。
      

  8.   

    还有一个效率问题,就是索引,没有索引时候的插入和存在索引时的插入是有明显区别的。至于你说的建立索引产生问题,可能是和tablespace有关联,倒不一定是什么数据问题。++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    至于逐行触发问题,我现在使用的数据量大概在400万条左右,全部遍历并不需要很多时间。++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
      

  9.   

    用select count(*) 查询,9:35---31358429条纪录,9:45----31361549条纪录。无索引,平均每秒钟5条。