question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Optimize database queries to only pull out fields actually requested

See original GitHub issue

Can use the info parameter passed to the resolver to pick out only the fields.

Eg, given this schema:

type User {
  id: ID!
  name: String
  avatar: Blob
  longVideo: Blob
}

When we do a query like:

User(id: "foobar") {
  id
  name
}

We don’t want to pay the cost of pulling the avatar and longVideo out of the database.

In the resolver, we can use the info parameter to see what fields are actually requested:

const resolvers = {
  User: (_, { id }, context, info) => {
    const nodeQueryInfo = info.fieldNodes[0]; // 0th is always the currently resolving node
    const fields = nodeQueryInfo.selectionSet.selections.map(({ name }) => name.value);
    return db.findById(id, fields); // (or `SELECT ${fields.join()} FROM ...`, etc)
  },
};

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
jesstelfordcommented, Feb 12, 2019

From https://github.com/keystonejs/keystone-5/pull/665#discussion_r

Will also need to think about it in the context of the hooks framework, and whether we need to perform this operation anyway to provide the full object to the hooks.

🤔 The test could be

if (hasHooks) {
  returnEverything();
} else {
  limitResults();
}
0reactions
timlesliecommented, Jun 1, 2020

@timleslie was any of this optimised with the relationship restructure?

Nope, this issue is still in the category of nice to have, but tricky to implement.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Query optimization techniques in SQL Server: tips and tricks
In this blog post we will show you step by step some tips and tricks for successful Query optimization techniques in SQL Server....
Read more >
Supercharge Your SQL Queries for Production Databases
Discover simple techniques you can start using today to optimize your SQL when querying production databases, with real-life examples.
Read more >
SQL Query Optimization: 12 Useful Performance Tuning Tips ...
Read the tutorial to learn more about SQL Server performance tuning best practices that help greater improve SQL Server query optimization.
Read more >
Chapter 4. Query Performance Optimization - O'Reilly
In the previous chapter, we explained how to optimize a schema, which is one of the necessary conditions for high performance.
Read more >
Performance Tuning SQL Queries | Advanced SQL - Mode
Learn how to conduct SQL performance tuning by reducing table size, simplifying joins, & the EXPLAIN command in this advanced SQL tutorial.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found