Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "login" index to Users table #1856

Merged
merged 6 commits into from
Feb 10, 2024
Merged

Conversation

nimmolo
Copy link
Contributor

@nimmolo nimmolo commented Jan 26, 2024

Without index:

irb(main):001> User.where(login: "Margarida").explain
  User Load (155.7ms)  SELECT `users`.* FROM `users` WHERE `users`.`login` = 'Margarida'
=> 
EXPLAIN SELECT `users`.* FROM `users` WHERE `users`.`login` = 'Margarida'
+----+-------------+-------+------------+------+---------------+------+---------+------+-------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key  | key_len | ref  | rows  | filtered | Extra       |
+----+-------------+-------+------------+------+---------------+------+---------+------+-------+----------+-------------+
|  1 | SIMPLE      | users | NULL       | ALL  | NULL          | NULL | NULL    | NULL | 68627 |     10.0 | Using where |
+----+-------------+-------+------------+------+---------------+------+---------+------+-------+----------+-------------+
1 row in set (0.00 sec)

This example is actually straight out of the production log, where it took much longer:

User Load (512.7ms)  SELECT `users`.* FROM `users` WHERE `users`.`login` = 'Margarida' LIMIT 1

With index:

irb(main):001> User.where(login: "Margarida").explain
  TRANSACTION (0.1ms)  BEGIN
  User Load (0.4ms)  SELECT `users`.* FROM `users` WHERE `users`.`login` = 'Margarida'
=> 
EXPLAIN SELECT `users`.* FROM `users` WHERE `users`.`login` = 'Margarida'
+----+-------------+-------+------------+------+---------------+-------------+---------+-------+------+----------+-------+
| id | select_type | table | partitions | type | possible_keys | key         | key_len | ref   | rows | filtered | Extra |
+----+-------------+-------+------------+------+---------------+-------------+---------+-------+------+----------+-------+
|  1 | SIMPLE      | users | NULL       | ref  | login_index   | login_index | 242     | const |    1 |    100.0 | NULL  |
+----+-------------+-------+------------+------+---------------+-------------+---------+-------+------+----------+-------+
1 row in set (0.00 sec)

@nimmolo nimmolo marked this pull request as draft January 26, 2024 08:38
@nimmolo
Copy link
Contributor Author

nimmolo commented Jan 26, 2024

Hmm. Can you index on a text column like this? (edit) yes, you can.

@coveralls
Copy link
Collaborator

coveralls commented Jan 26, 2024

Coverage Status

coverage: 94.555%. remained the same
when pulling c59e48e on nimmo-add-target-index-to-comments
into 83b0061 on main.

@nimmolo nimmolo marked this pull request as ready for review February 10, 2024 21:50
@mo-nathan
Copy link
Member

Looks good.

@nimmolo nimmolo merged commit f27959e into main Feb 10, 2024
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants