Home >Database >Mysql Tutorial >How Can I Perform Bulk Inserts into MySQL Using Node-MySQL?
Performing Bulk Inserts in MySQL Using Node.js and Node-MySQL
When utilizing the node-mysql library (https://github.com/felixge/node-mysql) to interact with MySQL, it is possible to perform bulk inserts to improve efficiency.
To achieve bulk inserts, you can employ nested arrays. The node-mysql documentation states that "nested arrays are turned into grouped lists (for bulk inserts)."
To clarify, you insert nested arrays of elements. For instance, consider the following example:
var mysql = require('mysql'); var conn = mysql.createConnection({ ... }); var sql = "INSERT INTO Test (name, email, n) VALUES ?"; var values = [ ['demian', '[email protected]', 1], ['john', '[email protected]', 2], ['mark', '[email protected]', 3], ['pete', '[email protected]', 4] ]; conn.query(sql, [values], function(err) { if (err) throw err; conn.end(); });
Here, values is an array of arrays, where each inner array represents a row to be inserted.
It is important to note that you can also utilize a different node-msql package that is specifically designed for bulk inserts.
The above is the detailed content of How Can I Perform Bulk Inserts into MySQL Using Node-MySQL?. For more information, please follow other related articles on the PHP Chinese website!