Add indices for simplenews_subscriber

Created on 27 March 2023, over 1 year ago
Updated 27 July 2023, over 1 year ago

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

✨ Feature request
Status

Fixed

Version

4.0

Component

Code

Created by

πŸ‡ΈπŸ‡°Slovakia kaszarobert

Live updates comments and jobs are added and updated live.
  • Performance

    It affects performance. It is often combined with the Needs profiling tag.

Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024