在大型SQL中运行复杂查询的最快方法

时间:2019-04-04 14:58:37

标签: mysql phpmyadmin

我的SQL数据包含两个名为buletinpasarbubar的表。 我想使用此查询获取数据:

SELECT
    b.date_time,
    b.Type,
    b.duration,
    (SELECT MAX(`0`) FROM rsam_bpptkg.pasarbubar e
     WHERE e.ss_time BETWEEN b.date_time AND DATE_ADD(b.date_time, INTERVAL 10 SECOND)) AS f0_max,
    (SELECT MAX(`1`) FROM rsam_bpptkg.pasarbubar e
     WHERE e.ss_time BETWEEN b.date_time AND DATE_ADD(b.date_time, INTERVAL 10 SECOND)) AS f1_max,
     (SELECT MAX(`2`) FROM rsam_bpptkg.pasarbubar e
     WHERE e.ss_time BETWEEN b.date_time AND DATE_ADD(b.date_time, INTERVAL 10 SECOND)) AS f2_max,
     (SELECT MAX(`3`) FROM rsam_bpptkg.pasarbubar e
     WHERE e.ss_time BETWEEN b.date_time AND DATE_ADD(b.date_time, INTERVAL 10 SECOND)) AS f3_max,
     (SELECT MAX(`4`) FROM rsam_bpptkg.pasarbubar e
     WHERE e.ss_time BETWEEN b.date_time AND DATE_ADD(b.date_time, INTERVAL 10 SECOND)) AS f4_max,
     (SELECT MAX(`5`) FROM rsam_bpptkg.pasarbubar e
     WHERE e.ss_time BETWEEN b.date_time AND DATE_ADD(b.date_time, INTERVAL 10 SECOND)) AS f5_max,
     (SELECT MAX(`6`) FROM rsam_bpptkg.pasarbubar e
     WHERE e.ss_time BETWEEN b.date_time AND DATE_ADD(b.date_time, INTERVAL 10 SECOND)) AS f6_max,
     (SELECT MAX(`7`) FROM rsam_bpptkg.pasarbubar e
     WHERE e.ss_time BETWEEN b.date_time AND DATE_ADD(b.date_time, INTERVAL 10 SECOND)) AS f7_max,
     (SELECT MAX(`8`) FROM rsam_bpptkg.pasarbubar e
     WHERE e.ss_time BETWEEN b.date_time AND DATE_ADD(b.date_time, INTERVAL 10 SECOND)) AS f8_max,
     (SELECT MAX(`9`) FROM rsam_bpptkg.pasarbubar e
     WHERE e.ss_time BETWEEN b.date_time AND DATE_ADD(b.date_time, INTERVAL 10 SECOND)) AS f9_max,
     (SELECT MAX(`10`) FROM rsam_bpptkg.pasarbubar e
     WHERE e.ss_time BETWEEN b.date_time AND DATE_ADD(b.date_time, INTERVAL 10 SECOND)) AS f10_max,
     (SELECT MAX(`11`) FROM rsam_bpptkg.pasarbubar e
     WHERE e.ss_time BETWEEN b.date_time AND DATE_ADD(b.date_time, INTERVAL 10 SECOND)) AS f11_max,
     (SELECT MAX(`12`) FROM rsam_bpptkg.pasarbubar e
     WHERE e.ss_time BETWEEN b.date_time AND DATE_ADD(b.date_time, INTERVAL 10 SECOND)) AS f12_max,
     (SELECT MAX(`13`) FROM rsam_bpptkg.pasarbubar e
     WHERE e.ss_time BETWEEN b.date_time AND DATE_ADD(b.date_time, INTERVAL 10 SECOND)) AS f13_max
FROM bul.buletin b

这次我通常使用phpmyadmin运行该查询,以获取25行数据需要1个小时。即使我必须获取的总数据多达3000行数据。也许任何人都可以帮助我修改查询或在pythonphp脚本中提出建议。

*注意:这里是我的数据示例

帕萨布巴桌子 pasarbubar table

Buletin表

buletin table

1 个答案:

答案 0 :(得分:0)

从13中选择最大.. 您可以使用一个查询

  SELECT MAX(`0`) f0_max
    , MAX(`1`) f1_max
    , MAX(`2`) f2_max
    , MAX(`3`) f3_max
    , MAX(`4`) f4_max
    , MAX(`5`) f5_max
    , MAX(`6`) f6_max
    , MAX(`7`) f7_max
    , MAX(`8`) f8_max
    , MAX(`9`) f9_max
    , MAX(`10`) f0_max
    , MAX(`11`) f11_max
    , MAX(`12`) f12_max
    , MAX(`13`) f13_max  
  FROM rsam_bpptkg.pasarbubar e
  INNER JOIN bul.buletin b ON  e.ss_time BETWEEN b.date_time 
        AND DATE_ADD(b.date_time, INTERVAL 10 SECOND)

并确保您有正确的索引

 table rsam_bpptkg.pasarbubar column ss_time 

 table  bul.buletin on clumn date_time