skills/redis-development/rules/rqe-query-optimization.md

1.2 KiB

title impact impactDescription tags description alwaysApply
Write Efficient Queries HIGH Proper filtering reduces query time by orders of magnitude rqe, ft.search, query, performance, filters Write Efficient Queries true

Write Efficient Queries

Be specific and use filters to reduce the result set early.

Correct: Use specific filters and limit results.

# Good: Specific query with filters
FT.SEARCH idx:products "@category:{electronics} @price:[100 500]"
    LIMIT 0 20
    RETURN 3 name price category

# Good: Use SORTBY and LIMIT
FT.SEARCH idx:products "@name:laptop"
    SORTBY price ASC
    LIMIT 0 10

Incorrect: Broad queries returning large result sets.

# Bad: Wildcard prefix scans entire index
FT.SEARCH idx:products "*" LIMIT 0 10000

# Bad: Loading all fields from source document
FT.AGGREGATE idx:products "*" LOAD *

Performance tips:

  • Add SORTABLE to fields used in SORTBY
  • Use TAG SORTABLE UNF for best performance on tag fields
  • Use NOSTEM if you don't need stemming
  • Profile queries with FT.PROFILE
FT.PROFILE idx:products SEARCH QUERY "@category:{electronics}"

Reference: Redis Search Query Syntax