Optimizing UNION Queries: From 30s To Under 1s Report Generation Time
Debugging performance issues in reports led to a breakthrough: replacing WHERE field IN with WHERE EXISTS (SELECT 1 ...) improved query time from 30s to under 1s!
Senior Rails developer from Latvia. Avid gamer. Longevity enthusiast. #keto-dude
Debugging performance issues in reports led to a breakthrough: replacing WHERE field IN with WHERE EXISTS (SELECT 1 ...) improved query time from 30s to under 1s!
Wrap .first_or_create!/find_or_create_by! calls in retryable block to prevent race conditions. Use Retryable gem with on: [ActiveRecord::RecordNotUnique, ActiveRecord::RecordInvalid] for a clean solution.
Participated in code challenge despite illness, focusing on Domain Query Interface & entity-based operations. Learned to optimize game state & debugging info for reproducibility.
Ruby's Lint/Void rule has an exception: when called via `super`, last expression is returned, not value passed. This can lead to unexpected behavior.
Include stackprof in gemfile, require it and run slow code in its block to generate a flamegraph for performance analysis.
Optimizing import logic with buffered bulk updates: collect updates in a buffer, perform upsert_all when reaching batch_size, reducing DB roundtrip overhead and improving performance.
JOIN tables with LIKE operator using PSQL concatenation: INNER JOIN config_entries ON things.name LIKE config_entries.lookup_term || '%
Use bundle outdated to find outdated gems in your Gemfile, ignoring recent Rails updates for a useful reference.
Parallel RSpec runners on CI took 12-20min due to specs writing to DB & DatabaseCleaner cleanup. Switched from :truncation to :deletion, reducing spikes by 60s. Monitoring run times is key to preventing issues.
Boost Rails JSON performance by 30-40% with Oj gem! Include in Gemfile, call Oj.optimize_rails in initializer and see significant execution time reduction.
Reduce N+1 queries in Rails v7.1 with `.strict_loading!`. Configure `config/application.rb` and `config/environments/development.rb` to log or raise violations. Chain `.strict_loading` in new controller code, especially actions.
Adding an index in production is relatively safe with `strong_migrations` and `pghero`. Use `ActiveRecord::Migration.add_index` to add concurrently, then remove if it fails. Commit a corresponding migration for all envs.
Experimenting with skipping navbar rendering in feature specs to reduce CI runtime by 2-4%. Created a hook to disable navbar by default, but allow opting back into rendering as needed.
Normalizes macro doesn't normalize nil values out of the box. Use `:boolean_field, apply_to_nil: true, with: -> { _1.present? }` to fix this issue.
Custom semantic order with SQL CASE or Rails' in_order_of! Use `in_order_of(:status, [:active, :draft], filter: false).order(:id)` for a better way since Rails 7.1.
has_many association in Rails gives 17 methods. Focus on collection_singular_ids=(ids) part which allows mass-assignment of genres for a book.
Fixed Excel export tests by creating custom matcher to ignore irrelevant empty cells. Simplified specs and made them more readable.
Disable after_commit callback: override method for spec instance, e.g. `def model_instance.some_callback; nil; end