Skip to content
Advertisement

Optimization of Oracle function

I wrote a function in the oracle database that calculates saldo and finds the date of the operation. Here’s how it works:

  1. He will receive
  2. Start subtracting the first cell of TURNOVER_DEBIT column from the vSumm and save it.
  3. Then, from the saved number, subtract the next cell again
  4. And so on until vSumm becomes less than or equal to 0.
  5. When vSumm <= 0 it grabes and return OPER_DAY record.

It is taking over 20 minutes. Because the average saldo records for per customer is 70-80 and It is looping for 140 000 clients.

How can I optimize my query? Any help would be much appreciated! Thank you.

Advertisement

Answer

You can use the single query to fetch the OPER_DAY with the mentioned requirement and then return it from the function as follows:

Here, I have considered that you want to scan from highest OPER_DAY to lowest OPER_DAY to sum the TURNOVER_DEBIT and once the sum of the TURNOVER_DEBIT becomes equal or more than ISALDO, the scan should stop and you must return that OPER_DAY.

A single query can give results way faster than looping through each and every record of the table, do some arithmetic, and take decisions based on that arithmetic.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement