I have two table:
1) One of them is for invoices and there are thousands of data. In my INVOICES table, there are invoices and their prices for customers.
2) The other one is for debts. In my DEBTS table, there are total debts of the invoices for every customer.
My target is to find the closest sums and the invoices of the debts. For instance, i have the tables:
DEBTS table:
CUSTOMER_ID TOTAL_DEBTS
3326660 444$
2789514 165$
4931541 121$
INVOICES table:
CUSTOMER_ID INVOICE_ID AMOUNT_OF_INVOICE
3326660 1a 157$
3326660 1b 112$
3326660 1c 10$
3326660 1d 94$
3326660 1e 47$
3326660 1f 35$
3326660 1g 14$
3326660 1h 132$
3326660 1i 8$
3326660 1j 60$
3326660 1k 42$
2789514 2a 86$
2789514 2b 81$
2789514 2c 99$
2789514 2d 61$
2789514 2e 16$
2789514 2f 83$
4931541 3a 11$
4931541 3b 14$
4931541 3c 17$
4931541 3d 121$
4931541 3e 35$
4931541 3f 29$
My target table is:
CUSTOMER_ID TOTAL_DEBTS CALCULATED_AMOUNT INVOICES_ID
3326660 444$ 444$ 1a,1b,1f,1h,1i
2789514 165$ 164$ 2b,2f
4931541 121$ 121$ 3d
because there are thousands of data in my tables performance is very important for me. I find the code from stackoverflow: closest subset sum
However, the performance is low. I must stop the addition loop when i find the same values between calculeted_amount and total_debts.
Thank you for your help.