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

CommentFileSizeAuthor
#4 simplenews.add-indices.3350587-4.patch3.15 KBadamps

Comments

kaszarobert created an issue. See original summary.

adamps’s picture

Thanks. We can copy the code from #2907367: Add indexes to important fields. Probably uid should be an index too.

adamps’s picture

Version: 3.x-dev » 4.x-dev
adamps’s picture

Status: Active » Needs review
StatusFileSize
new3.15 KB
adamps’s picture

@kaszarobert Please can you test and confirm it works for you?

adamps’s picture

Title: Add an index to the mail column in simplenews_subscriber » Add indices for simplenews_subscriber

  • AdamPS committed 9347b14f on 4.x
    Issue #3350587 by AdamPS: Add indices for simplenews_subscriber
    
adamps’s picture

Status: Needs review » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.