|
| 1 | +--- |
| 2 | +Title: Best practices for scalable Redis Query Engine |
| 3 | +alwaysopen: false |
| 4 | +categories: |
| 5 | +- docs |
| 6 | +- operate |
| 7 | +- stack |
| 8 | +description: Best practices for scalable Redis Query Engine in Redis Software and Redis Cloud. |
| 9 | +linkTitle: Best practices for scalable Redis Query Engine |
| 10 | +weight: 25 |
| 11 | +--- |
| 12 | + |
| 13 | +[Vertical scaling of Redis Query Engine]({{<relref "/operate/oss_and_stack/stack-with-enterprise/search/query-performance-factor">}}) requires configuring query performance factors. With careful crafting of search indices and queries, query performance factors allow throughput scaling up to 16X. The following recommendations help queries avoid accessing the keyspace and enable Redis Query Engine to benefit from additional CPUs allocated by query performance factors. |
| 14 | + |
| 15 | +## Best candidates for query performance factor improvements |
| 16 | + |
| 17 | +- Query types: |
| 18 | + |
| 19 | + - [Full-text]({{<relref "/develop/interact/search-and-query/query/full-text">}}) |
| 20 | + |
| 21 | + - [Tag]({{<relref "/develop/interact/search-and-query/advanced-concepts/tags">}}) |
| 22 | + |
| 23 | + - [Vector]({{<relref "/develop/interact/search-and-query/query/vector-search">}}) |
| 24 | + |
| 25 | +- Result set types: |
| 26 | + |
| 27 | + - Small result sets |
| 28 | + |
| 29 | + - Document subsets that are indexed in their [non-normalized]({{<relref "/develop/interact/search-and-query/advanced-concepts/sorting#normalization-unf-option">}}) form |
| 30 | + |
| 31 | +## Indexing best practices |
| 32 | + |
| 33 | +Follow these best practices for [indexing]({{<relref "/develop/interact/search-and-query/indexing">}}): |
| 34 | + |
| 35 | +- Include fields in the index definition that are used in the query or the required result sets (projections). |
| 36 | + |
| 37 | +- Use `SORTABLE` for all fields returned in result sets. |
| 38 | + |
| 39 | +- Use the `UNF` option for `TAG` and `GEO` fields. |
| 40 | + |
| 41 | +- Use the `NOSTEM` option for `TEXT` fields. |
| 42 | + |
| 43 | +## Query best practices |
| 44 | + |
| 45 | +Follow these best practices for [queries]({{<relref "/develop/interact/search-and-query/query">}}): |
| 46 | + |
| 47 | +- Specify the result set fields in the `RETURN` or `LOAD` clauses and include them in the index definition. Don’t just return the default result set from [`FT.SEARCH`]({{< baseurl >}}/commands/ft.search/) or `LOAD *` from [`FT.AGGREGATE`]({{< baseurl >}}/commands/ft.aggregate/). |
| 48 | + |
| 49 | +- Use `LIMIT` to reduce the result set size. |
| 50 | + |
| 51 | +- Use [`DIALECT 3`]({{<relref "/develop/interact/search-and-query/advanced-concepts/dialects#dialect-3">}}) or higher for any queries against JSON. |
| 52 | + |
| 53 | +## Index and query examples |
| 54 | + |
| 55 | +The following examples depict an anti-pattern index schema and query, followed by a corrected schema and query, which allows for scalability with the Redis Query Engine. |
| 56 | + |
| 57 | +### Anti-pattern index schema |
| 58 | + |
| 59 | +The following index schema is not optimized for vertical scaling: |
| 60 | + |
| 61 | +```sh |
| 62 | +FT.CREATE jsonidx:profiles ON JSON PREFIX 1 profiles: |
| 63 | + SCHEMA $.tags.* as t NUMERIC SORTABLE |
| 64 | + $.firstName as name TEXT |
| 65 | + $.location as loc GEO |
| 66 | +``` |
| 67 | + |
| 68 | +### Anti-pattern query |
| 69 | + |
| 70 | +The following query is not optimized for vertical scaling: |
| 71 | + |
| 72 | +```sh |
| 73 | +FT.AGGREGATE jsonidx:profiles '@t:[1299 1299]' LOAD * LIMIT 0 10 |
| 74 | +``` |
| 75 | + |
| 76 | +### Improved index schema |
| 77 | + |
| 78 | +Here's an improved index schema that follows best practices for vertical scaling: |
| 79 | + |
| 80 | +```sh |
| 81 | +FT.CREATE jsonidx:profiles ON JSON PREFIX 1 profiles: |
| 82 | + SCHEMA $.tags.* as t NUMERIC SORTABLE |
| 83 | + $.firstName as name TEXT NOSTEM SORTABLE |
| 84 | + $.lastName as lastname TEXT NOSTEM SORTABLE |
| 85 | + $.location as loc GEO SORTABLE |
| 86 | + $.id as id TAG SORTABLE UNF |
| 87 | + $.ver as ver TAG SORTABLE UNF |
| 88 | +``` |
| 89 | + |
| 90 | +### Improved query |
| 91 | + |
| 92 | +Here's an improved query that follows best practices for vertical scaling: |
| 93 | + |
| 94 | +```sh |
| 95 | +FT.AGGREGATE jsonidx:profiles '@t:[1299 1299]' |
| 96 | + LOAD 6 id t nam" lastname loc ver |
| 97 | + LIMIT 0 10 |
| 98 | + DIALECT 3 |
| 99 | +``` |
0 commit comments