-
Notifications
You must be signed in to change notification settings - Fork 198
perf: improve messages queries #1024
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
base: master
Are you sure you want to change the base?
Conversation
I tried query with query BatchLatestMCI {
_a42edf54dd93220a6f8a410c0919c77a67d2feef8a8295123eabe17426853284: messages(where: {
space: "swarmfoundation.eth",
type_in: ["proposal", "delete-proposal"],
},
first: 1,
orderBy: "mci",
orderDirection: desc) {
mci
}
_f88a5f7ff244b35c39427d542ba718362b413eec175276b193d459404dd1d0fc: messages(where: {
space: "etherfi-dao.eth",
type_in: ["proposal", "delete-proposal"],
},
first: 1,
orderBy: "mci",
orderDirection: desc) {
mci
}
} it is still slow somehow 🤔 taking forever, is it same for you? |
An immediate working fix is changing the orderBy to |
Aside from that, nothing really can be done, beside adding more index |
Adding space + mci index can help? may also need space + type + mci |
adding any composite index will help. Issue is that the size with the size of our table, it will takes few hours, and lot of spaces (like 30-50 gb) |
hmmm yeah, maybe we can switch index like this, this is much faster diff --git a/src/graphql/operations/messages.ts b/src/graphql/operations/messages.ts
index 9dd9fa7..797fe02 100644
--- a/src/graphql/operations/messages.ts
+++ b/src/graphql/operations/messages.ts
@@ -27,11 +27,16 @@ export default async function (parent, args) {
orderDirection = orderDirection.toUpperCase();
if (!['ASC', 'DESC'].includes(orderDirection)) orderDirection = 'DESC';
- // Use space index when filtering by space and not ordering by mci
- const useSpaceIndex =
- where.space && orderBy !== 'm.mci' ? 'USE INDEX (space)' : '';
+ // Optimized index selection for better performance
+ let indexHint = '';
+ if (where.space) {
+ indexHint = 'USE INDEX (space)';
+ } else if (orderBy === 'm.mci') {
+ indexHint = 'USE INDEX (mci)';
+ }
+
const query = `
- SELECT m.* FROM messages m ${useSpaceIndex}
+ SELECT m.* FROM messages m ${indexHint}
WHERE 1=1 ${queryStr}
ORDER BY ${orderBy} ${orderDirection} LIMIT ?, ?
`; |
Mci index should already be the one used by default, since it's the primary key. Do you notice difference by using it explicitly? |
hmm looks like above queries are resolving fast on hub.snapshot.org 😅 i tried different spaces and they are working as well. weird |
Fixes https://github.com/snapshot-labs/workflow/issues/585
This PR improves upon the previous fix.
We will now use the query hint
use index
when the query is filtered by spaces, and not ordered bymci
(which is an indexed column).