R
R
rumasterov2016-09-12 10:34:44
Yii
rumasterov, 2016-09-12 10:34:44

Yii2 and ORM: how to make large selections from the database?

The question is rather not even about Yii ORM, but in general about ORM as a data access scheme.
There is a page with schedules of events for the week, the table can contain up to 200-300 models of the Event type, and 2-3 links are still requested for each model. As a result, this page is loaded in 1-1.5 seconds.
I tried using with to immediately load connections (eager loading), but such a request with IN and a cloud of id in it only worsens the situation.
Yes, when there are 10-20 elements on a page, there are no problems at all, everything is fast and good, but sometimes you need to build such selections. How to make selections with a large number of models on one page?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
N
Nikita, 2016-09-12
@bitver

If you think that it takes a long time to form the second request, then you can use joinWith().
If you don't have special logic in models, then this will help: www.yiiframework.com/doc-2.0/guide-db-active-recor...
PS 200-300 objects with 2-3 links don't create 1s load, it takes longer render. If you optimize the view or cache, then get rid of the problem.

V
Vyacheslav Ovchinnikov, 2016-09-12
@ova777

In this case, I would not recommend getting this data in the form of models.
Yii has such a wonderful thing as DAO

M
mitaichik, 2016-09-12
@mitaichik

I think Yii has nothing to do with it. More precisely, its ORM. All these transformations / creations of objects that make it an ORM (more precisely, ActiveRecord) are all trifles, something else is slowing you down.
Here it is necessary:
​​1. To profile the request itself. Perhaps your selection itself is slow, there are not enough indexes, or something else.
2. Play with with: see what works better: with / joinWith (it will load everything with one request at all), or without with at all (that is, lazy loading - in some cases it’s better this way).
3. Pay attention to what happens after the selection - perhaps slows down post-processing.
4. Look at the models themselves: if you have a bunch of behavers hung on each model, it’s also possible to be dumb (although it started to be dumb when we chose tens of thousands of models)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question