‎2013 Oct 24 2:49 PM
Hi experts, can you give me any advice on optimizing the two SQL queries; thanks in advance. This is part of a program which is normally run in background but has to be run in foreground.
1st SQL query:
SELECT a~mblnr
a~mjahr
a~budat
b~bwart
b~matnr
b~werks
b~menge
b~meins
INTO TABLE i_mseg
FROM mkpf AS a INNER JOIN mseg AS b ON a~mblnr = b~mblnr
AND a~mjahr = b~mjahr
FOR ALL ENTRIES IN i_semit_cost
WHERE a~budat BETWEEN ws_d_first AND ws_d_last
AND b~bwart IN r_bwart
AND b~matnr = i_semit_cost-matnr_head
AND b~werks = i_semit_cost-werks.
2nd SQL query:
SELECT a~mblnr
a~mjahr
a~budat
b~bwart
b~matnr
b~werks
b~menge
b~meins
INTO TABLE i_mseg_ppv
FROM mkpf AS a INNER JOIN mseg AS b ON a~mblnr = b~mblnr
AND a~mjahr = b~mjahr
INNER JOIN ekko AS c ON b~ebeln = c~ebeln
FOR ALL ENTRIES IN i_semit_cost
WHERE a~budat BETWEEN ws_d_first AND ws_d_last
AND b~bwart IN r_bwart_ppv
AND b~matnr = i_semit_cost-matnr_head
AND b~werks = i_semit_cost-bwkey
AND c~reswk = p_werks.
‎2013 Oct 28 9:03 AM
Hi Jack,
If tables that you are using have already millions of records it may be difficult to optimize query such that it can run in the foreground.
Usually it is not recommended to mix JOINs and FOR ALL ENTRIES into same statement due to low performance results. In addition you do join 2 tables in first example and 3 tables in second one. So in fact if you add FOR ALL ENTRIES it is like join on 3 and 4 tables correspondingly and this will be time consuming.
You should try different approaches, do measurements in ST05 database logs and finally choose best performing solution. There are many factors like data distribution and indexes that will influence your query performance.
Some recommendations from me to try:
Regards,
Adam
‎2013 Oct 28 9:03 AM
Hi Jack,
If tables that you are using have already millions of records it may be difficult to optimize query such that it can run in the foreground.
Usually it is not recommended to mix JOINs and FOR ALL ENTRIES into same statement due to low performance results. In addition you do join 2 tables in first example and 3 tables in second one. So in fact if you add FOR ALL ENTRIES it is like join on 3 and 4 tables correspondingly and this will be time consuming.
You should try different approaches, do measurements in ST05 database logs and finally choose best performing solution. There are many factors like data distribution and indexes that will influence your query performance.
Some recommendations from me to try:
Regards,
Adam
‎2013 Nov 04 6:09 AM
Also note that in most cases INNER JOIN performs better than FOR ALL ENTRIES. So you should try INNER JOIN first.
‎2013 Nov 07 4:12 PM
Hello Jack,
well, there is actually only one ultimate solution for your problem. And this solution is described in the SAP note 1550000. Read it carefully. If you decide to go for it (we've got a couple of very positive feedbacks from our customers), you'll also have to slightly modify the query in order to use new fields from MSEG instead of MKPF.
Regards,
Yuri