Technical Questions
 Crystal Reports Forum : Crystal Reports 9 through 2020 : Technical Questions
Message Icon Topic: Improve/redesign slow query Post Reply Post New Topic
Author Message
pbengtss
Newbie
Newbie
Avatar

Joined: 18 May 2016
Online Status: Offline
Posts: 22
Quote pbengtss Replybullet Topic: Improve/redesign slow query
    Posted: 02 Nov 2016 at 12:55am
I could really need some help here. Is there a way to redesign my query in order to improve the performance? When I run it in Excel (MS Query) it takes about 4 minutes and in CR it takes between 5 minutes and eternity, sometimes CR stops responding completely.

I'm working with 3 tables and a stored procedure.

TABLES
ARTICLE
art_id
art_artnr
art_status

ARTICLE_EXTRA
art_id
ae_string_5

ARTICLE_STOCKLOCATION
art_id
lp_stock


STORED PROCEDURE
Myodbc.SP_Get_Transactions
The stored procedure looks like this:

ALTER PROCEDURE "Myodbc"."SP_Get_Transactions"(

     IN as_artnr NVARCHAR(16),
     IN al_art_id INTEGER
)
RESULT (
     artnr NVARCHAR(16),
     date DATETIME,
     transtype INTEGER,
     ordered DOUBLE,
     reserved DOUBLE,
     stock DOUBLE
     )
BEGIN
...



QUERY
This is the query that I use in CR via Add Command:
SELECT

ARTICLE.art_artnr,
Transactions.stock + SUM(Transactions.ordered-Transactions.reserved) OVER (PARTITION BY Transactions.artnr ORDER BY Transactions.date ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) as available_stock,
Transactions.date,
Transactions.transtype,
Transactions.stock,
Transactions.ordered,
Transactions.reserved

FROM
MyDB.ARTICLE ARTICLE
LEFT OUTER JOIN MyDB.ARTICLE_EXTRA ARTICLE_EXTRA ON ARTICLE_EXTRA.art_id=ARTICLE.art_id
LEFT OUTER JOIN MyDB.ARTICLE_STOCKLOCATION ARTICLE_STOCKLOCATION ON ARTICLE_STOCKLOCATION.art_id=ARTICLE.art_id
CROSS APPLY Myodbc.SP_Get_Transactions(ARTICLE.art_artnr, ARTICLE.art_id) as Transactions

WHERE
ARTICLE.art_artnr IN (
    SELECT
        TRANSX.artnr
    FROM
        Myodbc.SP_Get_Transactions(ARTICLE.art_artnr, ARTICLE.art_id) TRANSX
    WHERE
        TRANSX.date <= CURRENT DATE
        AND TRANSX.transtype NOT IN (2, 3)
    GROUP BY
        TRANSX.artnr
    HAVING
        SUM(TRANSX.reserved) > ARTICLE_STOCKLOCATION.lp_stock
)

AND ARTICLE.art_status BETWEEN 4 AND 6
AND Transactions.date <= CURRENT DATE
AND Transactions.transtype NOT IN (2, 3)
AND (ARTIKEL_EXTRA.ae_string_5 IS NULL OR ARTIKEL_EXTRA.ae_string_5<>'UTGÅTT')



What I'm doing:
My report should show all articles that have a higher demand than the current stock.
Is my query "OK"?
The stored procedure is by nature pretty slow, it contains I think 24 subqueries (1300+ rows of code). But other than that - is there anything fundamentally wrong? I'm not super confident with joins and CROSS APPLY...

Please let me know if you need more information!

Edited by pbengtss - 02 Nov 2016 at 10:27pm
IP IP Logged
pbengtss
Newbie
Newbie
Avatar

Joined: 18 May 2016
Online Status: Offline
Posts: 22
Quote pbengtss Replybullet Posted: 02 Nov 2016 at 1:33am
I can also add some information about the tables:

ARTICLE contains 51000 rows. After my conditions (not all are included in above query) at the end of the query it's reduced to 2000. 64 columns.

ARTICLE_STOCKLOCATION also contains 51000 rows. 31 columns.

ARTICLE_EXTRA contains 17000 rows. 15 columns.

The stored procedure typically returns 2-10 rows for each article


Edited by pbengtss - 02 Nov 2016 at 1:35am
IP IP Logged
Post Reply Post New Topic
Printable version Printable version

Forum Jump
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot delete your posts in this forum
You cannot edit your posts in this forum
You cannot create polls in this forum
You cannot vote in polls in this forum



This page was generated in 0.031 seconds.