Skip to content
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

Model::validateEntityScope() should not fetch/compute any data #1093

Open
mvorisek opened this issue Feb 16, 2023 · 2 comments
Open

Model::validateEntityScope() should not fetch/compute any data #1093

mvorisek opened this issue Feb 16, 2023 · 2 comments

Comments

@mvorisek
Copy link
Member

mvorisek commented Feb 16, 2023

can be reproduced on https://github.com/atk4/ui/blob/03a9d65ff32602493ecf9980d740fa3f8fc5e21f/demos/init-db.php#L403

image

NOTES:

  1. the performance graph above is with
    $this->reload();
    commented (otherwise 2x slower, as data are fetched 1 more time)
  2. when the L403 is changed from $entity->SubFolder->import... to $entity->getModel()->import..., the total import time is about 10x faster (measured /wo xdebug/profiling)
@mkrecek234
Copy link
Contributor

@mvorisek Can confirm that this dramatic performance loss with added references in models is happening, especially if you have lots of cascaded hasOne relations with added fields of each hasOne references.

@mvorisek
Copy link
Member Author

mvorisek commented Feb 25, 2023

@mkrecek234 I debug the repro steps on atk4/ui as described above and the problem is not reference travesing. With deeper nesting, the query is not becoming larger and larger. The repro here does use hasMany. For hasOne please submit a separate issue.

The problem here is:
a) https://github.com/atk4/data/blob/4.0.0/src/Model.php#L1469 kick in as when traversed, SubFolder condition is added, and thus the scope is checked with an extra load, which is prooven also by the performance graph above
b) the select query is slow/large as these:

  (
    select
      coalesce(
        count(*),
        0
      )
    from
      `file` `_affS_d17cc72bb50e`
    where
      `atk_afp_file__parent_folder_id` = `file`.`atk_afp_file__id`
  ) `atk_fp_file__count`

and

  `atk_afp_file__parent_folder_id` `atk_fp_file__parent_folder_id`,
  (
    select
      `atk_afp_file__name`
    from
      `file` `_affpf_f7867cefdce3`
    where
      (
        `atk_afp_file__is_folder` = 1
        and `atk_afp_file__id` = `file`.`atk_afp_file__parent_folder_id`
      )
  ) `atk_fp_file__parent_folder`

the fields should not be definitely computed on scope validation if there are not a part of validated scope (there are not) and in general, we might introduce something like "lazy fields", fields computed on the 1st access only.

also the performance graph above shows the query render might not be the fastest, so this should be investigated as well, but please note the real query render is lower, as the performance measurement itself imply significant overhead

@mvorisek mvorisek changed the title Traversing reference imply slow queries Model::validateEntityScope() should not fetch/compute any data Feb 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants