简体   繁体   中英

How can I make my SQL queries more efficient so that my processing of results does not take a long time

I have the following basic example:

https://phpize.online/?phpses=045f282ad521841c60cb77150ba7f20d&sqlses=a090bcc3e9d17e4e1dda56920d64765b&php_version=php7&sql_version=mysql57

So as seen in the example we get all rows and print them out and every 5 rows we reset and a "new page" is created. In reality each page has a table and at the bottom of each we have totals for the cols durationDay and durationNight. So a row for totals this page, totals previous page and grand totals. As seen in the example I would usually query this totals using SUM and array in () to get the totals I want (Which seems really inefficient). Now as seen there is a blankRow field which should count as a row ie max 5 rows per page but if 1 row as '2' blank rows then 2 counts are taken up.

I have some very overly complicated code for the above scenario in my Live env and I want to make it more efficient and for up to 100 rows my code is fine but if there is like 5000 rows I have timed it takes around 30 seconds to fully complete so. I am wondering if the SQL above can be modified so that it takes into account the pagination, blankRows and even the 3 types of totals at the bottom of each "page". Even if each page could maybe be returned as its own array or something but I don't really know and I need this to be a lot more efficient (I notices that if I remove the totals queries at the bottom of each page the processing time goes from 30 second for 8000 rows to under 2 seconds

Thanks

SQL

create table trip (
    date Date,
    goid varchar(255),
    backid varchar(255),
    vehicleId int, 
    durationDay int, 
    durationNight int,
    blankRow int
);

create table vehicle (
vehicleId int,
color varchar(255), 
name varchar(255)
);

insert into trip (date, goid, backid, vehicleId, durationDay, durationNight, blankRow) values ('2020-09-20', 'GO1', 'DAC', 22, 2, 3, 0);
insert into trip (date, goid, backid, vehicleId, durationDay, durationNight, blankRow) values ('2020-09-21', 'DAC', 'GO1', 22, 3, 4, 0);
insert into trip (date, goid, backid, vehicleId, durationDay, durationNight, blankRow) values ('2020-09-09', 'DAC', 'GO1', 33, 4, 3, 2);
insert into trip (date, goid, backid, vehicleId, durationDay, durationNight, blankRow) values ('2020-09-22', 'GO1', 'DAC', 22, 4, 4, 0);
insert into trip (date, goid, backid, vehicleId, durationDay, durationNight, blankRow) values ('2020-09-12', 'GO1', 'GO1', 33, 3, 3, 0);
insert into trip (date, goid, backid, vehicleId, durationDay, durationNight, blankRow) values ('2020-09-25', 'DAC', 'GO1', 22, 4, 4, 0);
insert into trip (date, goid, backid, vehicleId, durationDay, durationNight, blankRow) values ('2020-09-22', 'GO1', 'DAC', 22, 4, 4, 0);
insert into trip (date, goid, backid, vehicleId, durationDay, durationNight, blankRow) values ('2020-09-12', 'GO1', 'GO1', 33, 3, 3, 0);
insert into trip (date, goid, backid, vehicleId, durationDay, durationNight, blankRow) values ('2020-09-25', 'DAC', 'GO1', 22, 4, 4, 0);
insert into trip (date, goid, backid, vehicleId, durationDay, durationNight, blankRow) values ('2020-09-22', 'GO1', 'DAC', 22, 4, 4, 0);
insert into trip (date, goid, backid, vehicleId, durationDay, durationNight, blankRow) values ('2020-09-12', 'GO1', 'GO1', 33, 3, 3, 0);
insert into trip (date, goid, backid, vehicleId, durationDay, durationNight, blankRow) values ('2020-09-25', 'DAC', 'GO1', 22, 4, 4, 0);
insert into trip (date, goid, backid, vehicleId, durationDay, durationNight, blankRow) values ('2020-09-22', 'GO1', 'DAC', 22, 4, 4, 0);
insert into trip (date, goid, backid, vehicleId, durationDay, durationNight, blankRow) values ('2020-09-12', 'GO1', 'GO1', 33, 3, 3, 0);
insert into trip (date, goid, backid, vehicleId, durationDay, durationNight, blankRow) values ('2020-09-25', 'DAC', 'GO1', 22, 4, 4, 0);
insert into trip (date, goid, backid, vehicleId, durationDay, durationNight, blankRow) values ('2020-09-22', 'GO1', 'DAC', 22, 4, 4, 0);
insert into trip (date, goid, backid, vehicleId, durationDay, durationNight, blankRow) values ('2020-09-12', 'GO1', 'GO1', 33, 3, 3, 0);
insert into trip (date, goid, backid, vehicleId, durationDay, durationNight, blankRow) values ('2020-09-25', 'DAC', 'GO1', 22, 4, 4, 0);
insert into trip (date, goid, backid, vehicleId, durationDay, durationNight, blankRow) values ('2020-09-22', 'GO1', 'DAC', 22, 4, 4, 0);
insert into trip (date, goid, backid, vehicleId, durationDay, durationNight, blankRow) values ('2020-09-12', 'GO1', 'GO1', 33, 3, 3, 0);
insert into trip (date, goid, backid, vehicleId, durationDay, durationNight, blankRow) values ('2020-09-25', 'DAC', 'GO1', 22, 4, 4, 0);



insert into vehicle (vehicleId, color, name) values (22, 'Red', 'vehicle1');
insert into vehicle (vehicleId, color, name) values (33, 'Green', 'vehicle2');


SELECT trip.*, vehicle.* FROM trip, vehicle WHERE vehicle.vehicleId=trip.vehicleId ORDER BY date ASC

PHP

<?php
$query = "SELECT trip.*, vehicle.* FROM trip, vehicle WHERE vehicle.vehicleId=trip.vehicleId ORDER BY date ASC";
$stmt = $pdo->prepare($query);
$stmt->execute();

$count = 0;
$tripIdArray = [];
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    $count++;
    print_r($row["name"] . " + other data \n");

    if ($count == 5) {
        print_r("Select Sum query prev page total using another array \n");
        print_r("Select Sum query grand total using tripIdArray \n");
        $count = 0;
        
        print_r("New Page -------------------------- \n");

    }
}

$count = 0;
$sumdurday = 0.0;
$tripIdArray = [];
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
  $count++;
  $sumdurday += $row["durationDay"];
  print_r($row["name"] . " + other data \n");

  if ($count == 5) {
    print_r("Select Sum query prev page total using another array \n");
    print_r("Select Sum query grand total using tripIdArray \n");
    
    print_r("sumdurday = ");
    print_r($sumdurday);
    print_r("\n");
    $sumdurday = 0.0;
    $count = 0;
    
    
    
    print_r("New Page -------------------------- \n");

  }
}

No need to query again for the sums. Include columns you want to sum in the query and calculate running sums like $sumdurday above.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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