Monday, April 26, 2010

Replacing SEARCH with SEARCHALL in cobol

Click Here to Search This Site

Replacing SEARCH with SEARCHALL :

LINEAR search will be used when SEARCH keyword is used to find a particular row of an internal table. Here, every row of the internal table will be searched to check if it satisfies the required condition. Thus it degrades the performance of the job step which executes the SEARCH command. If the size of the internal table is very large, the CPU consumption by the SEARCH command will also be very high.
SEARCH ALL could be the better replacement of SEARCH command as it uses BINARY search for its search operation. Clear idea about Binary search could be found in the link - http://en.wikipedia.org/wiki/Binary_search_algorithm . There are some conditions to be satisfied for replacing the SEARCH logic with SEARCH ALL logic as listed below -
  • To perform SEARCH ALL, the internal table should be in sorted order of a key field
  • The key field should be defined in the declaration of the internal table
  • The search function over the internal table should be performed only through key of the internal table.
  • While mentioning the condition in when clause of the SEARCH ALL command, the internal table key field should be at the left side of " equal to" symbol and the value to be matched with key field should be at right side.
  • Before performing SEARCH ALL logic, high values should be moved to the entire internal table(if key is defined as ascending key in the declaration)
  • Before performing SEARCH ALL logic, low values should be moved to the entire internal table(if key is defined as descending key in the declaration)
  • only one condition can be checked in the when clause of the search all command and it should be based on the key of the internal table
  • If there are duplicate keys exist in an internal table, after positioning the row which has the required key, we can have external if checks to eliminate the rows having duplicate keys.

1 comment:

  1. 1, The keys for a search all should not only be loaded in the proper sequence, but also need to be unique. According to the COBOL manual, duplicate keys can cause unpredictable results


    2. If your table is a variable length table defined with occurs depending on, COBOL is smart enough not to search the unused portion of the table. This saves the time of loading high/low values, and also CPU cycles searching against the unused portion.

    ReplyDelete