Basically my problem is similar to this: Slow stream loading problem · Issue #7981 · diaspora/diaspora · GitHub
For myself, I solved the problem this way: I removed DISTINCT from the problematic queries. I know that this is not entirely correct, but it is better than loading one page for 2 minutes…
Examples:
**№1**
Before:
SELECT DISTINCT posts.*, `posts`.`id` FROM `posts` INNER JOIN `taggings` ON `taggings`.`taggable_type` = 'Post' AND `taggings`.`taggable_id` = `posts`.`id` WHERE `posts`.`type` = 'StatusMessage' AND `posts`.`public` = TRUE AND (taggings.tag_id IN (5974,8345,1,1123,19401,2294,2379,1538,23,6407,654,6,696,8959,543,1328,13921,655,17811,73684,8344,9881,34870,21,8960,8770,6264,6265,6263,8412)) AND (posts.author_id NOT IN (332,338,339,420,4476)) AND (posts.id NOT IN ('3604','3608','655029','654511')) AND (posts.created_at < '2023-07-25 13:24:10') AND `posts`.`type` IN ('StatusMessage', 'Reshare') ORDER BY posts.created_at DESC, posts.id DESC LIMIT 15;
Time: 2 min 25 sec
After:
SELECT posts.*, `posts`.`id` FROM `posts` LEFT JOIN `taggings` ON `taggings`.`taggable_type` = 'Post' AND `taggings`.`taggable_id` = `posts`.`id` WHERE `posts`.`type` = 'StatusMessage' AND `posts`.`public` = TRUE AND (taggings.tag_id IN (5974,8345,1,1123,19401,2294,2379,1538,23,6407,654,6,696,8959,543,1328,13921,655,17811,73684,8344,9881,34870,21,8960,8770,6264,6265,6263,8412)) AND (posts.author_id NOT IN (332,338,339,420,4476)) AND (posts.id NOT IN ('3604','3608','655029','654511')) AND (posts.created_at < '2023-07-25 13:24:10') AND `posts`.`type` IN ('StatusMessage', 'Reshare') ORDER BY posts.created_at DESC, posts.id DESC LIMIT 15;
Time: 0.146 sec
**№2**
Before:
SELECT COUNT(*) FROM `posts` INNER JOIN `people` ON `people`.`id` = `posts`.`author_id` WHERE `posts`.`type` = 'StatusMessage' AND `people`.`owner_id` IS NOT NULL;
Time: 1 min 40 sec
After:
SELECT STRAIGHT_JOIN COUNT(*) FROM `posts` INNER JOIN `people` ON `people`.`id` = `posts`.`author_id` WHERE `posts`.`type` = 'StatusMessage' AND `people`.`owner_id` IS NOT NULL;
Time: 29 sec
Maybe this will be useful to someone…