SELECT on Non-Key Fields and Index Usage
- Hilmi Günay
- 5 days ago
- 5 min read
Introduction
One of the most common causes of performance issues in SAP systems is executing SELECT statements on fields that are neither part of the primary key nor supported by an index.

When such queries are executed on large tables, they may lead to:
Long response times
Work process blocking
Background job delays
Overall production system performance degradation
In this article, we will examine step by step:
Why SELECT statements on non-key fields are slow
How the database processes these queries
What a secondary index is and how it works
When an index should be created
The risks of adding indexes
Why Is SELECT on a Non-Key Field Slow?
Example:
SELECT * FROM zsd_orders INTO TABLE @DATA(lt_orders) WHERE carrier_id = lv_carrier.If the field carrier_id is neither part of the primary key nor included in any index, the database has only one option:
It must read every single row in the table.
This is called a Full Table Scan.
If the table has 10,000 records, this may not be noticeable. If it has 10 million records, the system performance can severely degrade.
How Database Indexing Works (Simple Explanation)
Think of an index like the alphabetical index at the end of a book.
No index → You read the entire book from beginning to end
With an index → You directly jump to the relevant page
In SAP tables:
Primary index = Primary key
Secondary index = Additional performance indexes created manually
What Is a Secondary Index?
Indexes created outside of the primary key are called Secondary Indexes.
For example, consider table ZSD_ORDERS:
VBELN (primary key)
CARRIER_ID (frequently queried field)
If we create an index on CARRIER_ID, the database can resolve:
WHERE carrier_id = ...using the index instead of scanning the full table.
Result:
✅ Index scan instead of full table scan✅ Significantly faster SELECT✅ Reduced CPU and I/O consumption
How to Create an Index in SAP
1. Open SE11 and Display the Table

2. Navigate to Indexes
Click the Indexes button in the application toolbar.
This will open the index maintenance screen for the table.

3. Create a New Index
Enter a new index name
For custom developments, index names usually start with: Z01


Note:The primary index (index 0) already exists and should not be modified.
4. Add Fields to the Index
Open the index and add the relevant fields.
Example of a composite index: CARRIER_ID - CREATED_DATE

Field order is critical in composite indexes. The most selective field should come first.
5. Save and Activate
After adding the fields:
Click Save
Click Activate
SAP automatically creates the index at the database level.
When Should You Create an Index?
Not every slow query requires an index.
It makes sense to create one if:
✅ The field is frequently used in WHERE conditions
✅ The table is large
✅ The field has high selectivity (many distinct values)
✅ The program runs frequently in the production system
When Should You Avoid Creating an Index?
Avoid adding an index if:
❌ The table is small
❌ The field is frequently updated
❌ The field has low selectivity (e.g., FLAG = 'X')
❌ The query is part of a one-time report
Because indexes add overhead to:

INSERT
UPDATE
DELETE
operations.
In short:
Reading becomes faster, writing becomes slower.
What If It’s Still Slow Despite the Index?

Sometimes SAP does not use the index even if it exists.
Possible reasons:
Incorrect field order in composite index
Functions applied to fields (UPPER, SUBSTRING, etc.)
LIKE '%ABC' pattern usage
Implicit type conversion
To verify actual execution behavior, use:
ST05 – SQL Trace
Check the execution plan and confirm whether:
INDEX RANGE SCANis used instead of a full table scan.
Real-Life Mini Scenario
In a customer system:
SELECT * FROM zsd_delivery WHERE driver_id = lv_driver.Table size: 18 million recordsdriver_id: no index
Result:
⏱ 28 seconds
After adding a secondary index:
⏱ 0.3 seconds
Conclusion
SELECT statements executed on non-key fields can cause serious performance issues in SAP systems.
A properly designed secondary index:
Accelerates programs
Reduces system load
Improves user experience significantly
However, uncontrolled index creation may negatively impact write performance.
Therefore:
An index is like medicine.In the right dose, it heals. In excess, it harms.
-----------------------------------------------------------------------------------------------------------------------------------------
Key Olmayan Alanlarla SELECT ve Index Kullanımı
Giriş
SAP sistemlerinde performans problemlerinin en yaygın sebeplerinden biri, primary key veya index olmayan alanlar üzerinden SELECT sorguları çalıştırmaktır.
Özellikle büyük tablolarda yapılan bu tür sorgular:

Uzun response sürelerine
Work process bloklanmasına
Background job gecikmelerine
Production system performans düşüşüne sebep olabilir.
Bu makalede:
Key olmayan alanlarla yapılan SELECT neden yavaştır
Database bu sorguları nasıl işler
Secondary index nedir ve nasıl çalışır
Ne zaman index eklenmelidir
Index eklemenin riskleri konularını adım adım inceleyeceğiz.
Key Olmayan Alanla SELECT Neden Yavaştır?
Örnek:
SELECT * FROM zsd_orders INTO TABLE @DATA(lt_orders) WHERE carrier_id = lv_carrier.Burada carrier_id alanı ne primary key ne de index içinde yer alıyorsa database şunu yapmak zorunda kalır:
Tablodaki tüm satırları tek tek okur. Buna Full Table Scan denir. 10.000 kayıt varsa sorun olmaz.10 milyon kayıt varsa sistem diz çöker.
Database Index Mantığı (Basit Anlatım)
Index’i kitabın arkasındaki alfabetik dizin gibi düşünebilirsin.
Index yok → bütün kitabı baştan sona oku
Index var → direkt ilgili sayfaya git
SAP tablolarında:
Primary index = primary key
Secondary index = senin eklediğin performans indexleri
şeklinde çalışır.
Secondary Index Nedir?
Primary key dışında oluşturulan ek indexlere Secondary Index denir.
Örneğin:
ZSD_ORDERS tablosunda:
VBELN (primary key)
CARRIER_ID (sorgulanan alan)
üzerinden bir index oluşturursak:
Database artık:
WHERE carrier_id = ...koşulunu index üzerinden çözebilir.
Sonuç:
Full table scan yerine index scan
Dramatically faster SELECT
Daha az CPU ve I/O
SAP’te Index Nasıl Oluşturulur?
1. SE11 → Tabloyu aç

2. Indexes Butonuna Tıkla
Click the Indexes button in the application toolbar.
This will open the index maintenance screen for the table.

3. Yeni Indeks Oluştur
Yeni bir Indeks ismi vererek Indeks oluştur. Genellikle Z01 tercih edilir.


Not: Primary Index (0) otomatik olarak kaydedilmiştir. Değiştirilmemelidir.
4. Indekse yeni alanlar ekle
İlgili alanları Indekse ekle : CARRIER_ID - CREATED_DATE

Öncelikli alan ilk olarak yazılmalıdır. Indekslerde sıralama önemlidir.
5. Kaydet ve Aktifleştir
Alanları ekledikten sonra kaydet - aktifleştir.
Aktivasyon sonrası database seviyesinde otomatik oluşturulur.
Ne Zaman Index Eklenmeli?
Her yavaş sorguya index eklenmez.
Aşağıdaki şartlar varsa mantıklıdır:
✅ WHERE içinde sık kullanılan alan
✅ Tablo büyük
✅ Selectivity yüksek (çok farklı değer var)
✅ Program production’da yoğun çalışıyor
Ne Zaman Index Eklenmemeli?
❌ Küçük tablolar
❌ Çok sık update edilen alanlar
❌ Düşük selectivity (örnek: FLAG = ‘X’)
❌ Tek seferlik raporlar

Çünkü:
Index:
INSERT
UPDATE
DELETE
işlemlerinde ek yük oluşturur.
Yani okuma hızlanır, yazma yavaşlar.

Index Varken Bile Yavaşsa?
Bazen index olmasına rağmen SAP kullanmaz.
Sebep olabilir:
Field sırası yanlış
Fonksiyon kullanılmış (UPPER, SUBSTRING vs)
LIKE ‘%ABC’ kullanımı
Implicit type conversion
Kontrol etmek için:
ST05 SQL Trace
kullanarak gerçek execution planı görülebilir.
Gerçek Hayattan Mini Senaryo
Bir müşteride:
SELECT * FROM zsd_delivery WHERE driver_id = lv_driver.Tablo: 18 milyon kayıtdriver_id: index yok
Sonuç:
⏱ 28 saniye
Secondary index eklendi:
⏱ 0.3 saniye
Sonuç
Key olmayan alanlarla yapılan SELECT sorguları SAP sistemlerinde ciddi performans problemlerine yol açabilir.
Doğru tasarlanmış bir secondary index:
Programları hızlandırır
Sistem yükünü azaltır
Kullanıcı deneyimini ciddi şekilde iyileştirir
Ancak kontrolsüz index kullanımı da yazma performansını düşürür.
Bu yüzden:
Index bir ilaçtır.Dozunda kullanılırsa şifa, fazlası zehirdir.


Comments