src-db/database/model/triggers/M_WAREHOUSE_TRG.xml
author Jorge Garcia <jorge.garcia@openbravo.com>
Wed, 20 May 2015 18:17:08 +0200
changeset 26789 f89eff26d185
parent 20233 934faa344e9e
child 26790 ef5da8dc7f38
permissions -rw-r--r--
Fixed issue 25278: Long time to update a warehouse with thousends of locators

On an environment with 60.000 locators for one of the warehouse, when updating
a field of the warehouse it takes a long time to save the record.

Part of the problem is the update of the m_warehouse table. When updated, the
trigger launch an update of the organization id for M_LOCATOR and
M_WAREHOUSE_ACCT tables.

The solution is to check if the organization has changed and, if not, avoid the
unnecessary update of those tables.
<?xml version="1.0"?>
  <database name="TRIGGER M_WAREHOUSE_TRG">
    <trigger name="M_WAREHOUSE_TRG" table="M_WAREHOUSE" fires="after" insert="true" update="true" delete="false" foreach="row">
      <body><![CDATA[
    /*************************************************************************
    * The contents of this file are subject to the Compiere Public
    * License 1.1 ("License"); You may not use this file except in
    * compliance with the License. You may obtain a copy of the License in
    * the legal folder of your Openbravo installation.
    * Software distributed under the License is distributed on an
    * "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
    * implied. See the License for the specific language governing rights
    * and limitations under the License.
    * The Original Code is  Compiere  ERP &  Business Solution
    * The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
    * Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke,
    * parts created by ComPiere are Copyright (C) ComPiere, Inc.;
    * All Rights Reserved.
    * Contributor(s): Openbravo SLU
    * Contributions are Copyright (C) 2001-2008 Openbravo, S.L.U.
    *
    * Specifically, this derivative work is based upon the following Compiere
    * file and version.
    *************************************************************************
    * New Accounting Defaults
    */


  TYPE RECORD IS REF CURSOR;
  Cur_Defaults RECORD;
    
BEGIN
    
    IF AD_isTriggerEnabled()='N' THEN RETURN;
    END IF;


  IF(INSERTING) THEN
    FOR Cur_Defaults IN
      (
      SELECT *
      FROM C_AcctSchema_Default d
      WHERE EXISTS
        (
      SELECT 1 
      FROM AD_Org_AcctSchema
      WHERE (AD_IsOrgIncluded(AD_Org_ID, :new.AD_ORG_ID, :new.AD_Client_ID)<>-1
          OR AD_IsOrgIncluded(:new.AD_Org_ID, AD_ORG_ID, :new.AD_Client_ID)<>-1)
      AND IsActive = 'Y'
      AND AD_Org_AcctSchema.C_AcctSchema_ID = d.C_AcctSchema_ID
      AND AD_Org_AcctSchema.AD_Client_ID=:new.AD_Client_ID
        )
      )
    LOOP
      INSERT
      INTO M_Warehouse_Acct
        (
          M_Warehouse_Acct_ID, M_Warehouse_ID, C_AcctSchema_ID, AD_Client_ID,
          AD_Org_ID, IsActive, Created,
          CreatedBy, Updated, UpdatedBy,
          W_Inventory_Acct, W_Differences_Acct, W_Revaluation_Acct,
          W_InvActualAdjust_Acct
        )
        VALUES
        (
          get_uuid(), :new.M_Warehouse_ID, Cur_Defaults.C_AcctSchema_ID, :new.AD_Client_ID,
          :new.AD_ORG_ID,  'Y', now(),
          :new.CreatedBy, now(), :new.UpdatedBy,
          Cur_Defaults.W_Inventory_Acct, Cur_Defaults.W_Differences_Acct, Cur_Defaults.W_Revaluation_Acct,
          Cur_Defaults.W_InvActualAdjust_Acct
        )
        ;
    END LOOP;
  ELSIF (UPDATING) THEN
    IF (:new.AD_ORG_ID <> :old.AD_ORG_ID) THEN
      UPDATE M_WAREHOUSE_ACCT SET AD_ORG_ID = :new.AD_ORG_ID
      WHERE M_WAREHOUSE_ID = :new.M_WAREHOUSE_ID;
    
      UPDATE M_LOCATOR SET AD_ORG_ID = :new.AD_ORG_ID
      WHERE M_WAREHOUSE_ID = :new.M_WAREHOUSE_ID;
      END IF;
  END IF;
END M_WAREHOUSE_TRG
]]></body>
    </trigger>
  </database>