0

I am trying to index already created columns with over 5 million data in my table. My question is if I add index with the migration will the already created data be indexed as well ? Or do I need to re-index the created data if so how ?

This is my migration

add_index :data_prods, :date_field
add_index :data_prods, :entity_id

Thank you.

Edit I am using PostgreSQL dbms.

4
  • What DBMS are you using? Commented Jan 29, 2020 at 11:47
  • I am using PostgreSQL. Commented Jan 29, 2020 at 11:49
  • 1
    Index belongs to table, not to rows on table. Create your index and be happy. Commented Jan 29, 2020 at 12:31
  • So this will make the queries faster right? I am having a loading issue because of this much data. Commented Jan 29, 2020 at 12:32

1 Answer 1

1

The process of adding an index re-indexes the entire tables contents. A table with 5 million rows may take some time, I suggest testing in a staging environment (with a similar amount of data) to see how long this migration will take, as well as impact to the application.

Re: your comment about improving query times

Indexes will make queries faster, where the indexed columns are commonly referenced in "where" clauses. In your case, any query where you filter by date_field OR entity_id will be faster, but other queries will not be improved. It should be noted that each query will only use 1 index, if the majority of your queries use both date_field AND entity_id at the same time to filter data, you might be better off using a composite index. Id check out this post for further reading on composite indexes. Index on multiple columns in Ruby on Rails

Sign up to request clarification or add additional context in comments.

4 Comments

Could always use connection.execute to create indexes concurrently to help with no down time. Although that would make the creation of the index take longer.
@gdxn96 So composite indexing maybe better for me because I am using that combination in most of my queries.
@Int'lManOfCodingMystery Can you explain a bit on how to use the connetion.execute with no downtime.
@Amal , with connection.execute you can run raw SQL commands. So in your index you can do something like create index concurrently idx_index_name on table_name using btree (column1) Using the concurrent flag for creating an index will prevent a table lock. postgresql.org/docs/9.1/sql-createindex.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.