[Personal note] Rails DB index consideration

I didn't understand the index well, so I read the following article and thought about it.

Rails: Don't index dark clouds (translation)

The index seems to be created by copying the sorted columns separately. I couldn't understand it, so I will consider it.

I am writing on the assumption that I am inexperienced and wrong, so please do not refer to it.

In the above article, there was the following model as an example.

class ShoppingCart < ActiveRecord::Base
  has_many :shopping_cart_products
  has_many :products, through: :shopping_cart_products
end

class ShoppingCartProduct < ActiveRecord::Base
  belongs_to :shopping_cart
  belongs_to :product
end

class Product < ActiveRecord::Base
end

ShoppingCart ShoppingCartProduct Product

I'm making three models, but I stumbled from here.

I wondered if it was necessary to dare to intermediate ShoppingCartProduct. What is the reason for not directly associating Shopping Cart with Product?

If you associate ShoppingCart with Product, You will be querying the items in your cart.

What's wrong with directly querying products? I'm saying that I dare to use cart products here and that DB wants to speed up queries as much as possible. There is little doubt that the law will result in efficient results with the intervening items in the cart. The assumption is that querying the product directly would simply slow it down.

Why is it late? It's speculative because all the specific columns and data are not written in the reference article. If the cart model is directly associated with the product, then there are multiple products in the cart. Then, in order to browse the products, it is necessary to extract the products related to the cart ID from the product model.

For example, suppose you want to put an index here. The products are sorted in order of cart ID. Create another column in the product model so that it can be linked with the product ID. The index seems to be slow to write because it creates an extra column. The first thing I thought about was to include the index each time I wanted to call the product model. I wondered if I would search. Secondly, writing will be delayed because an index will be added each time a product is added. Thirdly, when you search for related products from the cart, you have to search for the products in the cart from the huge product list. A cart is something that changes frequently and is referred to many times. No matter how much the product model is related to the cart ID, Isn't it difficult to look at the cart and query the product model each time? I'm guessing that the third reason is the correct slowdown, which is probably the reason for including the cart's product model.

Here, by positioning the product model of the cart as the middle, When you press the add to cart button, the cart and the product are associated. Isn't it necessary to have an intermediate when pressing the add to cart button? I also thought It's probably okay to have a direct link between the cart and the item.

When you press the cart button, create a cart product model based on your cart ID and product ID, It is an understanding of mediating.

class CreateShoppingCartProducts < ActiveRecord::Migration
  def change
    create_table :shopping_cart_products do |t|
      t.integer :quantity, limit: 1, null: false
      t.belongs_to :shopping_cart, index: true, foreign_key: true
      t.belongs_to :product, index: true, foreign_key: true

      t.timestamps null: false
    end
  end
end

As for the index, When you look at the list of carts, search for products in the cart. You can search for products directly, but I think you don't want to put an index on each product. In that case, I'm thinking that I would like to put the cart ID index on the products in the cart and search for products by product ID.

In the example of the above article, it is wrong to put the index between carts and products in the cart, and between products in the cart. He says don't put an index between the items in the cart. The reason is that products are searched from the cart, but not the other way around. This will drop, so the index between the items in the cart will be deleted. The article says to add and remove indexes as follows:

python


class RemoveIndexShoppingCartProductsOnProductId < ActiveRecord::Migration
  def up
    remove_index(:shopping_cart_products, :product_id)
  end

  def down
    add_index(:shopping_cart_products, :product_id)
  end
end

I understand a little about how to index, I felt that the design of DB was not catching up yet, so Let's consider a simple DB design example.

Rather than just unknowingly using a general DB configuration example It also takes time to stop and think about why this is the design.

Recommended Posts

[Personal note] Rails DB index consideration
Rails Credentials Note
rails db: 〇〇 Summary
[Note] Rails error list
rails db: migrate failed!
[Rails] rails db command summary
[Note] Rails3 routing confirmation
rails test db only drop
Error in rails db: migrate
Exciting personal development with Rails Part 1: Try designing specifications and DB