Php sorgu hızlandırma
Merhaba, aşağıda bulunan kod yapısını sorgu hızı bakımından nasıl kısaltılabilir veya hızlandırılabilir ?
$detail = $this->connect->query("SELECT ES.orderId,ES.totalDiscount,ES.totalPrice,ES.orderDate,ES.PackageStatus,ES.Platform,ES.orderStatus,EC.customerfullName as customerNameSurname,(SELECT count(customerfullName) from entg_sales_customer where customerfullName=customerNameSurname) customerOrderCount FROM entg_sales ES LEFT JOIN entg_sales_customer EC ON ES.orderId = EC.orderId and ES.auth = EC.auth WHERE ES.orderDate BETWEEN '$orderDate' AND '$orderEndDate' AND (ES.auth = '$this->appkey' and ES.orderStatus='$orderStatus') ORDER BY ES.id DESC");
$rowPD = $detail->rowCount();
$orderList = [];
if ($rowPD > 0) {
$detail = $detail->fetchAll(PDO::FETCH_ASSOC);
foreach ($detail as $keys) {
$orderId = $keys["orderId"];
$detail = $this->connect->query("select orderId,packageId,totalDiscount,totalPrice,orderDate,agreedDeliveryDate,Platform,orderStatus from entg_sales where orderId='$orderId' and auth='$this->appkey'")->fetch(PDO::FETCH_ASSOC);
$adress = $this->connect->query("select firstName,lastName,fullName,address,city,district,type,fullAddress from entg_sales_adress where orderId='$orderId' and auth='$this->appkey'")->fetchAll(PDO::FETCH_ASSOC);
$cargoDetail = $this->connect->query("SELECT cargoTrackingNumber,cargoProviderName,cargoPrice FROM entg_sales_cargo where orderId='$orderId' and auth='$this->appkey'")->fetch(PDO::FETCH_ASSOC);
$customerDetail = $this->connect->query("SELECT customerFirstName,customerLastName,customerEmail,customerFullName,tcIdentityNumber,taxOffice,taxId FROM entg_sales_customer where orderId='$orderId' and auth='$this->appkey'")->fetch(PDO::FETCH_ASSOC);
$list = $this->connect->query("SELECT ESD.barcode, ESD.productName, ESD.productCode, ESD.merchantCode, ESD.productSize, ESD.quantity, ESD.amount, ESD.discount, ESD.price, ESD.attributeValue, ESD.Tax, ESD.salesCampaignId, EP.barcode, PM.imageName, P.purchase, P.purchaseTax, P.discount1, P.discount2, P.discount3, P.tax FROM entg_sales_detail ESD LEFT JOIN entg_product EP ON ESD.barcode = EP.barcode AND ESD.auth = EP.auth LEFT JOIN product_images PM ON EP.mainBarcode = PM.barcode AND EP.auth = PM.auth LEFT JOIN product P ON P.barcode = PM.barcode AND P.auth = PM.auth WHERE ESD.orderId = '$orderId' AND ESD.auth = '$this->appkey' GROUP BY EP.mainBarcode")->fetchAll(PDO::FETCH_ASSOC);
$orderJson = [
"detail" => $detail,
"adress" => $adress,
"cargoDetail" => $cargoDetail,
"customerDetail" => $customerDetail,
"list" => $list
];
$orderList[] = $orderJson;
}
}
Aşağıdaki gibi düzeltme sağladım şuanlık sağlıklı çalışıyor sizinde görüşünüzü almak isterim @ebykdrms
$detail = $this->connect->query("SELECT * FROM entg_sales WHERE orderDate BETWEEN '$orderDate' AND '$orderEndDate' AND (auth = '$this->appkey' and orderStatus='$orderStatus') ORDER BY id DESC");
$rowPD = $detail->rowCount();
$orderList = [];
$whereConditions = [];
$whereConditionsWithESD = [];
if ($rowPD > 0) {
$detail = $detail->fetchAll(PDO::FETCH_ASSOC);
foreach ($detail as $keys) {
$whereConditions[] = "(orderId='" . $keys["orderId"] . "' AND auth='" . $this->appkey . "')";
$whereConditionsWithESD[] = "(ESD.orderId='" . $keys["orderId"] . "' AND ESD.auth='" . $this->appkey . "')";
}
$whereCondition = implode(" OR ", $whereConditions);
$whereConditionWithESD = implode(" OR ", $whereConditionsWithESD);
$detail = $this->connect->query("select orderId,packageId,totalDiscount,totalPrice,orderDate,agreedDeliveryDate,Platform,orderStatus from entg_sales WHERE $whereCondition")->fetchAll(PDO::FETCH_ASSOC);
$adress = $this->connect->query("select orderId,firstName,lastName,fullName,address,city,district,type,fullAddress from entg_sales_adress WHERE $whereCondition")->fetchAll(PDO::FETCH_ASSOC);
$cargoDetail = $this->connect->query("SELECT orderId,cargoTrackingNumber,cargoProviderName,cargoPrice FROM entg_sales_cargo WHERE $whereCondition")->fetchAll(PDO::FETCH_ASSOC);
$customerDetail = $this->connect->query("SELECT orderId,customerFirstName,customerLastName,customerEmail,customerFullName,tcIdentityNumber,taxOffice,taxId FROM entg_sales_customer WHERE $whereCondition")->fetchAll(PDO::FETCH_ASSOC);
$list = $this->connect->query("SELECT ESD.orderId,ESD.barcode, ESD.productName, ESD.productCode, ESD.merchantCode, ESD.productSize, ESD.quantity, ESD.amount, ESD.discount, ESD.price, ESD.attributeValue, ESD.Tax, ESD.salesCampaignId, EP.barcode, P.barcode, ( SELECT imageName FROM product_images WHERE barcode = P.barcode ORDER BY imageName DESC LIMIT 1 ) AS imageName, P.purchase, P.purchaseTax, P.discount1, P.discount2, P.discount3, P.tax FROM entg_sales_detail ESD LEFT JOIN entg_product EP ON ESD.barcode = EP.barcode AND ESD.auth = EP.auth LEFT JOIN product_images PM ON EP.mainBarcode = PM.barcode AND EP.auth = PM.auth LEFT JOIN product P ON P.barcode = PM.barcode AND P.auth = PM.auth WHERE $whereConditionWithESD GROUP BY ESD.barcode ORDER BY imageName ASC")->fetchAll(PDO::FETCH_ASSOC);
$orderList = [
"detail" => $detail,
"adress" => $adress,
"cargoDetail" => $cargoDetail,
"customerDetail" => $customerDetail,
"list" => $list
];
$orderListESD = [];
foreach ($orderList["detail"] as $detail) {
$array = [
"detail" => $detail,
"adress" => array(),
"cargoDetail" => [],
"customerDetail" => [],
"list" => array()
];
foreach ($orderList["adress"] as $adress) {
if ($detail["orderId"] == $adress["orderId"]) {
array_push($array["adress"], $adress);
}
}
foreach ($orderList["cargoDetail"] as $cargoDetail) {
if ($detail["orderId"] == $cargoDetail["orderId"]) {
$array["cargoDetail"] = $cargoDetail;
}
}
foreach ($orderList["customerDetail"] as $customerDetail) {
if ($detail["orderId"] == $customerDetail["orderId"]) {
$array["customerDetail"] = $customerDetail;
}
}
foreach ($orderList["list"] as $list) {
if ($detail["orderId"] == $list["orderId"]) {
array_push($array["list"], $list);
}
}
$orderListESD[] = $array;
}
$orderList = $orderListESD;
}