MySQL PHPExcel查询执行时间太长

时间:2014-09-02 12:29:20

标签: php mysql phpexcel zend-server

我正在运行MySQL的PHP​​Excel输出。

当我运行代码并输出到带有几行输出的Excel时,一切都很好,当我运行以下代码时,除了根据zend服务器日志的非常长的查询执行时间之外,我无处可去。

我根本没有得到输出,我得到一个请求,在看似永恒之后打开一个PHP文件!

任何帮助表示赞赏。

这是迄今为止的代码:

$queryGetIMEI = "SELECT DISTINCT(wi_iridium_og_device) FROM wi_iridium_og ORDER BY wi_iridium_og_device DESC";
//declare the new array for the IMEI numbers
$IMEI = array();

if ($result = $mysqli->query($queryGetIMEI)) {

    /* fetch associative array */
    while ($row = $result->fetch_array()) {
        $IMEI[] = $row["wi_iridium_og_device"];
    }
    /* free result set */
    $result->free();
}

// call the PHPexcel Files
require_once 'classes/PHPExcel.php';
require_once 'classes/PHPExcel/IOFactory.php';


$objPHPExcel = new PHPExcel();

$sheet_count = 0;


foreach ($IMEI as $c) {
    if ($sheet_count > 0) {

        // This creates the next sheet in the sequence
        // One sheet per IMEI in this example
        $objPHPExcel->createSheet();
        $objPHPExcel->setActiveSheetIndex($sheet_count);
    }

    // Add tab label to the sheet
    $objPHPExcel->getActiveSheet()->setTitle(substr($c,0,30));

    // Column headings in the first row
    $objPHPExcel->getActiveSheet()->setCellValue('A1','Device');
    $objPHPExcel->getActiveSheet()->setCellValue('B1','Site Reference');
    $objPHPExcel->getActiveSheet()->setCellValue('C1','Charge Type');
    $objPHPExcel->getActiveSheet()->setCellValue('D1','Date');
    $objPHPExcel->getActiveSheet()->setCellValue('E1','Time');
    $objPHPExcel->getActiveSheet()->setCellValue('F1','Number Called');
    $objPHPExcel->getActiveSheet()->setCellValue('G1','Service');
    $objPHPExcel->getActiveSheet()->setCellValue('H1','Call Termination');
    $objPHPExcel->getActiveSheet()->setCellValue('I1','MSISDN');
    $objPHPExcel->getActiveSheet()->setCellValue('J1','Originating Country');
    $objPHPExcel->getActiveSheet()->setCellValue('K1','UNUSED_3');
    $objPHPExcel->getActiveSheet()->setCellValue('L1','UNUSED_4');
    $objPHPExcel->getActiveSheet()->setCellValue('M1','UNUSED_5');
    $objPHPExcel->getActiveSheet()->setCellValue('N1','UNUSED_6');
    $objPHPExcel->getActiveSheet()->setCellValue('O1','UNUSED_7');
    $objPHPExcel->getActiveSheet()->setCellValue('P1','UNUSED_8');
    $objPHPExcel->getActiveSheet()->setCellValue('Q1','Units');
    $objPHPExcel->getActiveSheet()->setCellValue('R1','Currency');
    $objPHPExcel->getActiveSheet()->setCellValue('S1','Charge');

    // Dynamic data comes next
    // Query the DB based on the IMEI value. Pass the result to the while loop

    $query_GetBillPerIMEI = "SELECT SQL_NO_CACHE wi_iridium_og_device, wi_iridium_og_charge_type, wi_iridium_og_date, wi_iridium_og_time, wi_iridium_og_number_called, wi_iridium_og_service FROM wi_iridium_og WHERE wi_iridium_og_device = ". $c ." AND  wi_iridium_og.wi_iridium_og_charge_type != 'SBD Overage' ORDER BY wi_iridium_og_charge_type ASC";
    $result = $mysqli->query($query_GetBillPerIMEI);    
    $rowcount = 2;
    while ($row = $result->fetch_array()){

        $objPHPExcel->getActiveSheet()->SetCellValue('A' .$rowcount, $row['wi_iridium_og_device']);
        $objPHPExcel->getActiveSheet()->SetCellValue('B' .$rowcount, $row['wi_iridium_og_site_reference']);
        $objPHPExcel->getActiveSheet()->SetCellValue('C' .$rowcount, $row['wi_iridium_og_charge_type']);
        $objPHPExcel->getActiveSheet()->SetCellValue('D' .$rowcount, $row['wi_iridium_og_date']);
        $objPHPExcel->getActiveSheet()->SetCellValue('E' .$rowcount, $row['wi_iridium_og_time']);
        $objPHPExcel->getActiveSheet()->SetCellValue('F' .$rowcount, $row['wi_iridium_og_number_called']);
        $objPHPExcel->getActiveSheet()->SetCellValue('G' .$rowcount, $row['wi_iridium_og_service']);

当我将以下行添加到输出时:

    $objPHPExcel->getActiveSheet()->SetCellValue('G' .$rowcount, $row['wi_iridium_og_service']);

然后该文件无法运行并产生输出。

我在数据库中有超过30,000个需要查询的条目。

帮助!!!

1 个答案:

答案 0 :(得分:0)

首先阅读有关PHP PDOPrepared Statements的内容:这足以处理/避免SQL注入(您的查询问题在WHERE wi_iridium_og_device = ". $c ." AND,因为您没有知道可能有害的$c的价值。

关于如何改进以提高源代码性能......(这不会起作用,请将其读作伪代码)

<?php
// call the PHPexcel Files
require_once 'classes/PHPExcel.php';
require_once 'classes/PHPExcel/IOFactory.php';

$objPHPExcel = new PHPExcel();
$sheet_count = 0;

$queryGetIMEI = "SELECT DISTINCT(wi_iridium_og_device) FROM wi_iridium_og ORDER BY wi_iridium_og_device DESC";

//declare the new array for the IMEI numbers

if ($result = $mysqli->query($queryGetIMEI))
{
    /* fetch associative array */
    while ($row = $result->fetch_array())
    {
        // your foreach login in here (without the foreach)
    }
}

这可以改善您的内存占用,但您可能也可以改善查询。