This piece of ABAP code should run through multiple tables and run a COUNT in all of them. The code runs through multiple tables to perform a count for a given ID. the ID is given by the user and then the program would look through all of the tables to check how many records each table has, that also has that ID. This is then returned to the user in an ALV with the table ID, the table Name and the number of records. 

The program is run an pretty large sized tables and takes about 4 minutes to finish. Since I am fairly new with ABAP and OpenSQL I was wondering how this query could be improved to boost performance. I tried first checking if the ID exists in the table with SELECT SINGLE, and if it exists launch the count but that only made it slower with about a minute. 

LOOP AT lt_where_used_data_of_coll ASSIGNING &lt;fs_curr_tab&gt;.

  SELECT ddtext AS table_description
  FROM dd02t
  INTO &lt;fs_curr_tab&gt;-table_description
  WHERE tabname = &lt;fs_curr_tab&gt;-table_id AND ddlanguage = 'EN'.
  ENDSELECT.

  lv_curr_table_name = &lt;fs_curr_tab&gt;-table_id.

        SELECT COUNT(*) AS nr_of_records
        FROM (lv_curr_table_name)
        INTO &lt;fs_curr_tab&gt;-nr_of_records
        WHERE id = p_c_id.
ENDLOOP.


The internal table in the program that is being filled already has the ID's of the tables to search through. The first SELECT inside the loop gets the name of the table corresponding with the ID. The second SELECT statement goes through all those tables and counts the number of records that contain the ID given by the user.

How can this statement be improved to run faster?
Your first query will only keep 1 value for &lt;fs_curr_tab&gt;-table_description so you might as well go for 

  SELECT SINGLE ddtext AS table_description
  FROM dd02t
  INTO &lt;fs_curr_tab&gt;-table_description
  WHERE tabname = &lt;fs_curr_tab&gt;-table_id AND ddlanguage = 'EN'.


The AS table_description is not needed since you are not using CORRESPONDING, I also do not like the hardcoded language, I would take the user language. I also prefer EQ over =

  SELECT SINGLE ddtext
  FROM dd02t
  INTO &lt;fs_curr_tab&gt;-table_description
  WHERE tabname EQ &lt;fs_curr_tab&gt;-table_id 
    AND ddlanguage EQ sy-langu.


If this is called with a large amount of tables (say over 50), then you should definitely consider reading all the table names at once with FOR ALL ENTRIES IN before the loop.

Finally, this could go horribly wrong if that table does not have an 'ID' column. If it were me I would check first the DD03L table to make sure the table does have an ID column. If I am not mistaken, DBIF_RSQL_INVALID_REQUEST is a non-catchable exception and would send the program straight into an abend.