I have a good written function; I guess, but when I use this function on a client that has many transactions it takes like 20 seconds, increasing everytime I add new transaction. This functions starts recounting all client debts since the beginning.
I am using :
- WAMP SERVER 3.1.4
- PHP 7.2.10
- MYSQL 5.7.23
I have tried some changes in php.ini
php.ini
post_max_size = 256M
upload_max_filesize = 128M
memory_limit = 1G
max_input_vars = 10000
and
my.ini
key_buffer_size = 256M
max_allowed_packet = 128M
sort_buffer_size = 128M
net_buffer_length = 8K
read_buffer_size = 128M
read_rnd_buffer_size = 128M
myisam_sort_buffer_size = 512M
innodb_buffer_pool_size = 256M
innodb_log_file_size = 256M
innodb_log_buffer_size = 512M
[mysqldump]
quick
max_allowed_packet = 512M
[isamchk]
key_buffer_size = 512M
sort_buffer_size = 128M
read_buffer_size = 256M
write_buffer_size = 256M
[myisamchk]
key_buffer_size = 512M
sort_buffer_size_size = 512M
read_buffer_size = 256M
write_buffer_size = 256M
and here’s the function
function reCalculateAll($conn, $clid, $cltp){
$stmt = $conn->prepare("SELECT * FROM transactions WHERE client_id = ? AND client_type = ? ORDER BY STR_TO_DATE(transaction_date, '%d/%m/%Y %H:%i:%s') ASC");
$stmt->bind_param('is', $clid, $cltp);
$stmt->execute();
$results = $stmt->get_result();
$stmt->fetch();
$numberofrows = $stmt->num_rows;
while ($row = $results->fetch_assoc()) {
$r = getAllTransactionsClient($conn, $clid, $cltp);
$result = array();
foreach($r as $i => $p){
$result[$p['client_type'].$p['client_id']][] = $p;
foreach ($result as $rr){
foreach ($rr as $c => $k){
reset($rr);
$trid = $k['id'];
$trcn = $k['client_id'];
$trtp = $k['client_type'];
$trdt = $k['transaction_date'];
if($c === key($rr)){
// FIX TRANSACTION
$addm = 0;
$stmtf = $conn->prepare("UPDATE transactions SET client_oldfunds = ?, client_newfunds = ? + added_amount where id = ?");
$stmtf->bind_param('ssi', $addm, $addm, $trid);
$stmtf->execute();
$stmtf->close();
$addm = $k['client_newfunds'];
} else {
$stmtn = $conn->prepare("UPDATE transactions SET client_oldfunds = ?, client_newfunds = ? + added_amount where id = ?");
$stmtn->bind_param('ssi', $addm, $addm, $trid);
$stmtn->execute();
$stmtn->close();
$addm = $k['client_newfunds'];
}
$cnf = getLastDebtFromTransaction($conn, $trtp, $trcn);
setDebts($conn, $trtp, $cnf, $trcn);
}
}
}
}
$results->free();
$stmt->execute();
$stmt->store_result();
$numberofrows = $stmt->num_rows;
if($numberofrows == 0){
setDebts($conn, $cltp, '0', $clid);
}
$stmt->close();
}
getAllTransactionsClient Function
function getAllTransactionsClient($conn, $clid, $cltp){
$stmt = $conn->prepare("SELECT * FROM transactions WHERE client_id = ? AND client_type = ? ORDER BY STR_TO_DATE(transaction_date, '%d/%m/%Y %H:%i:%s') ASC");
$stmt->bind_param('is', $clid, $cltp);
$stmt->execute();
$result = $stmt->get_result();
$products = array();
while ($row = $result->fetch_assoc()) {
$products[] = $row;
}
return $products;
$stmt->close();
}
2
Answers
Pre-cursor
I’m going to skip over "answering your question" – I think a lot of the problems are highlighted in the comments above – and move straight in to a solution for what I think you intend your code to do…
The Problem
Your code isn’t easy to follow, however, I believe the gist of the issue is that:
Your database has been corrupted in some way and fields
client_oldfunds
andclient_newfunds
no longer hold the correct data.You trust the field
added_amount
is correct and want to go back and recalculate the fields above for each transaction so that it all tallies up?Database
Table Structure
Example Current Data
Assume that the transactions here are ordered by date.
Example Correct Data
Assume that the transactions here are ordered by date.
Code
What we want it to do
Actual Code
N.B.
You really should be storing your dates in the MySQL format "Y-m-d H:i:s". It makes sorting easier; formatting should happen when the date is output to the browser.
Your code needs improving, I see that there are multiple loops in one,
Here’s your first function, you can get rid of the last one because it is kinda useless.
Actual function should be like this: