简体   繁体   English

使用Codeigniter优化CSV导出

[英]optimizing CSV export with codeigniter

i have recently migrated my export CSV code from core php to code igniter. 我最近将我的导出CSV代码从核心php迁移到了代码点火器。 the code works well but its very slow when exporting very large amount of data.. 该代码可以正常工作,但是在导出大量数据时速度非常慢。

here is my old code: 这是我的旧代码:

function exportCSV($qry,$con,$title)
{
$result = mysql_query($qry, $con) or die(mysql_error($con));

header('Content-Type: text/csv; charset=UTF-8');
header("Cache-Control: no-store, no-cache");  
header("Content-Disposition: attachment;filename=".$title."-".date('mdY').".csv");
//echo "\xEF\xBB\xBF";

$row = mysql_fetch_assoc($result);
 if ($row) {
  echocsv(array_keys($row));
 }
 while ($row) {
  echocsv($row);
  $row = mysql_fetch_assoc($result);
 }
}

function echocsv($fields)
{
    $separator = '';
    foreach ($fields as $field) {
        if (preg_match('/\\r|\\n|,|"/', $field)) {
            $field = '"' . str_replace('"', '""', $field) . '"';
        }
        echo $separator . $field;
        $separator = ',';
    }
    echo "\r\n";
}

and here is my codeigniter code that is very slow... exporting data to CSV with 77000 rows took about 15 minutes excluding the download time.. 这是我的codeigniter代码,它很慢...将数据导出到具有77000行的CSV大约需要15分钟,这不包括下载时间。

public function exportCSV()
{
    set_time_limit(0);
    $delimiter = ",";
    $newline = "\r\n";

    $curr_date_time = date("l jS \of F Y h:i:s A");

    $this->products_model->set_venture($this->selected_venture['abbrev']);

    if($get_data = $this->input->get())
    {
        $data = $this->products_model->export_model($get_data);
        $download = $this->dbutil->csv_from_result($data, $delimiter, $newline);
        force_download('export - '.$curr_date_time.'.csv', $download); 
    }
    else
    {
        show_404('page', FALSE);
    }

}

public function export_model($params = NULL)
{
    if ($params != NULL)
    {
        if ($params['name_filter'] != '')
        {
            $this->crawler_db->like('name', $params['name_filter']);
        }

        if ($params['comp_filter'] != '')
        {
            $this->crawler_db->where('fk_competitor_website', $params['comp_filter']);
        }
    }

    return $this->crawler_db->get('pcrawler_'.$this->venture.'.products_final');

}

Hi check database util class can work for you its really simple to generate good CSV files here is the code 嗨,检查数据库util类可以为您工作,它非常简单,可以生成优质的CSV文件,这里是代码

$this->load->dbutil();

$query = $this->db->query("SELECT * FROM mytable");

echo $this->dbutil->csv_from_result($query);

please read the document here Codeigniter CSV export with DB util 请在此处阅读文档, 使用DB util进行Codeigniter CSV导出

I would look at debugging the function, to see where it's actually slow. 我将研究调试该函数,以查看其实际运行速度。

Simple way is to use the Benchmarking Class to see if it's the query that's slow, or the call to csv_from_result(). 简单的方法是使用Benchmarking Class来查看是查询变慢还是对csv_from_result()的调用。

Is it still slow when you don't pass any params to export_model()? 当您不将任何参数传递给export_model()时,它仍然很慢吗? If it's only slow when you run a like or where against the database, then maybe you need to add some indexes? 如果只是在对数据库运行like或在数据库上运行时速度很慢,那么也许您需要添加一些索引?

https://ellislab.com/codeigniter/user-guide/libraries/benchmark.html https://ellislab.com/codeigniter/user-guide/libraries/benchmark.html

Once you find the bottleneck, you can go from there to 找到瓶颈后,您可以从那里转到

code igniter's csv_from_result is really slow for me, i made a different one to suit my needs.. it can be re used for other CSV exports too... 代码点火器的csv_from_result对我来说真的很慢,我根据自己的需要做了另外一个..它也可以用于其他CSV导出...

public function array_to_csv($array)
{
   if (count($array) == 0) {
     return null;
   }
   ob_start();
   $df = fopen("php://output", 'w');
   fputcsv($df, array_keys(reset($array)));
   foreach ($array as $row) {
      fputcsv($df, $row);
   }
   fclose($df);
   return ob_get_clean();
}

usage: 用法:

$this->array_to_csv($array);

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

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