When working with large internal tables in SAP ABAP, choosing the right search technique can make a huge difference in performance. A simple keyword like BINARY SEARCH can reduce search time from thousands of comparisons to just a handful.
Let's understand why.
๐
What is Linear Search?
A Linear Search checks each row one by one until it finds the required record.
๐ How it works
ABAP starts from the first row and compares each record sequentially.
If the target is at the end of the table, every preceding row must be checked first.
❌ Drawbacks
-
Checks every record sequentially
-
Slow for large internal tables
-
Processing time increases as the table grows
๐ Real-Life Analogy
Imagine searching for a person's name in a phone book by reading every page from the beginning until you find it.
It works—but it's slow.
⚡ What is Binary Search?
A Binary Search works only on a sorted internal table.
Instead of checking every row, it repeatedly divides the search space in half, eliminating half of the remaining records with each comparison.
๐ How it works
Instead of starting at row 1:
1️⃣ Check the middle row.
2️⃣ Decide whether the target is in the upper half or lower half.
3️⃣ Ignore the other half completely.
4️⃣ Repeat until the record is found.
Only two comparisons instead of six!
๐ Why is Binary Search Faster?
Because every comparison eliminates 50% of the remaining data.
Instead of searching like this:
It searches like this:
The search space keeps shrinking dramatically.
๐ Performance Comparison
Suppose your internal table contains 100,000 records.
❌ Linear Search
Worst case:
✅ Binary Search
Worst case:
๐ก That's because:
Seventeen steps are enough to cover over 100,000 records.
๐ Complexity Comparison
| Search Method | Time Complexity |
|---|
| Linear Search | O(n) |
| Binary Search | O(log n) |
O(n) → grows linearly with data size.
O(log n) → grows very slowly, making it ideal for large datasets.
⚠️ Important Rule
Binary Search only works correctly on a sorted internal table.
Always sort first:
Skipping the SORT may lead to incorrect or unpredictable results.
๐ก Modern ABAP Best Practice
In modern ABAP, rather than relying on BINARY SEARCH, it's often better to choose the right table type from the beginning.
๐ข SORTED TABLE
-
Automatically keeps data sorted
-
Optimized for key-based searches
-
No need to call
SORT before reading
๐ต HASHED TABLE
-
Uses a hash algorithm instead of searching
-
Extremely fast key lookups
-
No sorting required
-
Best for exact key access
๐ Which Table Type Should You Choose?
| Table Type | Search Method | Best Use Case |
|---|
| Standard Table | Linear Search (or Binary Search after sorting) | General-purpose processing |
| Sorted Table | Binary Search internally | Frequent reads with ordered data |
| Hashed Table | Hash Lookup | Fast, exact key-based access |
๐ Real-Life Analogy
❌ Linear Search
Looking for a chapter in a book by turning every page until you find it.
✅ Binary Search
Opening the book roughly in the middle.
If your chapter comes later, skip the first half.
Open the middle of the remaining pages.
Repeat until you reach the correct chapter.
Much fewer page turns!
๐ฏ Interview Question
❓ Why is BINARY SEARCH faster than a normal READ TABLE?
✅ Answer:
Because it repeatedly divides the sorted table into halves, reducing the number of comparisons from O(n) to O(log n). This makes searches significantly faster for large internal tables.
๐ก Key Takeaways
-
๐ Linear Search checks records one by one—simple but slower as data grows.
-
⚡ Binary Search works on sorted tables, eliminating half the search space with each comparison.
-
๐ Searching 100,000 records may take 100,000 checks with a linear search, but only about 17 checks with a binary search.
-
๐ For modern ABAP development, prefer SORTED TABLE or HASHED TABLE whenever they fit your use case, as they provide better performance and cleaner code than manually sorting a standard table before using
BINARY SEARCH.
Rule of thumb:
๐ Small table? A standard READ TABLE is usually fine.
๐ Large sorted table? Use BINARY SEARCH (or a SORTED TABLE).
⚡ Frequent exact key lookups? A HASHED TABLE is often the fastest choice.