FriendsOfFlarum / terms

Ask your users to accept TOS and Privacy Policy
https://discuss.flarum.org/d/11714
MIT License
14 stars 9 forks source link

Performance issue when loading topic #49

Open BartVB opened 4 months ago

BartVB commented 4 months ago

Bug Report

Current Behavior FoF Terms seems to load policy data when loading post data with a rather inefficient query.

Steps to Reproduce ab -n 10 'https://flarum.site/api/posts?filter[id]=5323291,5323293,5323296,5323302,5323307,5323320,5323327,5323539,5323541,5324486,5324508,5324904,5325088,5325459,5325900,5325942,5325993,5326020,5326283,5326289'

With FoF Terms enabled:

Time taken for tests:   24.228 seconds
Complete requests:      10
Failed requests:        0
Total transferred:      379372 bytes
HTML transferred:       369900 bytes
Requests per second:    0.41 [#/sec] (mean)
Time per request:       2422.847 [ms] (mean)
Time per request:       2422.847 [ms] (mean, across all concurrent requests)

With FoF Terms disabled:

Time taken for tests:   3.840 seconds
Complete requests:      10
Failed requests:        0
Total transferred:      379370 bytes
HTML transferred:       369900 bytes
Requests per second:    2.60 [#/sec] (mean)
Time per request:       383.995 [ms] (mean)
Time per request:       383.995 [ms] (mean, across all concurrent requests)

The offending query is:

select `fof_terms_policies`.*, `fof_terms_policy_user`.`user_id` as `pivot_user_id`, `fof_terms_policy_user`.`policy_id` as `pivot_policy_id`, `fof_terms_policy_user`.`accepted_at` as `pivot_accepted_at` from `fof_terms_policies` inner join `fof_terms_policy_user` on `fof_terms_policies`.`id` = `fof_terms_policy_user`.`policy_id` where `fof_terms_policy_user`.`user_id` = 21996645

This takes approximately 300ms (on a not very beefy DB server).

Expected Behavior No involvement of FoF Terms in viewing topics = no performance impact ;)

Environment