简体   繁体   中英

Optimize SQL Query In Dynamics AX

SELECT t1.*
FROM CUSTQUOTATIONJOUR t1
WHERE t1.RECID = (SELECT top 1 MAX(t2.RECID)
                  FROM CUSTQUOTATIONJOUR t2
                  WHERE substring(t2.QUOTATIONDOCNUM,1,8) = substring(t1.QUOTATIONDOCNUM,1,8)
                 )

Hi Guys, Please help me optimize this query.

To add to what @Jacob H said in his comment, sometimes optimizing the query means optimizing the table.

Your CustQuotationJour.QuotationDocNum field likely is in a format such as 12345678AA and 12345678BB or something.

If this were truly the only way to compare, then you should add a new column to contain only the 12345678 part you are comparing on and add to an index.

The in AX business logic, when the QuotationDocNum field is populated, you just perform the subStr() to store the 8 characters at that time.

Thanks guys, I managed. I just created two different views. One where I substring the QUOTATIONDOCNUM and then other view which contains the final selection. Looks like this

WITH t2
AS (
    SELECT [QUOTATIONID]
        ,[QUOTATIONDATE]
        ,[SALESID]
        ,[RESPITEDATE]
        ,[ORDERACCOUNT]
        ,[INVOICEACCOUNT]
        ,[CUSTGROUP]
        ,[PURCHASEORDER]
        ,[DELIVERYNAME]
        ,[CUSTOMERREF]
        ,[DLVTERM]
        ,[DLVMODE]
        ,[PAYMENT]
        ,[CASHDISCCODE]
        ,[CASHDISCPERCENT]
        ,[QTY]
        ,row_number() OVER (
            PARTITION BY QUOTATIONDOCNUM ORDER BY recid DESC
            ) AS t1
    FROM [CUSTQUOTATIONJOUR_vw_revised]
    )
SELECT [QUOTATIONID]
    ,[QUOTATIONDATE]
    ,[SALESID]
    ,[RESPITEDATE]
    ,[ORDERACCOUNT]
    ,[INVOICEACCOUNT]
    ,[CUSTGROUP]
    ,[PURCHASEORDER]
    ,[DELIVERYNAME]
    ,[CUSTOMERREF]
    ,[DLVTERM]
    ,[DLVMODE]
    ,[PAYMENT]
    ,[CASHDISCCODE]
    ,[CASHDISCPERCENT]
    ,[QTY]
FROM t2
WHERE t1 = 1;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM