ben182/laravel-ab

N+1 queries while creating the experiments collection.

Opened this issue · 4 comments

Thanks for the great package, Ben! I noticed N+1 queries:

Example: I created two experiments and two goals. If a user is visiting a page with an included a/b test the package fetches the experiment one, then separately the two goals. After that it fetches the second experiment and the two goals separately as well. Then, it is updating the visitors counter. So in the end 7 queries are performed.

The query problem is due to the nested foreach in the start() method in the ABTesting class. The goals model should be eager loaded.

It would be great if you have a quick solution for the problem. Otherwise I will create a PR in the next weeks.

Hi Robin, thanks for your effort in making this package better. However, I can not confirm the N+1 problem by adding eager loading. In fact by adding eager loading, two additional queries will be executed. Can you confirm this behavior?

By adding eager loading to the experiment, all associated goals will be fetched via one additional query. In the current implementation, all goals for all experiments are getting fetched separately. See here 19 queries for three experiments and four goals as an example:

Screenshot_2019-08-13 Request Details - Telescope

I can confirm the N+1 on the first page load for a new visitor. If I disable cookies it loads 14 queries every request.

2019-10-26_14-33-46
2019-10-26_14-34-33

Should be fixed through 2.0.2 can somebody confirm?