【NodeJS】Error: Can't create more than max_prepared_stmt_count statements (current value: 16382)

问题

在Log中发现数据库插入出错,Error: Can't create more than max_prepared_stmt_count statements (current value: 16382),初遇该问题的我,一脸懵逼!

原因

主因: 在写插入语句时,没有做参数化的优化,如下:

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语句为:

let sqlString = "INSERT INTO `TABLE` (`FIELD1`, `FIELD2`, `FIELD3`, `FIELD4`, `FIELD5`, `datetime`) VALUES (?, ?, ?, ?, ?, ?")"
const params = [open,close,low,high,`'${datetime}'`];
conn.execute(sqlString,params)

但是在更新语句进行参数化查询时,遇到了问题错误:MySQL: ERROR 2027 (HY000): Malformed packet。

唉。。。只能保持更新语句不变,每次执行完sql语句后,清除prepare的缓存了。

sqlString = updateSQLString;
conn.execute(sqlString)
        .then(()=>{
          conn.unprepare(sqlString);
          resolve(sqlString);
        })
        .catch((err)=>{
          reject(err);
        });

你可能感兴趣的:(【NodeJS】Error: Can't create more than max_prepared_stmt_count statements (current value: 16382))