Problem/Motivation
Currently, when we need to know if an e-mail is a subscriber or not, the address is queried but that results a full table scan every time:
DESCRIBE
SELECT s.id
FROM drupal1.simplenews_subscriber s
where mail = 'a12000@example.com';
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE s ALL NULL NULL NULL NULL 12001 Using where
You can see that it searched in all 12001 rows of the table for that e-mail address. So the more addresses you have, the slower the query will be.
I would suggest adding an index to that column (I don't know the proper Drupal way but in SQL it would be this: CREATE INDEX mail ON simplenews_subscriber (mail);)
This will eliminate full table scans if we analyze the same query as before:
DESCRIBE
SELECT s.id
FROM drupal1.simplenews_subscriber s
where mail = 'a12000@example.com';
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE s ref mail mail 1019 const 1 Using where; Using index
In the module Subscriber::loadByMail($email); and $subscribers = \Drupal::entityTypeManager()->getStorage('simplenews_subscriber')->loadByProperties(['mail' => $mail]); use this column for their queries.
Steps to reproduce
Proposed resolution
Remaining tasks
User interface changes
API changes
Data model changes
| Comment | File | Size | Author |
|---|---|---|---|
| #4 | simplenews.add-indices.3350587-4.patch | 3.15 KB | adamps |
Comments
Comment #2
adamps commentedThanks. We can copy the code from #2907367: Add indexes to important fields. Probably uid should be an index too.
Comment #3
adamps commentedComment #4
adamps commentedComment #5
adamps commented@kaszarobert Please can you test and confirm it works for you?
Comment #6
adamps commentedComment #8
adamps commented