un-optimized version:
$wpdb; $t1 = microtime(TRUE); for($i = 0; $i < 10000; $i++){ $wpdb->query(" INSERT INTO test (value) VALUES ('xxx') "); } $t2 = microtime(TRUE); echo $t2 - $t1; die;
Runtime(Seconds):
4.44
4.61
4.33
Optimized version:
$wpdb; $t1 = microtime(TRUE); $sql = "INSERT INTO test (value) VALUES "; $arr = array(); for($i = 0; $i < 10000; $i++){ $arr[] = "('xxx')"; } $sql .= implode(',', $arr); $wpdb->query( $sql ); $t2 = microtime(TRUE); echo $t2 - $t1; die;
Runtime(Seconds):
0.074
0.091
0.065
Use of transaction and commit:
$wpdb; $t1 = microtime(TRUE); $wpdb->query('START TRANSACTION'); for($i = 0; $i < 10000; $i++){ $wpdb->query(" INSERT INTO test (value) VALUES ('xxx') "); } $wpdb->query('COMMIT'); $t2 = microtime(TRUE); echo $t2 - $t1; die;
Runtime(Seconds):
2.21
2.55
2.47