Skip to content

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

Conversation

wa0x6e
Copy link
Contributor

@wa0x6e wa0x6e commented Jul 9, 2025

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 by mci (which is an indexed column).

@wa0x6e wa0x6e requested a review from ChaituVR July 9, 2025 14:21
@wa0x6e wa0x6e marked this pull request as ready for review July 9, 2025 14:21
@ChaituVR
Copy link
Member

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?

@wa0x6e
Copy link
Contributor Author

wa0x6e commented Jul 10, 2025

An immediate working fix is changing the orderBy to timestamp (which is essentially the same thing), which return in a relatively acceptable time (~10s)

@wa0x6e
Copy link
Contributor Author

wa0x6e commented Jul 10, 2025

Aside from that, nothing really can be done, beside adding more index

@ChaituVR
Copy link
Member

ChaituVR commented Jul 10, 2025

Aside from that, nothing really can be done, beside adding more index

Adding space + mci index can help? may also need space + type + mci

@wa0x6e
Copy link
Contributor Author

wa0x6e commented Jul 10, 2025

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)

@ChaituVR
Copy link
Member

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 ?, ?
   `;

@wa0x6e
Copy link
Contributor Author

wa0x6e commented Jul 11, 2025

Mci index should already be the one used by default, since it's the primary key. Do you notice difference by using it explicitly?

@ChaituVR
Copy link
Member

hmm looks like above queries are resolving fast on hub.snapshot.org 😅 i tried different spaces and they are working as well. weird

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants