spatie / mailcoach-support

Questions and support for Mailcoach
https://mailcoach.app
31 stars 2 forks source link

routes mailcoach.emailLists and mailcoach.emailLists.subscribers slow with 800k subscribers #247

Closed electronick86 closed 3 years ago

electronick86 commented 3 years ago

Hello!

One of the project I'm working on with mailcoach (currently v2.23.13) has 800k subscribers. Once the subscribers are imported, the routes mailcoach.emailLists and mailcoach.emailLists.subscribers becomes very slow.

I saw (thanks telescope) thatone query tooks ~10seconds...(on my macbook pro 16 with valet and also on a 8vcpu server) I think one new index could be created. Can you give me your point of view?

Mysql_Local___immovlan_mail_-_MySQL_5_7_29
 SELECT
    `mailcoach_email_lists`.*,
    (
        SELECT
            count(id)
        FROM
            `mailcoach_subscribers`
        WHERE
            `subscribed_at` IS NOT NULL
            AND `unsubscribed_at` IS NULL
            AND `mailcoach_subscribers`.`email_list_id` = `mailcoach_email_lists`.`id`) AS `active_subscribers_count`
    FROM
        `mailcoach_email_lists`
    ORDER BY
        `name` ASC
    LIMIT 15 OFFSET 0;

Execution time : 11.544 s

CREATE INDEX `test` ON `immovlan_mail`.`mailcoach_subscribers` (`email_list_id`,`subscribed_at`,`unsubscribed_at`) USING BTREE;

With this index created, same request take 385 ms .

Do you see any downside to create this extra index. There is no so many insert/updates to this table. Or a better way to solve this problem?

Thanks!

connecteev commented 3 years ago

I haven't encountered this yet, but wow....11 seconds to 385ms, that's amazing!

freekmurze commented 3 years ago

@electronick86 that's quite amazing. Could you send a PR to the v3 branch that adds this index to the default migration?

riasvdv commented 3 years ago

Added in the v3 branch