'How to speed up these SQL queries to create charts?
I have these SQL queries and it's took about 0.275 sec to load the entire page. I think it can be faster with some method.
I don't have any indexes on SQL tables (except from ID-s), what do you think it would be a significant difference if I created an index for example to o_date column? Thank you.
Here is my code:
// GET THE LAST MONTH ORDERS
$stmtgetlastmonth = $connpdo->prepare("SELECT count(*) as lastmonthorders FROM customers WHERE DATE(o_date) > (NOW() - INTERVAL :datepicked DAY)");
$stmtgetlastmonth->execute([":datepicked"=>$datepickedchart]);
$resultlastmonth = $stmtgetlastmonth->fetch();
// GET THE NON IMPORTED ORDERS
$stmtgetneworders = $connpdo->query("SELECT count(*) as neworders FROM customers WHERE imported = '2' AND DATE(o_date) > (NOW() - INTERVAL 7 DAY)");
$stmtgetneworders->execute();
$resultneworders = $stmtgetneworders->fetch();
// GET THE MARKED ORDERS
$stmtmarkedorders = $connpdo->query("SELECT count(*) as markedorders FROM customers WHERE imported = '5' AND DATE(o_date) > (NOW() - INTERVAL 32 DAY)");
$stmtmarkedorders->execute();
$resultmarkedorders = $stmtmarkedorders->fetch();
// GET THE SHIPPING ROWS COUNT
$stmtontheway = $connpdo->query("SELECT count(*) as ontheway FROM shipping WHERE delivered = 9 OR delivered = 10");
$stmtontheway->execute();
$resultontheway = $stmtontheway->fetch();
$resultreturnedanddelivered = array();
// GET THE SHIPPING BACK ORDERS
$stmtgetreturned = $connpdo->query("SELECT count(*) as returned FROM shipping_back WHERE DATE(import_date) > (NOW() - INTERVAL 32 DAY)");
$stmtgetreturned->execute();
$getreturnedresults = $stmtgetreturned->fetch();
$resultreturnedanddelivered[] = $getreturnedresults["returned"];
$returnedpercent = $getreturnedresults["returned"];
// GET THE SHIPPING DELIVERED ORDERS
$stmtgetdelivered = $connpdo->query("SELECT count(*) as delivered FROM shipping_delivered WHERE DATE(import_date) > (NOW() - INTERVAL 32 DAY)");
$stmtgetdelivered->execute();
$getdeliveredresults = $stmtgetdelivered->fetch();
$resultreturnedanddelivered[] = $getdeliveredresults["delivered"];
$deliveredpercent = $getdeliveredresults["delivered"];
$percentdlvrd = cal_percentage($returnedpercent,$deliveredpercent);
// GET THE OPSYSTEM COUNT
$stmtgetopsystem = $connpdo->prepare("SELECT opsystem, count(*) as sum FROM customers WHERE DATE(o_date) > (NOW() - INTERVAL :datepicked DAY) GROUP BY opsystem ASC");
$stmtgetopsystem->execute([":datepicked"=>$datepickedchart]);
$opsystem = array();
$opsystemcount = array();
while ( $resultopsystem = $stmtgetopsystem->fetch() ) {
$opsystem[] = $resultopsystem["opsystem"];
$opsystemcount[] = $resultopsystem["sum"];
}
// GET THE UTM COUNT
$stmtgetutmcode = $connpdo->prepare("SELECT utmcode, count(*) as sumutm FROM customers WHERE DATE(o_date) > (NOW() - INTERVAL :datepicked DAY) GROUP BY utmcode ORDER BY sumutm DESC LIMIT 5");
$stmtgetutmcode->execute([":datepicked"=>$datepickedchart]);
$utmcode = array();
$utmcodecount = array();
while ( $resultutmcode = $stmtgetutmcode->fetch() ) {
$utmcode[] = $resultutmcode["utmcode"];
$utmcodecount[] = $resultutmcode["sumutm"];
}
// GET THE FORM ID COUNT
$stmtgetform = $connpdo->prepare("SELECT form, count(*) as sumform FROM customers WHERE DATE(o_date) > (NOW() - INTERVAL :datepicked DAY) GROUP BY form ORDER BY sumform DESC LIMIT 5");
$stmtgetform->execute([":datepicked"=>$datepickedchart]);
$form = array();
$formcount = array();
while ( $resultform = $stmtgetform->fetch() ) {
$form[] = $resultform["form"];
$formcount[] = $resultform["sumform"];
}
// GET THE LAST 30 DAY ORDERS
$stmtgetorders = $connpdo->query("SELECT DATE_SUB(CURDATE(), INTERVAL `seq`.`seq` DAY) AS `date`, COUNT(`c`.`id`) AS `sum` FROM `seq_30_to_0` AS `seq` LEFT JOIN `customers` AS `c` ON (DATE(`c`.`o_date`) = DATE_SUB(CURDATE(), INTERVAL `seq`.`seq` DAY)) GROUP BY `seq`.`seq` ORDER BY `seq`.`seq` DESC");
$stmtgetorders->execute();
$orderdate = array();
$ordercount = array();
while ( $resultgetorders = $stmtgetorders->fetch() ) {
$orderdate[] = $resultgetorders["date"];
$ordercount[] = $resultgetorders["sum"];
}
/// GET THE LAST 30 DAYS PRODUCTS ORDERED
$stmtgetproductordered = $connpdo->prepare("SELECT pp.productname, pp.productprice, COUNT(oi.productid) as productcount FROM products pp INNER JOIN orderitems oi ON pp.productid = oi.productid INNER JOIN orders ON oi.orderid = orders.orderid WHERE DATE(orders.orderdate) > (NOW() - INTERVAL :datepicked DAY) GROUP BY oi.productid LIMIT 6");
$stmtgetproductordered->execute([":datepicked"=>$datepickedchart]);
$productsnameandprice = array();
$productscount = array();
$chartcolor = array();
while ( $resultproductscount = $stmtgetproductordered->fetch() ) {
$chartcolor[] = "#4e73df";
$productnamelenght = strlen($resultproductscount["productname"]) > 32 ? substr($resultproductscount["productname"],0,32)."..." : $resultproductscount["productname"];
$productsnameandprice[] = $productnamelenght." Price: ".$resultproductscount["productprice"];
$productscount[] = $resultproductscount["productcount"];
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
