简体   繁体   English

使用node.js批量删除mysql行

[英]Bulk deletion of mysql rows using node.js

var Sequelize= require('sequelize')
var sequelize = new Sequelize('db', 'user', 'password',
{pool: { maxConnections: 5, maxIdleTime: 30 }})
var table = sequelize.define('User', 
{ 
       trip_paramid: Sequelize.INTEGER
},

{timestamps:false})

table.sync({ force: false }).success(function()
{
  table.destroy('`id` >= 5685321').success(function() {
  console.log("deleted");
  })
  table.destroy('`id` >= 9600000').success(function() {
  console.log("deleted");
  })
  table.destroy('`id` >= 15600000').success(function() {
  console.log("deleted");
  })
  table.destroy('`id` >= 20000000').success(function() {
  console.log("deleted");
  })
  table.destroy('`id` >= 25000000').success(function() {
  console.log("deleted");
  })
  table.destroy('`id` >= 35600000').success(function() {
  console.log("deleted");
  })
})

I have used sequelize as an ORM here for bulk deletion of rows based on a condition. 我在这里使用sequelize作为ORM来基于条件批量删除行。 It is working. 这是工作。 But now i am trying to do the same function without using sequelize. 但是现在我正在尝试不使用sequelize而执行相同的功能。 The problem is because i need to delete rows from an existing database. 问题是因为我需要从现有数据库中删除行。 In this scenario using sequelize becomes a problem. 在这种情况下使用续集成为一个问题。 i have tried bulk deletion without using sequelize it is also working but the whole process has become slow. 我试过不使用sequelize进行批量删除,它也可以正常工作,但是整个过程变慢了。

var mysql = require('mysql');              
var pool  = mysql.createPool({
connectionLimit : 10,
host            : 'host',
user            : 'user',
password        : 'password',
database        : 'db',
multipleStatements: true
 });


var id=57166;
var query='delete from users1 where id=';
bulkdelete();

function bulkdelete()
{
if(trip_paramid >=57166)
  {
   pool.query(query+ id+';'+query+(id+1)+';'+query+(id+1)+';'+query+(id+1)+';'+query+(id+1)+';', function(err, results) 
   {
    if (err) throw err;
    trip_paramid++;
        bulkdelete();
        console.log("deleted1");
    });
}
}

Can anyone suggest an idea to solve this issue. 任何人都可以提出解决此问题的想法。 I need to speed up the process (many rows in less time without using any ORM) 我需要加快处理速度(无需使用任何ORM即可在更短的时间内完成许多行)

Couple issues with your code that I can see. 我看到的几个与您的代码有关的问题。

First off, the concatenation of strings into the query the way you're doing it is extremely dangerous (prone to other folks hacking your stuff) and it's also slow by itself and a memory hog. 首先,将字符串连接到查询中的方式非常危险(容易被其他人窃取您的资料),并且它本身也很慢,而且耗费内存。 You're allocating a new string for every time you do a + operation. 您每次执行+操作都会分配一个新的字符串。

Second, databases excel in set operations and doing them quickly; 其次,数据库擅长设置操作并能快速完成操作。 why do it one-by-one if you're trying to get everything greater than 57166, just build the query like that. 如果要使所有内容都大于57166,为什么要一一进行,只需像这样构建查询即可。

delete from users where id >= 57166; .

If that needs to be an actual parameter, use the prepared statements as described in the mysql2 driver: https://github.com/sidorares/node-mysql2 如果需要作为实际参数,请使用mysql2驱动程序中所述的准备好的语句: https : //github.com/sidorares/node-mysql2

Your query would then look something like: 您的查询将如下所示:

delete from users where id >=?;

and you'd pass the min ID number as a parameter. 并将最小ID号作为参数传递。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM