atk4 / data

Data Access PHP Framework for SQL & high-latency databases
https://atk4-data.readthedocs.io
MIT License
270 stars 46 forks source link

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

Open mvorisek opened 1 year ago

mvorisek commented 1 year ago

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 https://github.com/atk4/data/blob/dfa5de573036a56717fd836b7db4a910ba257f8d/src/Model.php#L1575 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 commented 1 year ago

@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 commented 1 year ago

@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:

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