-
Notifications
You must be signed in to change notification settings - Fork 279
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
SpiceDB Spanner queries use different+worse index than e.g. same query in Spanner Studio #1687
Comments
Thanks for the detailed report @kruegener! 👏🏻 ⭐ |
I suspect the best way forward is actually to force the index. This is often discouraged for other databases, but it looks like the Spanner documentation actually recommends it: https://cloud.google.com/spanner/docs/secondary-indexes#index-directive |
If you'd like you can try out #1692 which should fix this issue. |
Thx for the quick MR on this! I hope you also had some time off in the recent weeks. There was a small typo in your MR, the index is called I can confirm that the slow query is altered (what seems correct to me) with the forced index and now looks like this: SELECT namespace,
object_id,
relation,
userset_namespace,
userset_object_id,
userset_relation,
caveat_name,
caveat_context
FROM relation_tuple /*@ FORCE_INDEX = ix_relation_tuple_by_subject */
WHERE ((userset_namespace = @p1 AND userset_object_id IN (@p2) AND userset_relation = @p3))
AND namespace = @p4
AND relation = @p5
AND ((object_id > @p6))
ORDER BY userset_namespace, userset_object_id, userset_relation, namespace, object_id,
relation LIMIT 9223372036854775807 But Running the same query once again in Spanner Studio does not use that index, similar to the original situation without the forced index. |
I've found some documentation interleaved indexes which is another lead to explore for making these queries perform.
Totally spitballing: it's possible the full scan is of this table is worse than using a less-specific, but interleaved index. |
@kruegener Would you be willing to try modifying one of your Spanner indexes manually? The current index being used is defined as Now that LR has cursors, it might be worth adding another index of the form:
If you're willing to add that manually and try on your slow query, I'd appreciate it |
What platforms are affected?
others
What architectures are affected?
others
What SpiceDB version are you using?
1.25.0 - 1.27.0
Not sure if this issue is present in earlier versions as well.
Steps to Reproduce
TLDR:
Details:
A LookupResource will issue multiple SQL queries targeting the Spanner datastore that are very similar, but will use different indices for the lookup, which depending on the amount of relations present will result in large performance degradations.
First query, using the
ix_relation_tuple_by_subject
index and returning in <20ms:Later query, using
ix_relation_tuple_by_subject_relation
index, returns in 2000-4000ms:This only occurs with queries issued by SpiceDB. Running the EXACT SAME query inside e.g. Spanner Studio will always use the correct index and return in <20ms as well, yielding the same rows.
This issue was investigated by adding the
QueryWithStats()
Spanner call inspanner.go
and verifying the planning and execution times as well as which indices where used.The used
Query optimizer version
andStatistics package
are the same between the queries in Spanner Studio and issued by SpiceDB.Expected Result
SpiceDB issued Spanner calls use the correct/fastest index for SQL queries
Actual Result
ix_relation_tuple_by_subject_relation
is used instead ofix_relation_tuple_by_subject
, leading to a severe performance degradation.We have stopped using
LookupResources
calls for now for cases with many relations, as the performance penalty was too severe.The text was updated successfully, but these errors were encountered: