简体   繁体   English

为什么执行SQL查询时我的静态API如此慢? 但是当我只是运行查询时它很快

[英]Why my restful api is so slow while execute my SQL query? but it fast when i just run the query

what i would like to achieve is normal length (below 1 minute) of execution restful api time, what my technology that i am using for front end is using Angularjs and Java Spring MVC and for the back end is Java Spring MVC Restful API. 我想实现的是正常的执行RESTful API时间长度(不到1分钟),我用于前端的技术是使用Angularjs和Java Spring MVC,而用于后端的是Java Spring MVC Restful API。 But it so slow, it takes more than 5 minute for this only one Stored procedure that only return 2 column. 但是它是如此之慢,仅此一个存储过程仅返回2列就花费了5分钟以上。 I dunno what i do wrong, because in my local computer it just work fine, but on the server it run very slow, i have other query that complex than this one and it execute faster than this query, here is my query looked like : 我不知道我做错了什么,因为在我的本地计算机上它运行良好,但是在服务器上运行非常慢,我有其他查询比该查询复杂,并且执行速度比该查询快,这是我的查询:

    [WEB_SP_Get_Summary]

    ALTER PROCEDURE [dbo].[WEB_SP_Get_Summary]
        @Kd_Plg VARCHAR(50)='KLOP001',
        @Kd_Lokasi VARCHAR(3)='WAT'
    AS
    BEGIN

        SELECT CONVERT(VARCHAR(11),TglJthTempo,106) AS TglJthTempo,
        ISNULL(SUM(PHILIP),0) + ISNULL(SUM(KIM),0) + ISNULL(SUM(HOLIC),0) + ISNULL(SUM(OSRAM),0) + ISNULL(SUM(PANASONIC),0) + ISNULL(SUM([MEGAMEN]),0) AS total_next
        FROM 
        dbo.WEB_F_Dashboard_Get_SisaHutang(@Kd_Plg,@Kd_Lokasi)
        WHERE (TglJthTempo>=GETDATE())
        GROUP BY Kd_Plg,TglJthTempo

    END

Here is my table valued function :


ALTER FUNCTION [dbo].[WEB_F_Dashboard_Get_SisaHutang]
(   
    @Kd_Plg VARCHAR(7),
    @Kd_Lokasi VARCHAR(3)
)
RETURNS @SisaHutang TABLE
(
    Kd_Plg VARCHAR(7),
    TglJthTempo DATETIME,
    PHILIP MONEY,
    KIM MONEY,
    HOLIC MONEY,
    OSRAM MONEY,
    PANASONIC MONEY,
    MEGAMEN MONEY
)
AS
BEGIN
    INSERT INTO @SisaHutang(TglJthTempo,Kd_Plg,PHILIP,KIM,HOLIC,OSRAM,PANASONIC,MEGAMEN)
    SELECT Tgl_JatuhTempo, Kd_Plg, PHILIP, KIM, HOLIC, OSRAM, PANASONIC, MEGAMEN FROM 
        (SELECT a.Divisi,SUM(a.Grandtotal-ISNULL(c.TD,0)) AS sisa_faktur
                ,a.Tgl_JatuhTempo,a.Kd_Plg
        FROM ViewGrandtotal a LEFT JOIN (SELECT No_Faktur, SUM(Total_Distribusi) AS TD
        FROM Trx_DetailDistribusi GROUP BY No_faktur) c ON a.No_Faktur=c.No_faktur 
        WHERE a.GrandTotal - ISNULL(c.TD,0) <> 0
        AND a.Cut_Off = 'A' and a.Kd_Trn='J'
        AND a.Kd_Plg = @Kd_Plg
        AND a.kd_lokasi = @Kd_Lokasi
        GROUP BY a.Divisi, a.Kd_Plg,a.Tgl_JatuhTempo) s
        PIVOT
        (SUM(sisa_faktur)
            FOR Divisi IN (PHILIP,KIM,HOLIC,OSRAM,PANASONIC,MEGAMEN)) AS pvt
RETURN 
END

is there something wrong with my query? 我的查询有问题吗?

and here is how i do request in my java application : 这是我在Java应用程序中的要求方式:

public List<DashboardPiutangAkanDatang> getDashboardPiutangAkanDatang(String Kd_Plg, String Kd_Lokasi) throws DataAccessException {

    Map<String, Object> params = new HashMap<String, Object>();
       params.put("Kd_Plg", Kd_Plg);
       params.put("Kd_Lokasi", Kd_Lokasi);

    ArrayList<DashboardPiutangAkanDatang> dashboard = (ArrayList<DashboardPiutangAkanDatang>) this.namedParameterJdbcTemplate.query
            ("EXEC WEB_SP_Get_Summary :Kd_Plg, :Kd_Lokasi", params, BeanPropertyRowMapper.newInstance(DashboardPiutangAkanDatang.class));  
    return dashboard;

}

(too long for comment) (评论太久了)

The code smells: 该代码闻起来:

  • unnecessary encapsulation into TVF which may and I'm sure does cause some performance degradation; 不必要地封装到TVF中,这可能并且我确定确实会导致性能下降; get rid of TVF, run updated query at prod server and show actual execution plan 摆脱TVF,在生产服务器上运行更新的查询并显示实际执行计划
  • GROUP BY , GROUP BY , GROUP BY ,.. which is very suspicious; GROUP BYGROUP BYGROUP BY ,..这是非常可疑的;
  • LEFT JOIN (SELECT No_Faktur, SUM(Total_Distribusi) AS TD FROM Trx_DetailDistribusi GROUP BY No_faktur) will cause entire scan of Trx_DetailDistribusi whereas you're applying several filters to the left table ViewGrandtotal which means OUTER APPLY or even simple LEFT JOIN could perform much better LEFT JOIN (SELECT No_Faktur, SUM(Total_Distribusi) AS TD FROM Trx_DetailDistribusi GROUP BY No_faktur)将导致整个扫描Trx_DetailDistribusi而你申请几个过滤器,以ViewGrandtotal这意味着OUTER APPLY甚至简单的LEFT JOIN可能性能要好得多
  • ViewGrandtotal sounds suspicious - view and total are hinting that something is going on inside it and may be questioned as well ViewGrandtotal听起来可疑- viewtotal被暗示的东西是它里面怎么回事,可能会受到质疑,以及
  • WHERE a.GrandTotal - ISNULL(c.TD,0) <> 0 I don't like it very much WHERE a.GrandTotal - ISNULL(c.TD,0) <> 0我不太喜欢
  • pivoting and collapsing all the columns into single value right after that? 之后将所有列都旋转并折叠为单个值? what is there pivot for? 枢轴有什么用?
  • SQL is not JAVA, such encapsulation and code "reusability" are killing it SQL不是JAVA,这种封装和代码“可重用性”正在杀死它
  • compare server and local table structs, list of indexes 比较服务器和本地表结构,索引列表
  • by the way, is db content equal? 顺便说一句,数据库内容相等吗? or localhost db has let's say 10 records whereas on server there are millions? 还是localhost db拥有10条记录,而服务器上有数百万条记录?

If same query giving different performance then something is wrong with your execution plan. 如果相同的查询提供不同的性能,则您的执行计划有问题。 when you are executing procedure from your Local SSMS, it uses your SSMS SET Properties. 从本地SSMS执行过程时,它会使用SSMS SET属性。 ie SET ARITH_ABORT ON, ... which doesn't in API request. 即SET ARITH_ABORT ON,...不在API请求中。

Please read below article and make required changes in your procedure. 请阅读以下文章,并在您的程序中进行必要的更改。

https://www.mssqltips.com/sqlservertip/4318/sql-server-stored-procedure-runs-fast-in-ssms-and-slow-in-application/ https://www.mssqltips.com/sqlservertip/4318/sql-server-stored-procedure-runs-fast-in-ssms-and-slow-in-application/

暂无
暂无

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

相关问题 在Oracle SQL Developer中运行的SQL查询但是当我从我的JAVA程序运行查询时,它给出了Sql异常 - Sql Query running in oracle SQL Developer But when i run the query from my JAVA program it is giving Sql exception 为什么我的SQL查询失败? - Why is my SQL query failing? 当我单击开始(框架中的按钮)并减慢它以使循环不快时如何启动我的程序? - How to start my program when I click start (button in frame) and slow it down so the loop isn't to fast? sql查询在Hibernate中很慢,在mysql上快 - sql query very slow in Hibernate, fast on mysql Hibernate-SQL很快,但是查询仍然很慢 - Hibernate - SQL is fast, but query is still slow 当我尝试破坏活动时,不会留下任何痕迹,为什么我的程序在运行时会退出? - When i try to destroy the activity so no traces will be stay my program just quit when running it why? 如果我通过phpMyAdmin执行查询,则它在我的Java程序中不存在时仍然有效 - If I execute my query through phpMyAdmin it works while it doesn't in my java program 为什么当我将国际象棋项目作为一个可运行的jar文件运行时,我得到一个奇怪的行为,而当我在eclipse中运行它时一切正常? - Why when I run my chess project as a runnable jar file I get a strange behavior while when I run it inside eclipse everything works just fine? 为什么在SQL查询中找不到列? - Why am I getting column not found in my SQL query? 为什么这个Clojure程序这么慢?如何让它快速运行? - Why is this Clojure program so slow? How to make it run fast?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM