Best practices while using indexes
Whenever you are using indexes, you can follow certain guidelines to make sure you get the best performance for your queries. Following are some of the key points to remember:
- Avoid creating an index on a sequence. Due to the nature of how the columns are sharded, sometimes we can have range hotspots, where most of the requests are coming for the same range, which can slow down the query. It would be best to use UUIDs or randomly generated keys. If you must create an index on a column that is sequential in nature, you should use hash-sharded indexes, as discussed in the Hash-sharded indexes section.
- If you are using multiple columns in your
WHERE
clause or in yourORDER BY
clause, you should consider creating an index for all these columns. - In your
WHERE
clause, make sure to have filters that are more restrictive before the ones that are a bit more generic. For example,=
andIN
should come beforeLIKE
,>
,!=
, and...