gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 16:08:20 25 4
gpt4 key购买 nike

我的 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 table

最佳答案

而不是 13 选择 max .. from您可以使用单个查询

  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

关于mysql - 在大型 SQL 中运行复杂查询的最快方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55519139/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com