The CreatorCon Call for Content is officially open! Get started here.

AddQuery vs AddEncodedQuery

Anubhav24
Mega Sage

Hi All,

 

Is there a difference between addquery and addencodedquery. Is there a performance difference between these two considering 2 scenarios : a) When only column value is being matched b)When multiple column values are being matched?

Eventually the query gets converted to "Select *" format right ?

 

11 REPLIES 11

Sayali Gurav
Kilo Sage

Hello @Anubhav24 ,

 

addQuery:

This method allows you to construct a query using ServiceNow's query syntax.
It takes a field name, an operator, and a value to define the condition.
For example: addQuery('priority', '=', '1') would filter records where the 'priority' field is equal to 1.


addEncodedQuery:

This method allows you to provide a pre-built encoded query string.
An encoded query string is a URL-encoded string representing a query condition.
This is useful when you want to construct complex queries with logical operators like AND, OR, etc.
For example: addEncodedQuery('priority=1^ORstate=2') would filter records where 'priority' is 1 OR 'state' is 2.
So, the main difference is in how you specify the conditions. addQuery uses a method call with separate arguments for field, operator, and value, while addEncodedQuery takes a pre-built encoded query string.

In general, if you're constructing queries programmatically within a script, addQuery might be more intuitive. If you're working with complex, pre-defined queries or dealing with user inputs that are already in encoded form, addEncodedQuery is a more convenient choice.

 

Mark helpful or correct if applicable.

THanks & Regards,

Sayali Gurav

praneeth7
Tera Guru

Hi @Anubhav24 

In Simple terms, 

The addQuery method is used to add simple, field-specific conditions to a GlideQuery or GlideAggregate object. You provide the field name and the value you want to filter on, and it constructs a basic condition for you. It's suitable for straightforward queries with conditions that don't require complex logic or multiple operators.

Example:

 

var incidentQuery = new GlideQuery('incident');
incidentQuery.addQuery('priority', '=', '1');
incidentQuery.query(); // Execute the query to retrieve incidents with priority 1

 

Where, The addEncodedQuery method is used for more complex queries. It allows you to specify conditions using encoded query strings, which can include logical operators (AND, OR), multiple fields, and comparison operators. This method provides greater flexibility when constructing complex queries.

Example:

 

var incidentQuery = new GlideQuery('incident');
incidentQuery.addEncodedQuery('priority=1^ORstate=2'); // Retrieve incidents with priority 1 OR state 2
incidentQuery.query(); // Execute the query

 

In summary, addQuery is used for simple, field-specific conditions, while addEncodedQuery is used for more complex queries with multiple conditions and logical operators. The choice between them depends on the complexity of your filtering requirements.

if you find this useful Please mark my answer Helpful and Accepted solution,

Thank you

 

We do have OR , IN operators for addQuery method as well , but my intention out here is to understand the performance side or the performance for both methods is same ?

Hi @Anubhav24 

 

In general, the performance difference between the two methods may not be significant for most use cases. However, when dealing with very complex queries or large datasets, optimizing your queries becomes more critical. Here are some best practices for improving query performance in ServiceNow:

  • Use appropriate indexing: Ensure that the fields you are querying on are indexed. Indexed fields are retrieved more efficiently.
  • Limit the number of records: If possible, use filters to limit the number of records returned by your query. This reduces the workload on the system.
  • Profile and analyze: Use the ServiceNow Performance Analytics tools to profile and analyze query performance. This can help identify and address performance bottlenecks.
  • Avoid excessive OR conditions: When using addEncodedQuery with multiple OR conditions, be cautious not to create queries with an excessive number of OR clauses, as this can negatively impact performance.

Ultimately, the performance impact of using addQuery vs. addEncodedQuery will depend on the specific query, the size of the dataset, and the underlying database schema. It's a good practice to profile and benchmark your queries in a real-world scenario to determine if there are any significant performance differences and to optimize as needed.

 

Please refer this link, it might be give the answer for you Performance Best Practice for Efficient Queries - Top 10 Practices

 

if you find this useful Please mark my answer Helpful and Accepted solution,

Thank you