如何在PHP中简化这个(相当大的)MySQL查询

时间:2014-04-18 00:04:52

标签: php mysql

我认为我在这方面做得很好但是......

我从一个约8,500行的客户表和一个大约相同行数的地址表中得到以下查询。

此查询需要 10秒才能完成 ,我无法确定如何将其设置为毫秒。

我做错了什么?

<select>
<option value=""></option>
<?php // get the customers
    $sql = "
        SELECT `cust`.`custid`, `cust`.`custname`, `cust`.`custactive`, `address`.`addtype`, `address`.`address1`, `address`.`addcity`, `address`.`addstate`
        FROM `cust`
        LEFT Join `address` 
        ON `cust`.`custid` = `address`.`addcustid`
        and `address`.`addtype` = 'b'
        WHERE `cust`.`custactive` = 'y'"
    ;
    $result = mysqli_query($con,$sql) or die('Query failed: Could not get list of CLIENTS: ' . mysqli_error($con)); // query
        while ($row = mysqli_fetch_array($result)) {

                $custid = $row['custid'];
                $space="";
                $endingspaces = 4-(2*strlen($prodid));
                for($count=1; $count<$endingspaces; $count++){
                    $space .="&nbsp;";
                }

                $custid = $row['custid'];
                $custname = substr($row['custname'],0,15);
                $address1 = substr($row['address1'],0,15);
                $addcity = substr($row['addcity'],0,15);
                $addstate = $row['addstate'];

                print "<option value=\"$custid\">$custid: $space$custname, $address1, $addcity, $addstate</option>";                                                
        }
?>
</select>

DDL:

CREATE TABLE `cust` (
  `custid` int(11) DEFAULT NULL,
  `custname` varchar(45) DEFAULT NULL,
  `custactive` varchar(255) DEFAULT NULL,
  `custcreated` varchar(255) DEFAULT NULL,
  `custmodified` varchar(255) DEFAULT NULL,
  `cust_dataease_custid` int(11) DEFAULT NULL,
  `custtype` varchar(5) DEFAULT NULL,
  `custrep` int(11) DEFAULT NULL,
  `custsource` varchar(5) DEFAULT NULL,
  `custdiscount` decimal(65,30) DEFAULT NULL,
  `custrepcomm` varchar(255) DEFAULT NULL,
  `custsurcharge` varchar(255) DEFAULT NULL,
  `custterms` varchar(12) DEFAULT NULL,
  `custups` varchar(255) DEFAULT NULL,
  `custnotes` varchar(255) DEFAULT NULL,
  `custbilldif` varchar(5) DEFAULT NULL,
  UNIQUE KEY `custid` (`custid`),
  KEY `cust_dataease_custid_idx` (`cust_dataease_custid`),
  KEY `custrep_idx` (`custrep`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

CREATE TABLE `address` (
  `addid` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `addcustid` int(11) unsigned NOT NULL,
  `addname` varchar(200) DEFAULT NULL,
  `addtype` enum('b','s') NOT NULL DEFAULT 'b',
  `address1` varchar(150) NOT NULL,
  `address2` varchar(150) DEFAULT NULL,
  `addcity` varchar(150) DEFAULT NULL,
  `addstate` varchar(150) DEFAULT NULL,
  `addstateother` varchar(150) DEFAULT NULL,
  `addzip` varchar(150) DEFAULT NULL,
  `addcountry` varchar(150) DEFAULT NULL,
  `addnote` tinyblob,
  PRIMARY KEY (`addid`),
  KEY `add_cust_id_idx` (`addcustid`)
) ENGINE=InnoDB AUTO_INCREMENT=13037 DEFAULT CHARSET=utf8

1 个答案:

答案 0 :(得分:0)

John Green 让我想到了从服务器发送信息的方式,这让我发现了问题。谢谢约翰。

这是我修复它的方式:显然,当我的循环迭代时,它是&#34;打印&#34;每个8500 OPTION行,哪个动作(我都不知道)是服务器 - 客户端交互。因此,对于每一行,服务器和浏览器都必须进行对话,这会减慢一切。

我通过让服务器在服务器端编译整个SELECT框来修复它,包括打开SELECT标签并通过回显我的&#34; add-to&#34;与浏览器进行交互。变量:$ select_box。

现在需要1.2秒。我可以忍受。

谢谢大家帮我追逐这只兔子。

<?php // get the products
    $select_box = "<select name=\"customer\" id=\"customer\" onChange=\"getcustinfo();\" data-placeholder=\"Choose a Customer\" class=\"chosen-select\" style=\"width:227px;\" tabindex=\"1\">
<option value=\"\"></option>";

    $sql = "
        SELECT *
        FROM `cust`
        LEFT Join `address` 
        ON `cust`.`custid` = `address`.`addcustid`
        and `address`.`addtype` = 'b'
        WHERE `cust`.`custactive` = 'y'"
    ;
    $result = mysqli_query($con,$sql) or die('Query failed: Could not get list of CLIENTS: ' . mysqli_error($con)); // query
        while ($row = mysqli_fetch_array($result)) {
                foreach ($row as $key => $value){ ${$key} = $value; }
                $space="";   
                $custname = substr($row['custname'],0,15);
                $address1 = substr($row['address1'],0,15);
                $addcity = substr($row['addcity'],0,15);

                $select_box .= "<option value=\"$custid\">$custid: $space$custname, $address1, $addcity, $addstate</option>";                                               
        }
    $select_box .="</select>";
    echo $select_box;   
?>