Codeigniter insert_batch限制问题

时间:2016-06-21 13:17:54

标签: php mysql codeigniter sql-insert codeigniter-3

我使用codeigniter insert_batch()函数而不是使用简单插入来循环数据。我的行数约为390,只有100个插入,因为codeigniter(或mysql)不允许在单个查询中插入超过100行。

然后我用 array_chunk 函数将100分100分隔,如下所示:

$all_hafars = array_chunk($hafar_co,100);
foreach ($all_hafars as $hafar) {
    $this->db->insert_batch('hafar_co', $hafar);
}

再次只有100个插入!有什么想法吗?

修改:我甚至使用简单的insert功能与交易。当我使用事务时,它再次只插入100行。

2 个答案:

答案 0 :(得分:3)

PHP的查询受 max_allowed_packet 配置选项的限制。它定义了查询字符串的绝对长度限制(以字符为单位)。请注意,这不仅仅是要插入的数据的总大小,而是整个查询字符串。 SQL命令,标点符号,空格等...

SHOW VARIABLES WHERE Variable_name LIKE  '%max_allowed_packet%'

答案 1 :(得分:1)

  

### MAXIMIZATION STATS ### # objective: p (index: 0) # interval: [ -INF , +INF ] # # Search terminated! # Exact strict optimum found! # Optimum: <1 # Search steps: 1 (sat: 1) # - binary: 0 (sat: 0) # - linear: 1 (sat: 1) # Restarts: 1 [session: 1] # Decisions: 3 (0 random) [session: 3 (0 random)] # Propagations: 6 (0 theory) [session: 13 (0 theory)] # Watched clauses visited: 1 (0 binary) [session: 2 (1 binary)] # Conflicts: 3 (3 theory) [session: 3 (3 theory)] # Error: # - absolute: 0 # - relative: 0 # Total time: 0.000 s # - first solution: 0.000 s # - optimization: 0.000 s # - certification: 0.000 s # Memory used: 8.977 MB sat ( (p (/ 1999999 2000000)) (q (/ 1 2000000)) ) 会阻止一些insert_batch限制问题。一世   认为它与MySQL中的配置有关。如果你试图跨越某些东西,主要的想法是避免。

我建议您使用事务而不是insert_batch。

您可以在此处详细了解交易:https://www.codeigniter.com/user_guide/database/transactions.html

您可以在此处详细了解您的问题:https://github.com/bcit-ci/CodeIgniter/issues/2680