performance - Is it a bad practice to use php transactions with every query? -
in php can start transactions when performing queries. when should use it? bad use feature every query? or should use query when adding/removing/updating large quantities of data?
example of transaction:
try { $db->begintransaction(); $db->exec("some query"); $stmt = $db->prepare("another query"); $stmt->execute(array($value)); $stmt = $db->prepare("another query again??"); $stmt->execute(array($value2, $value3)); $db->commit(); } catch(pdoexception $ex) { //something went wrong rollback! $db->rollback(); echo $ex->getmessage(); }
my guess would heavily bring down performance if every query in code
tl;dr can use transactions every query? or should use them when performing large manipulations database?
you need use transactions if want make changes or none. example if transferring money 1 person should increase one's balance , decrease another's.
it's use transactions if have many simple queries , need performance. depends on database databases try make changes in memory during transaction , flush disk. memory faster disks.
but matters if have highload site or need insert 1000s of records , each 100ms important. if have less 100 queries, simple , there nothing wrong if execute half of them , script breaks (server restart, php timeout, ...) , data still consistent - may not use transactions.
if think don't use transactions if don't start them explicitly may wrong. depends on database may autostart transaction each query.
https://stackoverflow.com/a/2950764/437763
Comments
Post a Comment