Advertisement
Guest User

Untitled

a guest
Apr 19th, 2015
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1. drop table m_loan_transaction_temp;
  2.  
  3. /**Temp table with primary key and indices**/
  4. CREATE TABLE `m_loan_transaction_temp` (
  5. `id` BIGINT(20) NOT NULL,
  6. `loan_id` BIGINT(20) NOT NULL,
  7. `amount` DECIMAL(19,6) NOT NULL DEFAULT '0.000000',
  8. `transaction_date` DATE NOT NULL,
  9. PRIMARY KEY (`id`),
  10. INDEX `loan_id` (`loan_id`),
  11. INDEX `transaction_date` (`transaction_date`)
  12. )
  13. COLLATE='utf8_general_ci'
  14. ENGINE=InnoDB;
  15.  
  16. /**Copy data to temp table, skip accrual transaction, greatly reduces table size**/
  17. INSERT INTO m_loan_transaction_temp(`id`,`loan_id`,`transaction_date`,`amount`)
  18. select lt.id, lt.loan_id,lt.transaction_date,if(lt.transaction_type_enum = 1 , IFNULL(lt.amount,0),IFNULL(-lt.principal_portion_derived,0)) from m_loan_transaction lt where lt.is_reversed=0 and lt.transaction_type_enum <> 10;
  19.  
  20.  
  21. /**Drop stored procedure**/
  22. drop procedure update_running_balance;
  23.  
  24.  
  25. /**Update Balance for 500 records at a time using store procedure**/
  26. DELIMITER //
  27.  
  28. CREATE PROCEDURE `update_running_balance` ()
  29. LANGUAGE SQL
  30. DETERMINISTIC
  31. SQL SECURITY DEFINER
  32. COMMENT 'Update Running Balances for Loans'
  33. BEGIN
  34.  
  35. DECLARE max_transaction INT DEFAULT 0;
  36. DECLARE i INT DEFAULT 0;
  37. DECLARE n INT DEFAULT 0;
  38. DECLARE page_size INT DEFAULT 500;
  39.  
  40. select max(id) into max_transaction from m_loan_transaction;
  41.  
  42.  
  43. SET i = 0;
  44.  
  45. theLoop:LOOP
  46. SET n = i + page_size;
  47.  
  48.  
  49. IF i < max_transaction THEN
  50.  
  51. START TRANSACTION;
  52.  
  53. UPDATE m_loan_transaction lt SET lt.outstanding_loan_balance_derived = (select sum(ltt.amount) from m_loan_transaction_temp ltt where ((ltt.transaction_date = lt.transaction_date and ltt.id <= lt.id) or ltt.transaction_date < lt.transaction_date) and ltt.loan_id = lt.loan_id) where lt.transaction_type_enum != 10 and lt.is_reversed = 0 and lt.id BETWEEN i AND n;
  54. SET i = i + page_size;
  55.  
  56. COMMIT;
  57.  
  58. ITERATE theLoop;
  59.  
  60.  
  61. END IF;
  62.  
  63. LEAVE theLoop;
  64. END LOOP theLoop;
  65.  
  66. END//
  67.  
  68. /**Call stored procedure**/
  69. call update_running_balance();
  70.  
  71. /**Drop stored procedure**/
  72. drop procedure update_running_balance;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement