Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

Useless queries on not displayed related entities #2866

Closed
jocel1 opened this issue Jun 17, 2019 · 7 comments
Closed

Useless queries on not displayed related entities #2866

jocel1 opened this issue Jun 17, 2019 · 7 comments

Comments

@jocel1
Copy link
Contributor

jocel1 commented Jun 17, 2019

After taking a look at the SQL queries, I've just figured out Api platform was requesting useless queries from object not displayed in the JSON.

How to reproduce:

In the DB:

DROP DATABASE IF EXISTS apibug;
CREATE DATABASE apibug;

Then:

git clone https://github.com/Softizy/api-bug
cd api-bug;
./bin/console doctrine:schema:create
./bin/console server:start 

curl -X POST "http://127.0.0.1:8000/api/mums" -H "accept: application/ld+json" -H "Content-Type: application/ld+json" -d "{ \"mumId\": \"0test0\"}"

 curl -X GET "http://127.0.0.1:8000/api/mums" -H "accept: application/ld+json" -H "Content-Type: application/ld+json"
{  
   "@context":"\/api\/contexts\/Mums",
   "@id":"\/api\/mums",
   "@type":"hydra:Collection",
   "hydra:member":[  
      {  
         "@id":"\/api\/mums\/0test0",
         "@type":"Mums",
         "mumId":"0test0"
      }
   ],
   "hydra:totalItems":1
}

In the doctrine debug, you'll see:

cat ./var/log/dev.log
[2019-06-17 19:23:49] doctrine.DEBUG: SELECT m0_.mum_id AS mum_id_0 FROM mums m0_ ORDER BY m0_.mum_id ASC LIMIT 10 [] []
[2019-06-17 19:23:49] doctrine.DEBUG: SELECT t0.latitude AS latitude_1, t0.longitude AS longitude_2, t0.mum_id AS mum_id_3 FROM mum_locations t0 WHERE t0.mum_id = ? ["0test0"] []
[2019-06-17 19:23:49] doctrine.DEBUG: SELECT count(m0_.mum_id) AS sclr_0 FROM mums m0_ [] []

The query on mum_locations seems to be useless, it shouldn't be hydrated by getOneOrNullResult
Am I missing something?

@soyuka
Copy link
Member

soyuka commented Jun 18, 2019

Did you set up serialization groups? You could also disable eager loading for this resource.

@jocel1
Copy link
Contributor Author

jocel1 commented Jun 18, 2019

Already try disabling the eager loading, even with EXTRA_LAZY it's still doing the fetch :)
You can see the entity here:
https://github.com/Softizy/api-bug/blob/master/src/Entity/Mums.php
Serialization groups are indeed enabled, but the mum_locations properly doesn't have any serialization group, so it should be excluded (and it's indeed excluded from the json result)

@teohhanhui
Copy link
Contributor

As an aside, naming the entity in plural is really confusing! 🙈

@teohhanhui
Copy link
Contributor

teohhanhui commented Jun 18, 2019

Please see doctrine/orm#4389 (comment) and doctrine/orm#4389 (comment)

It's how Doctrine works.

@teohhanhui
Copy link
Contributor

teohhanhui commented Jun 18, 2019

So indeed, what we could do in API Platform is to always eager load such an association, even if it's not in the serializer group. Counter intuitive, I know! Doing an unnecessary join ends up saving us one query.

@teohhanhui
Copy link
Contributor

The alternative is to enable partial loading, but that comes with its own problems... (Hence not enabled by default.)

@jocel1
Copy link
Contributor Author

jocel1 commented Jun 19, 2019

Hi @teohhanhui! Thanks for the feedback, indeed those plural entities need to be changed ;)

@soyuka soyuka removed the question label Nov 6, 2022
@api-platform api-platform locked and limited conversation to collaborators Nov 6, 2022
@soyuka soyuka converted this issue into discussion #5151 Nov 6, 2022

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Projects
None yet
Development

No branches or pull requests

3 participants