QA1004: EndsWith

This advisor message is returned when a query uses the ENDSWITH function. The message clarifies the reason and provides guidance to improve query performance.

Properties

Value
RuleID QA1004
Title EndsWith
Category Performance

Cause

The query uses ENDSWITH.

Rule Description

ENDSWITH is less efficient than prefix-based matching. ENDSWITH requires using a full index scan, which can be more costly than functions like STARTSWITH, which performs a precise index scan. To understand the difference between the different type of index scan, see Overview of Indexing.

Recommendation

If the comparison logic allows, create and index a computed property that reverses the string and use STARTSWITH for comparison.

For more information on computed properties, see Computed properties.

Example

Original query:

SELECT * 
FROM c 
WHERE ENDSWITH(c.name, "xyz")

To improve this query, first create and index a computed property that reverse the property:

{ 
  "computedProperties": [ 
	{ 
		"name": "cp_reverseName", 
		"query": "SELECT VALUE REVERSE(c.name) FROM c" 
	} 
  ] 
}

Then use the following updated query:

SELECT * 
FROM c 
WHERE STARTSWITH(cp_reverseName, "xyz")