The Truth? I Broke Production. More Than Once.
Back in 2017, I proudly deployed a trigger that worked like magic in the sandbox. But in production? Boom 💥 — “System.LimitException: Too many SOQL queries: 101”.
That day, I learned SOQL isn’t just about writing queries — it’s about writing smart queries.
This blog isn’t a tutorial — it’s a reflection of real-life developer facepalms, learnings, and yes, a few sleepless nights. Here’s what I wish someone had told me earlier.
1. Never Query Inside a Loop – Unless You Enjoy Errors
Real Story:
We had a bulk upload of 200 contacts. I wrote:
for (Contact c : contactList) {
Account a = [SELECT Name FROM Account WHERE Id = :c.AccountId];
}
Everything was fine… until someone uploaded 200 contacts during a live Zoom demo.
💥 “Too many SOQL queries: 101”
My PM stared at me. My coffee went cold. I wanted to disappear.
What I Do Now:
Set<Id> accIds = new Set<Id>();
for (Contact c : contactList) accIds.add(c.AccountId);
Map<Id, Account> accMap = new Map<Id, Account>(
[SELECT Id, Name FROM Account WHERE Id IN :accIds]
);
Now it handles 2 records or 2000 with the same grace — like a seasoned Bollywood actor under pressure.
2. “!=” Is the Silent Query Killer
Error:
SELECT Id FROM Opportunity WHERE StageName != 'Closed Won'
Looks innocent, right? But guess what — “!=” disables index usage. The query dragged like a Monday morning.
My Fix:
SELECT Id FROM Opportunity WHERE StageName IN ('Prospecting', 'Negotiation')
Index-friendly, faster, and now the report loads before the client finishes their tea.
3. Don’t Carry What You Don’t Need
What I Did:
SELECT Id, Name, Phone, Email, CreatedDate, Owner.Name, Industry, Website, Type FROM Account
I only needed Name
and Industry
. Why was I loading the entire kitchen sink?
What I Do Now:
SELECT Name, Industry FROM Account
Lighter queries = faster app = happier users = less grumpy admins.
4. Join Relationships, Don’t Over-Nest
You can go 5 levels deep in SOQL, but should you?
What I Tried:
SELECT Name, (SELECT Subject FROM Tasks), (SELECT CaseNumber FROM Cases) FROM Account
Looks cool in theory… until your query returns half the org’s data.
What’s Cleaner:
SELECT Name, Owner.Name FROM Account
Keep it short. Keep it sweet. Think of your query like a resume — no one wants to read 5 pages.
5. LIMIT Is Your Secret Debug Wand
What Saved Me:
During development, I always test like this:
SELECT Id, Name FROM Contact LIMIT 10
Even with 2 million records, I get instant results. Perfect for testing in sandboxes without raising eyebrows.
6. Always Use the Query Plan Tool (Even if You’re a “Pro”)
I once said, “I’ve written this query a hundred times — it’s fast.”
Then I ran the Query Plan Tool in Developer Console.
My confidence = shattered.
Now I always run queries through Query Plan like a responsible adult.
Signs You Need to Optimize Your SOQL
- Queries taking 5+ seconds in reports
- The governor limits hitting randomly
- Triggers failing in bulk uploads
- Admins are secretly angry at you
- “Salesforce is slow”, complaints from users
- You debug more than you develop
Wrapping Up (and Owning My Mistakes)
I’ve made these mistakes. You’ve probably made some too. And that’s okay — that’s how we grow.
The good news? These small SOQL optimisations can massively improve your app’s performance and your team’s confidence in your code.
Let’s stop blaming Salesforce for slowness and start blaming bad queries 😄.
What’s Your Most Embarrassing SOQL Story?
Let’s turn those bugs into blog-worthy lessons.
Drop your story in the comments or connect with me on LinkedIn — I promise not to laugh (too hard).
Avoid SOQL in loops
Salesforce governor limits
SOQL best practices
Apex SOQL real examples
Salesforce query plan
SOQL for large data
Salesforce trigger optimisation
SOQL indexing tips