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
Execute SingleOrDefault query on database #5566
Comments
The code you provided looks good ... not sure whats wrong. Can you create a little repro and attach it to the issue? |
@michaelstaib here I prepared small repro for you, this time using SQL Lite instead of Azure SQL. Migrations and DB seeding should be executed on startup. These are all possible queries to execute on BananaCakePop. query getAll {
all {
id
name
}
}
query getById {
byId(id: 1) {
id
name
}
}
query getByIdX {
byIdX(id: 1) {
id
name
}
} If you check getById query, you can see, that the generated SQL statement is: SELECT "a"."Id", "a"."Name"
FROM "Accounts" AS "a"
WHERE "a"."Id" = @__id_0 And if you check getByIdX, you will see that the generated SQL looks like: SELECT "a"."Id", "a"."Country", "a"."CreatedAt", "a"."Email", "a"."LastChangedAt", "a"."Name"
FROM "Accounts" AS "a"
WHERE "a"."Id" = @__id_0
LIMIT 2 As I said in previous post, for me it looks like 1st query loads all the items that satisfy condition to memory, and then performs SingleOrDefault on data that are already in memory. In 2nd case, there is different query, where SingleOrDefault is performed on DB layer, but projection isn't working. I may be wrong, or maybe I don't understand how each of those SQL statements works, so that's why I am asking for help. |
I looked into this a bit (using mostly v13), and there does appear to be quite a critical issue with the [UseFirstOrDefault] This executes the query without a limit. This means that it could load an entire table into memory It should be setting the limit/take to 1. [UseSingleOrDefault] This also executes the query without a limit. If the column is unique, then it's not an issue. If the column is not unique, then it will load all matching data Ideally, it should be setting the limit/take to 2. @xmasctf For |
Is there an existing issue for this?
Describe the bug
Hi,
I'm trying to optimize my SingleOrDefault() query to the database, but can not get desired result.
I created that GraphQL query, which is giving me correct result, besides that Projection is working well too, so only columns provided are also returned. What bother me is when I look at the generated SQL query, it looks like all the items that satisfy .Where() condition are loaded into memory, and then SingleOrDefault() is executed.
My C# code:
Generated SQL:
If I try with another code, then SingleOrDefault() is performed on database, but the thing is that Projection stops working. I am aware that Projections are working only on IQueryable, and that is the reason why it returns all of the columns from DB.
Antoher C# example:
And generated SQL statement:
As you can see this is whole different query performed on database level, but in this case Projection stops working. If I instead of searching by int id use [UseFiltering] attribute the things are still working the same way as in 1st case.
Is there something I am missing out, am I interpreting SQL statements in a wrong way, or is the query I am trying to achieve impossible using HotChocolate?
Thank you for help.
Am I missing something, o
Steps to reproduce
I provided all the source code in descrpiton of bug.
Relevant log output
No response
Additional Context?
No response
Product
Hot Chocolate
Version
12
The text was updated successfully, but these errors were encountered: