function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Kirill_YunussovKirill_Yunussov 

SOQL - Any point to use "Limit 1" if query for specific ID?

Query optimization: if you add "Limit 1", it will stop searching after finding the first match.   Now, if you specify an ID in the WHERE clause, then the search will already be quick, since ID is indexed.

 

Question: what is the performance gain of using "LIMIT 1" when quering for record with a specific ID?   Would it be a best practice?

Kirill_YunussovKirill_Yunussov

tested running the same query 100 times, pulling an Opportunity by ID, only pulling the ID.   One problem with these numbers is that after the first query the search results are probably stored in the cache, so this does not really show accurate performance numbers, except the first time of 493 ms..

 

by id only
493 ms
395 ms
378 ms


by id and with "limit 1"
427 ms
365 ms
368 ms
414 ms