Is gr.getLimit() better for performance?

Marion de Groo1
Tera Guru

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

 

1 ACCEPTED SOLUTION

Hitoshi Ozawa
Giga Sage
Giga Sage

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/62814115/why-does-order-by-and-limit-1-slow-down-a-mysql-query-s...

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.

https://developer.servicenow.com/dev.do#!/reference/api/paris/server/no-namespace/c_GlideRecordScope...

 

View solution in original post

9 REPLIES 9

sys_id's are probably indexed. Indexed columns uses hash when searching instead of going through all records.

Mark Roethof
Tera Patron
Tera Patron

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

LinkedIn

Hitoshi Ozawa
Giga Sage
Giga Sage

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/62814115/why-does-order-by-and-limit-1-slow-down-a-mysql-query-s...

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.

https://developer.servicenow.com/dev.do#!/reference/api/paris/server/no-namespace/c_GlideRecordScope...

 

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.

https://developer.servicenow.com/dev.do#!/reference/api/paris/server/no-namespace/c_GlideRecordScope...

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