gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 00:22:14 25 4
gpt4 key购买 nike

我以为我很擅长这类事情,但是......

我从大约 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

最佳答案

John Green 让我思考信息发送到服务器和从服务器发送出去的方式,这让我找到了问题所在。谢谢约翰。

这就是我修复它的方法:显然,当我的循环迭代时,它正在“打印”8500 个选项行中的每一个,哪个操作(我不知道)是一个服务器- 客户互动。因此,对于每一行,服务器和浏览器都必须进行对话,这会减慢一切。

我通过让服务器在服务器端编译整个 SELECT 框(包括打开的 SELECT 标签)并通过回显我的“添加到”变量 $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;
?>

关于php - 如何在 PHP 中简化这个(相当大的)MySQL 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23145457/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com