Skip to Content

Archived discussions are read-only. Learn more about SAP Q&A

sql statement too slow

For some reason I have to change code from this:

               LOOP AT GT_SKA1.
			SELECT A~BELNR A~GJAHR A~BUZEI A~BUKRS A~HKONT A~SHKZG A~KOSTL B~HWAER A~DMBTR B~BKTXT A~BLDAT A~BUDAT B~CPUDT B~CPUTM B~USNAM
			FROM BSIS AS A INNER JOIN BKPF AS B ON A~BUKRS = B~BUKRS AND
												   A~GJAHR = B~GJAHR AND
												   A~BELNR = B~BELNR
			APPENDING CORRESPONDING FIELDS OF TABLE GT_GLACC
			WHERE A~HKONT = GT_SKA1-SAKNR AND
				  A~BUKRS IN S_WERKS AND
				  A~BLDAT IN S_BLDAT AND
				  A~BUDAT IN S_BUDAT AND
				  B~CPUDT IN S_CPUDT AND
				  B~USNAM IN S_USNAM AND
				  B~BLART NE 'WA'.
		ENDLOOP.

to this:

			SELECT A~BELNR A~GJAHR A~BUZEI A~BUKRS A~HKONT A~SHKZG A~KOSTL B~HWAER A~DMBTR B~BKTXT A~BLDAT A~BUDAT B~CPUDT B~CPUTM B~USNAM
			FROM BSIS AS A INNER JOIN BKPF AS B ON A~BUKRS = B~BUKRS AND
												   A~GJAHR = B~GJAHR AND
												   A~BELNR = B~BELNR
			APPENDING CORRESPONDING FIELDS OF TABLE GT_GLACC
			WHERE A~HKONT IN S_SAKNR AND
				  A~BUKRS IN S_WERKS AND
				  B~CPUDT IN S_CPUDT AND
				  B~USNAM IN S_USNAM AND
				  B~BLART NE 'WA'.
				  
			SELECT A~BELNR A~GJAHR A~BUZEI A~BUKRS A~HKONT A~SHKZG A~KOSTL B~HWAER A~DMBTR B~BKTXT A~BLDAT A~BUDAT B~CPUDT B~CPUTM B~USNAM
			FROM BSIS AS A INNER JOIN BKPF AS B ON A~BUKRS = B~BUKRS AND
												   A~GJAHR = B~GJAHR AND
												   A~BELNR = B~BELNR
			APPENDING CORRESPONDING FIELDS OF TABLE GT_GLAC2
			WHERE A~HKONT IN S_SAKNR AND
				  A~BUKRS IN S_WERKS AND
				  A~BUDAT IN S_CPUDT AND
				  B~USNAM IN S_USNAM AND
				  B~BLART NE 'WA'.
*delete entries which posting date greater than higher limit of selected period
		LOOP AT GT_GLACC.
			IF GT_GLACC-BUDAT > S_CPUDT-HIGH.
				DELETE GT_GLACC.
			ENDIF.
		ENDLOOP.
*delete entries which entry date greater than lower limit of selected period
		LOOP AT GT_GLAC2.
			IF GT_GLAC2-CPUDT < S_CPUDT-LOW.
				MOVE-CORRESPONDING GT_GLAC2 TO GT_GLACC.
				APPEND GT_GLACC.
			ENDIF.
		ENDLOOP.

However, the resulted code is very slow. The original one only runs around 10 minutes while the resulted one need to be run for 10 hours.

Actually, the sql statement did not change much and I only run two times for it in the new code design.

I want to ask why is it so slow and any solution for this.

Thanks, Experts.

Former Member
Not what you were looking for? View more on this topic or Ask a question