Skip to content
This repository has been archived by the owner on Jul 8, 2023. It is now read-only.

more SQLs then expected #231

Open
tasiotas opened this issue Jun 7, 2023 · 6 comments
Open

more SQLs then expected #231

tasiotas opened this issue Jun 7, 2023 · 6 comments

Comments

@tasiotas
Copy link

tasiotas commented Jun 7, 2023

Hi,

Here is my schema and query. When I run the query, I get some extra SQL queries in Debug Toolbar, that seem unnecessary. Not sure what is invoking them and how to avoid them.

  | + | SELECT ••• FROM "accounts_user" WHERE "accounts_user"."id" = '1' LIMIT 21 |   | 2.24 | Sel Expl
  | + | SELECT COUNT(*) AS "__count" FROM "marketplace_product" WHERE "marketplace_product"."author_id" = '1' |   | 1.08 | Sel Expl
  | + | SELECT ••• FROM "marketplace_product" WHERE "marketplace_product"."author_id" = '1' LIMIT 8 |   | 0.41 | Sel Expl
  | + | SELECT ••• FROM "marketplace_product" WHERE "marketplace_product"."id" = '659' LIMIT 21 7 similar queries. |   | 0.36 | Sel Expl
  | + | SELECT ••• FROM "marketplace_product" WHERE "marketplace_product"."id" = '660' LIMIT 21 7 similar queries. |   | 0.34 | Sel Expl
  | + | SELECT ••• FROM "marketplace_product" WHERE "marketplace_product"."id" = '661' LIMIT 21 7 similar queries. |   | 0.34 | Sel Expl
  | + | SELECT ••• FROM "marketplace_product" WHERE "marketplace_product"."id" = '662' LIMIT 21 7 similar queries. |   | 0.37 | Sel Expl
  | + | SELECT ••• FROM "marketplace_product" WHERE "marketplace_product"."id" = '663' LIMIT 21 7 similar queries. |   | 0.35 | Sel Expl
  | + | SELECT ••• FROM "marketplace_product" WHERE "marketplace_product"."id" = '664' LIMIT 21 7 similar queries. |   | 0.62 | Sel Expl
  | + | SELECT ••• FROM "marketplace_product" WHERE "marketplace_product"."id" = '665' LIMIT 21 7 similar queries. |   | 0.46 | Sel Expl

3rd query gets me what I need (product's name), what are the queries below it?
Here are 4 last lines from Debug Toolbar stack tract:

/usr/local/lib/python3.11/site-packages/strawberry_django_plus/utils/resolvers.py in wrapper(103)
  return resolver(*args, **kwargs)

/usr/local/lib/python3.11/site-packages/strawberry_django_plus/field.py in resolve_connection(335)
  return super().resolve_connection(

/usr/local/lib/python3.11/site-packages/strawberry_django_plus/relay.py in resolve_connection(1079)
  return return_type.from_nodes(nodes, **kwargs)

/usr/local/lib/python3.11/site-packages/strawberry_django_plus/relay.py in from_nodes(770)
  edges = [edge_class.from_node(v, cursor=start + i) for i, v in enumerate(iterator)]
#schema.py
@gql.django.type(User, filters=UserFilter, order=UserOrder)
class UserType(gql.relay.Node):
    email: gql.auto
    ...

    @gql.django.connection()
    def products(self) -> List["ProductType"]:
        return self.product_set.all()


@gql.django.type(Product, filters=ProductFilter, pagination=True)
class ProductType(gql.relay.Node):
    name: gql.auto
    ...
#query
query MyQuery {
  user {
    products {
      edges {
        node {
          name
        }
      }
    }
  }
}
@bellini666
Copy link
Member

Hey @tasiotas ,

I think that it is because the name products is not matching the django model name, so the optimizer doesn't know where to find.

You can try 2 changes in there:

  1. Define a related_name="products on the foreignkey so that it is accessible by self.products.all() instead of self.product_set.all(). Since it matches the name of the field it should work

  2. If you don't want to set a related_name, try to define the field as:

    @gql.django.connection(name="products")
    def product_set(self) -> List["ProductType"]:
        return self.product_set.all()

Let me know if any of those solutions works for you. Otherwise I might need some more info to understand what might be going on there

@tasiotas
Copy link
Author

tasiotas commented Jun 7, 2023

unfortunately both solutions didn't work. Will try to get you a repro.

Thanks!

@bellini666
Copy link
Member

unfortunately both solutions didn't work. Will try to get you a repro.

Thanks!

A repro will help a lot to understand how to properly optimize this :)

@tasiotas
Copy link
Author

tasiotas commented Jun 7, 2023

got the repro. Im new to Poetry, didn't know how to update this lib to latest, so its at 2.4.0
https://github.com/tasiotas/strawberry_debug

without optimizer I have 3 queries, with optimizer I get 6.
Just create some Project and Milestone objects and run this query:

query MyQuery {
  project(name: "one") {
    milestones {
      edges {
        node {
          name
        }
      }
    }
  }
}

@bellini666
Copy link
Member

Hey @tasiotas ,

I just released a new version, which has a major refactoring of the relay integration by using the official one instead.

Could you check if your issue still happens in that version? I think some of the changes I've made should have fixed this.

@tasiotas
Copy link
Author

unfortunately, its still the same behavior in v3

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

No branches or pull requests

2 participants