php - 在codeigniter中批量删除

标签 php codeigniter-3

insert_batch() 是一个 codeigniter 内置函数,它一次插入 100 行数据。这就是插入大量数据要快得多的原因。

现在我想像 insert_batch() 函数那样删除大量数据。

他们有什么办法吗?

我已经在使用 where_in 函数,但它没有 insert_batch() 快多少,这就是经常发生超时错误的原因。

我特别想知道,我可以在 codeigniter system/database/db_query_builder 中创建类似 insert_batch()insert_update() 的函数吗?

如果我可以怎么做。或者任何其他建议?

最佳答案

如果我们谈论很多行,通过插入和删除原始表来执行表“切换”可能更容易。

然而,这样做的一个缺点是任何自动增量 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" );

关于php - 在codeigniter中批量删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44166526/

相关文章:

PHP数据库获取变量

php - 使用 MAX 和 GROUP BY 从表中提取数据

php - 如何检查发布的值是否已经存在于数据库中?

php - 如何用名称替换两个用户 ID 列?

php - 使用数据库与文件系统进行图像排序

mysql - 如何在 CodeIgniter 中编写以下查询?

php - Codeigniter 3 博客应用程序 : whenever a category is deleted, 将该类别中所有帖子的 cat_id 设置为 1

php - POST 响应为 null()

codeigniter - 如何在 Codeigniter 中的更新批处理中使用 where 条件

php - Laravel 对多列进行唯一验证