-
Notifications
You must be signed in to change notification settings - Fork 1
/
database.js
45 lines (37 loc) · 1.02 KB
/
database.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
const Test = require("mysql2")
const pool = Test.createPool({
host: 'localhost',
user: 'root',
database: 'shop_dev',
password: 'root',
port: 33060
})
const batchSize = 10000;
const totalSize = 10_000_000;
let currentID = 1;
const insertBatch = async() => {
const values = [];
for(let i = 0 ; i < batchSize && currentID <= totalSize; i++) {
const name = "Name" + currentID;
const age = currentID;
values.push([name, age]);
currentID++;
}
if(values.length == 0) {
pool.end(err => {
if(err) {
console.log('error')
} else {
console.log('success')
}
})
return;
}
const sql = `INSERT INTO product (name, age) VALUES ?`;
pool.query(sql,[values] , async function(err, results){
if(err) throw err;
console.log(`Insert ${results.affectedRows} records`);
await insertBatch();
})
}
insertBatch().catch(err => console.log(err))