- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-26-2022 08:38 AM
If I set the limit on query at 1 result with gr.getLimit(1), will it be faster than without limit?
Edit: is should be setLimit(1) that was a typo
Solved! Go to Solution.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-26-2022 04:03 PM
As others have replied, it's setLimit().
In short, not always. Setting .setLimit() does not always imply performance improvement.
ServiceNow by default is using MySQL database. "setLimit()" probably translates to "LIMIT" in sql select statement.
In general, including LIMIT will improve performance but as can be found from reading the following threads, setting LIMIT may actually decrease performance in some circumstances. It's best practice to actually benchmark performance.
https://stackoverflow.com/questions/4481388/why-does-mysql-higher-limit-offset-slow-the-query-down
https://stackoverflow.com/questions/34488/does-limiting-a-query-to-one-record-improve-performance
Also, check the documentation.
For example, there is a warning on using setLimit() with deleteMultiple().
Also, do not use this method with the chooseWindow() or setLimit() methods when working with large tables.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-27-2022 01:20 AM
sys_id's are probably indexed. Indexed columns uses hash when searching instead of going through all records.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-26-2022 08:53 AM
Hi there,
If it's an query with If and not While, then you are interested in the first record, so definitely apply setLimit, simply a best practice.
1 remark though: this does not apply when also using getRowCount! Then don't apply setLimit.
Depending on what you are doing with the query, you might also choose hasNext instead of next.
If my answer helped you in any way, please then mark it as helpful.
Kind regards,
Mark
2020, 2021 ServiceNow Community MVP
2020, 2021 ServiceNow Developer MVP
---
LinkedIn
Community article, blog, video list
Kind regards,
Mark Roethof
Independent ServiceNow Consultant
10x ServiceNow MVP
---
~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-26-2022 04:03 PM
As others have replied, it's setLimit().
In short, not always. Setting .setLimit() does not always imply performance improvement.
ServiceNow by default is using MySQL database. "setLimit()" probably translates to "LIMIT" in sql select statement.
In general, including LIMIT will improve performance but as can be found from reading the following threads, setting LIMIT may actually decrease performance in some circumstances. It's best practice to actually benchmark performance.
https://stackoverflow.com/questions/4481388/why-does-mysql-higher-limit-offset-slow-the-query-down
https://stackoverflow.com/questions/34488/does-limiting-a-query-to-one-record-improve-performance
Also, check the documentation.
For example, there is a warning on using setLimit() with deleteMultiple().
Also, do not use this method with the chooseWindow() or setLimit() methods when working with large tables.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-26-2022 04:53 PM
Also, don't use setLimit() with updateMultiple() when working with large tables.
Note: Do not use this method with the chooseWindow() or setLimit() methods when working with large tables.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-26-2022 05:13 PM
BTW, there's also chooseWindows(). According to the following statement, it seems performance consideration will be the same as with setLimit().
Note: setLimit internally calls chooseWindow.
https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0868974