php - Why submit transaction every row is so slow? -
the table empty @ first .
when commit transaction after 20000 row ,it cost less 1 second .
$stmt = $conn->prepare("insert iptable (ip) values (?)"); $stmt->bind_param("i", $ip); $conn->query('begin'); for( $count = 0 ; $count < 20000 ; $count ++){ $ip = rand(1,10000000); $stmt->execute(); } $conn->query('commit'); $stmt->close(); $conn->close();
with time command
time php test.php real 0m0.785s user 0m0.220s sys 0m0.096s
but when commented out $conn->query('begin'); $conn->query('commit'); , run more 20 minute , don't know how time have wait finish .i have stop ctrl-c .
$stmt = $conn->prepare("insert iptable (ip) values (?)"); $stmt->bind_param("i", $ip); //$conn->query('begin'); for( $count = 0 ; $count < 20000 ; $count ++){ $ip = rand(1,10000000); $stmt->execute(); } //$conn->query('commit'); $stmt->close(); $conn->close();
try turning on autocommit
$conn->autocommit(true);
$conn->autocommit(true); $stmt = $conn->prepare("insert iptable (ip) values (?)"); $stmt->bind_param("i", $ip); //$conn->query('begin'); for( $count = 0 ; $count < 20000 ; $count ++){ $ip = rand(1,10000000); $stmt->execute(); } //$conn->query('commit'); $stmt->close(); $conn->close();
Comments
Post a Comment