简体   繁体   English

在codeigniter中批量删除

[英]batch delete in codeigniter

insert_batch() is a codeigniter build-in function which inserts 100 data of rows at a time. insert_batch()是一个codeigniter内置函数,它一次插入100个行数据。 That's why it is so much faster for inserting large amount of data. 这就是为什么它插入大量数据的速度要快得多。

Now I want to delete large number of data like insert_batch() function does. 现在我想删除像insert_batch()函数那样的大量数据。

Is their any way to do it? 他们有什么办法吗?

Already I am using where_in function but it is not that much faster like insert_batch() and that's why timeout error occur often. 我已经在使用where_in函数了,但它没有像insert_batch()那么快,这就是经常发生超时错误的原因。

I want to know specially that can i make a function like insert_batch() or insert_update() in codeigniter system/database/db_query_builder ? 我想知道我可以在codeigniter system/database/db_query_builder创建一个类似insert_batch()insert_update() system/database/db_query_builder吗?

If I can how to do it. 如果我能怎么做 or any other suggestion please ? 还是其他任何建议?

If we are talking about alot of rows, it may be easier to perform a table 'switch' by inserting and dropping the the original table. 如果我们谈论很多行,通过插入和删除原始表来执行表'切换'可能更容易。

However one drawback to this will mean any Auto Increment IDs will be lost. 但是,这样做的一个缺点将意味着任何自动增量ID都将丢失。

<?php

// Create a Copy of the Same Data structure
$this->db->query( "CREATE TABLE IF NOT EXISTS `TB_TMP` .. " );

// Select the data you wish to -Keep- by excluding the rows by some condition.
// E.g. Something that is no longer active.
$recordsToKeep = 
    $this->db
         ->select( "*" )
         ->get_where( "TB_LIVETABLE", [ "ACTIVE" => 1 ])
         ->result();

// Insert the Excluded Rows.
$this->db->insert_batch( "TB_TMP", $recordsToKeep );

// Perform the Switch
$this->db->query( "RENAME TABLE TB_LIVETABLE TO TB_OLDTABLE" );
$this->db->query( "RENAME TABLE TB_TMP TO TB_LIVETABLE " );
$this->db->query( "DROP TABLE TB_OLDTABLE" );

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

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