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/