How to Improve Query Performance When Retrieving Data from Notes & Invoice History

How to Improve Query Performance When Retrieving Data from Notes & Invoice History

Release Date: 12/12/17

Q - I am trying to get all SHIP notes with related invoice information for a specific customer for a specific day. The speed of my attempts has been okay, but it feels like it could perform much faster. How can the below be improved? Thanks!

SELECT NOTE_FILE_NAME,NOTE_CREATE_DATE,NOTE_TYPE,NOTE_TOPIC,NOTE_CONTENT_1,
NOTE_CONTENT_2,NOTE_CONTENT_3,NOTE_CONTENT_4,NOTE_CONTENT_5,NOTE_CONTENT_6,
NOTE_CONTENT_7,NOTE_CONTENT_8,NOTE_CONTENT_9,NOTE_CONTENT_10,INV_NO,
INV_PURCHASE_ORD_NO,INV_CUSTOMER_NO,INV_BILL_TO_NAME,INV_SHIP_TO_NAME,
INV_SHIP_VIA_CODE,INV_DATE
FROM NOTE_INV_VIEW WHERE
INV_CUSTOMER_NO = '651675' AND
NOTE_TYPE = 'SHIP' AND
NOTE_FILE_NAME = 'CPINVHDR' AND
NOTE_CREATE_DATE = 20171211


A - Try the following the following SQL statement instead:

SELECT NOTE_FILE_NAME, NOTE_CREATE_DATE,NOTE_TYPE,NOTE_TOPIC,NOTE_CONTENT_1,
NOTE_CONTENT_2,NOTE_CONTENT_3,NOTE_CONTENT_4,NOTE_CONTENT_5,NOTE_CONTENT_6,
NOTE_CONTENT_7,NOTE_CONTENT_8,NOTE_CONTENT_9,NOTE_CONTENT_10,INV_NO,
INV_PURCHASE_ORD_NO,INV_CUSTOMER_NO,INV_BILL_TO_NAME,INV_SHIP_TO_NAME,
INV_SHIP_VIA_CODE,INV_DATE
FROM CPINVHDR, NOTES
WHERE INV_CUSTOMER_NO = '651675'
AND NOTE_FILE_NAME = 'CPINVHDR'
AND NOTE_FILE_REF_NO = RIGHT(CONCAT('000000',CONVERT(INV_NO,SQL_CHAR)),6)
AND NOTE_TYPE = 'SHIP'
AND NOTE_CREATE_DATE = 20171211;

I changed the SELECT statement in the following way:

  1. I don't use NOTE_INV_VIEW in this case because In that view we join NOTES (on the left) to CPINVHDR (on the right).  But in this case, the best strategy is to filter on CPINVHDR first before it joins with NOTES.  Therefore, we do not use NOTE_INV_VIEW in this case.
  2. I manually joined CPINVHDR and NOTES in this case by putting CPINVHDR on the left, NOTES on the right. This causes PSQL to process the CPINVHDR table first.
  3. In the where clause, I started with filtering on INV_CUSTOMER_NO, which is part of the alternate index for CPINVHDR, so this will cause PSQL to go through CPINVHDR to find a small subset of records where INV_CUSTOMER_NO = ‘651675’ first before it joins with the NOTES table.

So the lessons from this example are: (A) Sometimes you need to perform your own join instead of using the existing view to get the best performance, and; (B) you need to form a strategy on how to use index to speed up your join.

Note that you can also use the following SQL SELECT statement, but the performance will not be good:

SELECT NOTE_FILE_NAME, NOTE_CREATE_DATE,NOTE_TYPE,NOTE_TOPIC,NOTE_CONTENT_1,
NOTE_CONTENT_2,NOTE_CONTENT_3,NOTE_CONTENT_4,NOTE_CONTENT_5,NOTE_CONTENT_6,
NOTE_CONTENT_7,NOTE_CONTENT_8,NOTE_CONTENT_9,NOTE_CONTENT_10,INV_NO,
INV_PURCHASE_ORD_NO,INV_CUSTOMER_NO,INV_BILL_TO_NAME,INV_SHIP_TO_NAME,
INV_SHIP_VIA_CODE,INV_DATE
FROM CPINVHDR, NOTES
WHERE INV_CUSTOMER_NO = '651675'
AND NOTE_FILE_NAME = 'CPINVHDR'
AND CONVERT(NOTE_FILE_REF_NO,SQL_NUMERIC) = INV_NO
AND NOTE_TYPE = 'SHIP'
AND NOTE_CREATE_DATE = 20171211;

The reason this SELECT statement will not perform as well is because when the system joins from CPINVHDR to NOTES, it will not able to use the NOTE_FILE_REF_NO index due to the "CONVERT" function.  As a result, it has to read through all the CPINVHDR notes and that is much more data to process and thus slows down the procedure.  So the most important takeaway is that you need to understand how to help influence the PSQL relational engine to use the index so it can complete the operation quickly by reading through the smallest number of records.

EMK



    • Related Articles

    • How to Optimize SQL SELECT Statement When Retrieving Data from Invoice History

      'Release Date:11/29/2017 Q - I used the following SQL statement to retrieve invoice history data for a certain item within a certain date range. It took forever to run. Please help. SELECT CPINVLIN.INV_ITM_ITM_NO, CPINVLIN.INV_ITM_INV_DATE, ...
    • Slow PSQL Relational Engine Performance

      Release Date: 12/15/2017 Many users encounter situations where a particular relational engine SQL query can sometimes be slow. This can be complicated to diagnose due to a lot of reasons. Sometimes it's because of a SQL SELECT statement that's not ...
    • How to Retrieve Tracking Number for an Order from Notes

      Release Date: 12/13/2017 Q - Can you let me know how the tracking number is stored in Elliott so I can use query to retrieve the information? A - Regarding how Elliott stores tracking numbers, please refer to the following Knowledge Base article: ...
    • Feature - Added QI (Qty Sold in Invoice History) Data Type to the User Defined CSV Export

      Release date: 10/8/2020 Version: 8.5 & Up Changes have been made to the Reordering Advice User Defined CSV Export programs. You can access this feature from I/M -> Reports -> Reordering Advice Reports -> Maintenance -> User Defined CSV Layout for the ...
    • Feature - Invoice History Archive

      Release Date: 8/23/21 Version: 8.6 and Above The current invoice history data tables CPINVHDR, CPINVLIN, CPINVOPT, CPINVMTL, CPINVRTG, CPBOXFHS (history version of CPBOXFIL), CPBOXHSR (history version of CPBOXSER), CPBOXHST (history version of ...