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/Remove Indexes - Media Table #1126

Merged
merged 5 commits into from
Apr 3, 2024
Merged

Add/Remove Indexes - Media Table #1126

merged 5 commits into from
Apr 3, 2024

Conversation

kaysiz
Copy link
Contributor

@kaysiz kaysiz commented Feb 14, 2024

Purpose

We have an incomplete index that is causing the query to do a filesort in the filter. The first part of the query uses index to fetch record but other part is now using filesort.

=> EXPLAIN for: SELECT `media`.* FROM `media` WHERE `media`.`dataset` = 38 ORDER BY created DESC
+----+-------------+-------+------------+------+-----------------------------------+-----------------+---------+-------+------+----------+---------------------------------------+
| id | select_type | table | partitions | type | possible_keys                     | key             | key_len | ref   | rows | filtered | Extra                                 |
+----+-------------+-------+------------+------+-----------------------------------+-----------------+---------+-------+------+----------+---------------------------------------+
|  1 | SIMPLE      | media | NULL       | ref  | dataset_updated,FK62F6FE44D3D6B1B | dataset_updated | 8       | const |    1 |    100.0 | Using index condition; Using filesort |
+----+-------------+-------+------------+------+-----------------------------------+-----------------+---------+-------+------+----------+---------------------------------------+

closes: #1124

Approach

  • Add a composite index that will cover both parts of the query
=> EXPLAIN for: SELECT `media`.* FROM `media` WHERE `media`.`dataset` = 38 ORDER BY created DESC
+----+-------------+-------+------------+------+---------------------------------------------------------------+-----------------------------+---------+-------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys                                                 | key                         | key_len | ref   | rows | filtered | Extra       |
+----+-------------+-------+------------+------+---------------------------------------------------------------+-----------------------------+---------+-------+------+----------+-------------+
|  1 | SIMPLE      | media | NULL       | ref  | dataset_updated,FK62F6FE44D3D6B1B,index_media_dataset_created | index_media_dataset_created | 8       | const |    1 |    100.0 | Using where |
+----+-------------+-------+------------+------+---------------------------------------------------------------+-----------------------------+---------+-------+------+----------+-------------+
  • Refactor Media related queries

Open Questions and Pre-Merge TODOs

Learning

Types of changes

  • Bug fix (non-breaking change which fixes an issue)

  • New feature (non-breaking change which adds functionality)

  • Breaking change (fix or feature that would cause existing functionality to change)

Reviewer, please remember our guidelines:

  • Be humble in the language and feedback you give, ask don't tell.
  • Consider using positive language as opposed to neutral when offering feedback. This is to avoid the negative bias that can occur with neutral language appearing negative.
  • Offer suggestions on how to improve code e.g. simplification or expanding clarity.
  • Ensure you give reasons for the changes you are proposing.

@kaysiz kaysiz self-assigned this Feb 14, 2024
@kaysiz kaysiz requested a review from a team February 14, 2024 13:04
@@ -50,7 +50,7 @@ def doi_id=(value)

def set_defaults
current_media =
Media.where(dataset: dataset).order("media.created DESC").first
Media.where(dataset: dataset).order("created DESC").first
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Specifying a table name is only necessary on complex queries, simple queries like this one you can just specify the column as there is no ambiguity as to where the column belongs

@@ -1874,7 +1874,7 @@ def metadata_version
end

def current_media
media.order("media.created DESC").first
Copy link
Contributor Author

@kaysiz kaysiz Feb 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is already defined in the association has_many :media, -> { order "created DESC" }.. as a result it produces a query like this "SELECT `media`.* FROM `media` WHERE `media`.`dataset` = 38 ORDER BY created DESC, media.created DESC"

@ashwinisukale ashwinisukale self-requested a review April 3, 2024 08:09
@kaysiz kaysiz merged commit 3763817 into master Apr 3, 2024
13 checks passed
@kaysiz kaysiz deleted the performance/media-indexes branch April 3, 2024 08:13
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.

Add/Remove Indexes on Media table
3 participants