laravel-ab icon indicating copy to clipboard operation
laravel-ab copied to clipboard

N+1 queries while creating the experiments collection.

Open rocramer opened this issue 6 years ago • 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.

rocramer avatar Aug 12 '19 09:08 rocramer

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?

ben182 avatar Aug 12 '19 20:08 ben182

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

rocramer avatar Aug 13 '19 05:08 rocramer

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

adevade avatar Oct 26 '19 12:10 adevade

Should be fixed through 2.0.2 can somebody confirm?

ben182 avatar Nov 17 '21 20:11 ben182