Review your query metrics
The cost of your queries will always depend on your specific data. It is best to get into the habit of reviewing the metrics sent back with your queries. You can do this by inspecting the response headers. You can also see a summary of the stats when you execute a query in the dashboard shell.
Reducing Read Ops
You can define values for an index. If all of the data you need is contained within the index, and thus you do not need to Get
additional documents, you will only be charged for the read cost of the index.
For this reason, it can be considered a good practice (depending on your use case of course) to put as much information into indexes as possible. This can greatly increase storage, but storage is cheap. This option becomes less appealing when there is a higher mix of write operations, since writing to the index will also be more expensive. Is up to you what the best trade off is.
The following queries could cost one TRO and one TCO, so long as size is small enough.
Get(Ref(Collection("things"), '1'))
// OR
CreateIndex({
name: "all_things_by_color",
source: Collection("things"),
terms: [{ field: ["data", "color"] }],
values: [
{ field: ["data", "name"] },
{ field: ["data", "description"] },
{ field: ["data", "length"] },
{ field: ["data", "width"] },
{ field: ["ref"] }, // <-- good to include in case you do need more information or want to use elsewhere
]
})
Paginate(Match(Index("all_things_by_color"), "blue"))
Cost of Intersection and other Set operations
Regarding multiple WHERE conditions… you need to think about this in terms of FQL. FQL is your precise query plan, so knowing the FQL is a must for estimating the cost.
The Paginate
function is not what incurs the cost. It’s actually reading an index. When you Paginate
a single Match(Index(...))
it happens to only read one index, once, so it only costs 1 TRO.
Multiple conditions with AND would use Intersection
. The Pagination
step for Set operations, such as Intersection
, Difference
, Union
etc. cost more because they operate on multiple indexes and possibly need to do so multiple times. Intersection
, for example, costs about ([# of indexes] - 1) * 2
to account for the work to compare the indexes
// 2 TROs
Paginate(
Intersection(
Match(Index("things_by_a"), 1),
Match(Index("things_by_b"), 2),
)
)
// 4 TROs
Paginate(
Intersection(
Match(Index("things_by_a"), 1),
Match(Index("things_by_b"), 2),
Match(Index("things_by_c"), 3),
)
)
// 6 TROs
Paginate(
Intersection(
Match(Index("things_by_a"), 1),
Match(Index("things_by_b"), 2),
Match(Index("things_by_c"), 3),
Match(Index("things_by_d"), 4),
)
)