首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >mysql执行查询的低性能

mysql执行查询的低性能
EN

Stack Overflow用户
提问于 2016-02-16 15:26:01
回答 1查看 44关注 0票数 0

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

代码语言:javascript
复制
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

我怎样才能做到这一点?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-02-16 15:59:24

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

代码语言:javascript
复制
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 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35436432

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档