Skip to content

Instantly share code, notes, and snippets.

View haseebeqx's full-sized avatar
🟢
Building software

Haseeb Annadamban haseebeqx

🟢
Building software
View GitHub Profile
@haseebeqx
haseebeqx / pluck_in_batches.rb
Created July 14, 2024 05:44
Pluck in batches - combines the efficiency of pluck with the memory-friendly approach of find_in_batches
module ActiveRecord
class Relation
def pluck_in_batches(*columns, batch_size: 1000, start: nil)
relation = self
relation = relation.where(table[primary_key].gteq(start)) if start
batch_order = "#{table.name}.#{primary_key} ASC"
relation = relation.reorder(batch_order)
loop do
batch = relation.limit(batch_size).pluck(*columns, primary_key)
@haseebeqx
haseebeqx / query.sql
Last active July 7, 2024 10:24
query.sql
SELECT
i.relname AS index_name,
t.relname AS table_name,
s.idx_scan AS times_used,
pg_size_pretty(pg_relation_size(i.oid)) AS index_size
FROM
pg_stat_user_indexes s
JOIN
pg_index ix
ON
@haseebeqx
haseebeqx / exceed_query_limit.rb
Created October 9, 2018 17:59
n+1 issue find using rspec
# from gitlab/spec/support/matchers/exceed_query_limit.rb
RSpec::Matchers.define :exceed_query_limit do |expected|
supports_block_expectations
match do |block|
@subject_block = block
actual_count > expected_count + threshold
end
failure_message_when_negated do |actual|