Skip to content
This repository has been archived by the owner on Jun 18, 2019. It is now read-only.

Returning all records results in 200+ queries #483

Closed
turtlechase opened this issue Jun 26, 2018 · 2 comments
Closed

Returning all records results in 200+ queries #483

turtlechase opened this issue Jun 26, 2018 · 2 comments

Comments

@turtlechase
Copy link

turtlechase commented Jun 26, 2018

Hi,

Country::listsTranslations('name')->pluck('name', 'id')->toArray();

I'm doing this to get all my countries translated.

It does 200+ queries. Does a query per country to get it's name.

It runs this query:

SELECT
  `countries`.`id`,
  `country_translations`.`name`
FROM `countries`
  LEFT JOIN `country_translations` ON `country_translations`.`country_id` = `countries`.`id`
WHERE (
  `country_translations`.`locale` = 'pt' 
  OR (
    `country_translations`.`locale` = 'pt' 
    AND `country_translations`.`country_id` NOT IN (
      SELECT `country_translations`.`country_id`
      FROM `country_translations`
      WHERE `country_translations`.`locale` = 'pt'
    )
  )
);

But then runs the same query

SELECT *
FROM  `country_translations`
WHERE `country_translations`.`country_id` IS NULL 
  AND `country_translations`.`country_id` IS NOT NULL;

one for each country.

What am I doing wrong?

Thanks

@turtlechase turtlechase changed the title Returning all records results in 254 queries Returning all records results in 200+ queries Jun 26, 2018
@Gummibeer
Copy link
Collaborator

This is related to #482 - I bet that the problem is that your toArray() call ends in the attributesToArray() which says that translations relation is not loaded but he should do it on toArray - so he loads it. Super bad in your case.
Ideas to solve this are in the linked issue.

As a workaround you could do ->load('translations'). This will load all translations in a single query. If you don't want to load translations on toArray() call in general you can also disable this in the config file.

But I will think about a solution to get around this.

@Gummibeer
Copy link
Collaborator

I've added a fix in #484 - can you take a look if it solves your problem?

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

No branches or pull requests

2 participants