gpt4 book ai didi

mysql执行查询性能低下

转载 作者:行者123 更新时间:2023-11-29 21:27:44 25 4
gpt4 key购买 nike

我想优化下面的 SQL 查询,目标是在 3-4 秒内获得结果。

select(
((select data from t_sayac_degerler where id = (select max(id) from t_sayac_degerler where adres = '1.8.0' and tarih between '2016.02.15 11:00' and '2016.02.16 17:13'))
-
(select data from t_sayac_degerler where id = (select min(id) from t_sayac_degerler where adres = '1.8.0' and tarih between '2016.02.15 11:00' and '2016.02.16 17:13')))
*
(select multiplier from t_sayac where s_id = ((select s_id from t_sayac_degerler where id = (select max(id) from t_sayac_degerler where adres = '1.8.0' and tarih between '2016.02.15 11:00' and '2016.02.16 17:13'))))
)
as value

我怎样才能实现这个目标?

最佳答案

我只是好奇这是否会提高您的性能,通过删除嵌套的 Select 语句,我将删除嵌套循环。我假设 id 值是 int:

DECLARE @MaxID int, @MinID int, @s_id int;


SELECT @MaxID=max(id) , @MinID = min(id)
FROM t_sayac_degerler
WHERE adres = '1.8.0' and tarih between '2016.02.15 11:00' and '2016.02.16 17:13';

SELECT @s_id=s_id
FROM t_sayac_degerler
WHERE id = @MaxID


SELECT(
(
(SELECT data
FROM t_sayac_degerler
WHERE id = @MaxID)
-
(SELECT data
FROM t_sayac_degerler
WHERE id = @MinID)
)
*
(SELECT multiplier
FROM t_sayac
WHERE s_id = @s_id
)
)
as value

关于mysql执行查询性能低下,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35436432/

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