問(wèn)題
在Log中發(fā)現(xiàn)數(shù)據(jù)庫(kù)插入出錯(cuò),Error: Can't create more than max_prepared_stmt_count statements (current value: 16382)录择,初遇該問(wèn)題的我桦卒,一臉懵逼立美!
原因
主因: 在寫(xiě)插入語(yǔ)句時(shí),沒(méi)有做參數(shù)化的優(yōu)化方灾,如下:
let sqlString = "INSERT INTO `TABLE` (`FIELD1`, `FIELD2`, `FIELD3`, `FIELD4`, `FIELD5`, `datetime`) VALUES (NULL, 19360.34, 18982.02, 18900, 19598, '2020-12-04T10:59:28.472Z')"
conn.execute(sqlString);
次因:NodeJS里mysql2模塊中的用到了Prepare Statement提高性能建蹄,https://www.npmjs.com/package/mysql2#using-prepared-statements
解決方案
修改sql語(yǔ)句為:
let sqlString = "INSERT INTO `TABLE` (`FIELD1`, `FIELD2`, `FIELD3`, `FIELD4`, `FIELD5`, `datetime`) VALUES (?, ?, ?, ?, ?, ?")"
const params = [open,close,low,high,`'${datetime}'`];
conn.execute(sqlString,params)
但是在更新語(yǔ)句進(jìn)行參數(shù)化查詢時(shí),遇到了問(wèn)題錯(cuò)誤:MySQL: ERROR 2027 (HY000): Malformed packet迎吵。
唉躲撰。。击费。只能保持更新語(yǔ)句不變拢蛋,每次執(zhí)行完sql語(yǔ)句后,清除prepare的緩存了蔫巩。
sqlString = updateSQLString;
conn.execute(sqlString)
.then(()=>{
conn.unprepare(sqlString);
resolve(sqlString);
})
.catch((err)=>{
reject(err);
});