简体   繁体   English

如何使我的 SQL 查询更高效,以便我的结果处理不会花费很长时间

[英]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 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.因此,如示例中所示,我们获取所有行并将其打印出来,并且每重置 5 行并创建一个“新页面”。 In reality each page has a table and at the bottom of each we have totals for the cols durationDay and durationNight.实际上,每个页面都有一个表格,在每个页面的底部,我们有列的 durationDay 和 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).如示例中所示,我通常会使用 SUM 和 () 中的数组来查询此总数以获得我想要的总数(这似乎效率很低)。 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.现在可以看到有一个 blankRow 字段,它应该算作一行,即每页最多 5 行,但如果 1 行作为 '2' 空白行,则占用 2 个计数。

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.我的 Live env 中有一些针对上述场景的非常复杂的代码,我想让它更高效,最多 100 行我的代码很好,但是如果有 5000 行我已经计时了大约需要 30 秒才能完全完成。 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".我想知道是否可以修改上面的 SQL,以便它考虑到每个“页面”底部的分页、blankRows 甚至 3 种类型的总数。 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即使每个页面都可以作为它自己的数组或其他东西返回,但我真的不知道,我需要这样做更有效率(我注意到,如果我删除每个页面底部的总计查询,处理时间从 8000 行的 30 秒变为不到 2 秒

Thanks谢谢

SQL 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

<?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.包括要在查询中求和的列并计算运行总和,如上面的 $sumdurday。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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