23 lines
936 B
JavaScript
23 lines
936 B
JavaScript
import fs from 'fs'
|
|
import mysql from 'mysql2/promise'
|
|
|
|
export async function update(field, list) {
|
|
const { mysql: config } = JSON.parse(fs.readFileSync('config.json', 'utf8'))
|
|
const conn = await mysql.createConnection(config)
|
|
|
|
console.log('创建临时表')
|
|
await conn.query(`CREATE TEMPORARY TABLE temp_updates (id INT PRIMARY KEY, ${mysql.escapeId(field)} INT UNSIGNED)`)
|
|
|
|
console.log(`数据总量 ${list.length} 条, 每次 10000 条写入 ${Math.ceil(list.length/10000)} 次`)
|
|
for (let i = 0; i < list.length; i += 10000) {
|
|
console.log(`第 ${i/10000+1} 次写入`)
|
|
const values = list.slice(i, i + 10000)
|
|
await conn.query(`INSERT INTO temp_updates (id, ${field}) VALUES ?`, [values])
|
|
}
|
|
|
|
console.log('更新数据从临时表转入生产库')
|
|
await conn.query(`UPDATE web_images t JOIN temp_updates tu ON t.id = tu.id SET t.${field} = tu.${field}`)
|
|
await conn.end()
|
|
console.log('更新完成')
|
|
}
|