Print Page | Close Window

Improve/redesign slow query

Printed From: Crystal Reports Book
Category: Crystal Reports 9 through 2020
Forum Name: Technical Questions
Forum Discription: Formulas, charting data, Crystal syntax, etc.
URL: http://www.crystalreportsbook.com/forum/forum_posts.asp?TID=22128
Printed Date: 18 May 2024 at 6:51am


Topic: Improve/redesign slow query
Posted By: pbengtss
Subject: Improve/redesign slow query
Date 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!



Replies:
Posted By: pbengtss
Date 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



Print Page | Close Window