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

virtual depth_cache #662

Open
mitsuru opened this issue Mar 30, 2023 · 4 comments
Open

virtual depth_cache #662

mitsuru opened this issue Mar 30, 2023 · 4 comments

Comments

@mitsuru
Copy link
Contributor

mitsuru commented Mar 30, 2023

I noticed a problem updating the depth_cache in the big tree.
Since it is a sequential update, the updates are issued in a large number of UPDATE statements.
I have solved this problem with generated columns. (It depends on MySQL, but Postgres may be able to handle it as well as Rails7)
rails/rails#41856

    change_table :table do |t|
      t.virtual :ancestry_depth, type: :integer, as: "LENGTH(ancestry) - LENGTH(REPLACE(ancestry, '/', '')) - 1", stored: true
      t.index :ancestry_depth
    end

This example depends on ancestry_format: :materialized_path2.

This solution has the problem that existing scopes cannot be used.

scope :before_depth, lambda { |depth| where("#{depth_cache_column} < ?", depth) }
scope :to_depth, lambda { |depth| where("#{depth_cache_column} <= ?", depth) }
scope :at_depth, lambda { |depth| where("#{depth_cache_column} = ?", depth) }
scope :from_depth, lambda { |depth| where("#{depth_cache_column} >= ?", depth) }
scope :after_depth, lambda { |depth| where("#{depth_cache_column} > ?", depth) }

@kbrock
Copy link
Collaborator

kbrock commented Apr 9, 2023

Looks like rails 7 supports generated columns for both mysql and postgres.
This looks great. I did not know rails supported this feature.

In the current master (will be the 5.0 branch), I have implemented ancestry_depth_sql. (the sql should look familiar)

Currently it supports updating all depth_cache values in a single query. build_depth_cache_sql! #654 - I probably should have just updated build_depth_cache to use the sql version but I took it slowly.

Let me know if this helps with your problem or if you have suggestions to make it better

@kbrock
Copy link
Collaborator

kbrock commented Jul 17, 2023

Had a few ideas around this and put together a PR to share.
Let me know if you still use virtual columns?

@kbrock
Copy link
Collaborator

kbrock commented Nov 13, 2023

@mitsuru Did you have a chance to look at #670 ?

@kbrock
Copy link
Collaborator

kbrock commented Mar 6, 2024

Also, there is the ability to update all depth values using a single query. Not sure if this would work for you.

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

No branches or pull requests

2 participants