简体   繁体   English

MySQL PHPExcel查询执行时间过长

[英]MySQL PHPExcel query taking too long to execute

I am running a PHPExcel output from MySQL. 我正在从MySQL运行PHPExcel输出。

When I run the code and output to Excel with a few lines of the output everything is fine, when I run the following code I am getting nowhere apart from a very long query execution time according to the zend server logs. 当我运行代码并输出到带有几行输出的Excel时,一切都很好,当我运行以下代码时,根据zend服务器日志,查询执行时间非常长,我无处可去。

I am not getting an output at all, I get a request to open a PHP file after what seems an eternity! 我一点都没有得到输出,在看起来像是永恒之后,我得到了打开PHP文件的请求!

Any assistance appreciated. 任何帮助表示赞赏。

here's the code so far: 这是到目前为止的代码:

$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']);

When I add the following line to the output: 当我在输出中添加以下行时:

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

The file then fails to run and produce the output. 该文件然后无法运行并产生输出。

I have over 30,000 entries in the database that need to be queried. 我在数据库中有超过30,000个条目需要查询。

Help!!! 救命!!!

First of all read about PHP PDO and Prepared Statements : this will be enough to deal with/avoid SQL injection (the problem on your query is in here WHERE wi_iridium_og_device = ". $c ." AND because you don't know the value of $c which could be harmful). 首先阅读有关PHP PDOPrepared Statements的知识 :这足以处理/避免SQL注入(查询的问题在这里WHERE wi_iridium_og_device = ". $c ." AND因为您不知道$c ,可能有害)。

About how to rework to improve your source code performance... (this won't be functional, read it as pseudo-code) 关于如何进行返工以提高源代码性能...(这将无法正常工作,请以伪代码形式阅读)

<?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)
    }
}

This should improve your memory footprint, but probably you're also able to improve your queries. 这可以改善您的内存占用量,但也许您也可以改善查询。

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

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