performance

sql
-- PostgreSQL Declarative Partitioning (10+)

-- Create partitioned table by date range
CREATE TABLE measurements (
  id BIGSERIAL,
  sensor_id INT NOT NULL,

Table partitioning for large datasets

database partitioning postgresql
by Maria Garcia 2 tabs
go
package middleware

import (
  "compress/gzip"
  "net/http"
  "strings"

Gzip compression middleware with correct Vary header

go http middleware
by Leah Thompson 1 tab
javascript
import { Controller } from "@hotwired/stimulus"

export default class extends Controller {
  static targets = ["count", "button"]
  static values = {
    url: String,

Optimistic UI updates with Turbo Streams

hotwire turbo stimulus
by Jordan Lee 3 tabs
ruby
module ExistenceChecks
  extend ActiveSupport::Concern

  class_methods do
    def has_any?(conditions = {})
      relation = conditions.present? ? where(conditions) : all

Fast “Exists” Checks with select(1) and LIMIT

rails activerecord performance
by codesnips 4 tabs
go
package hotpath

import "testing"

func BenchmarkParse(b *testing.B) {
  b.ReportAllocs()

Benchmarking hot paths (allocs and throughput)

go performance benchmarking
by Leah Thompson 1 tab
typescript
import DataLoader from "dataloader";
import { Pool } from "pg";

export interface User { id: number; name: string; }
export interface Post { id: number; user_id: number; title: string; }

N+1 avoidance with DataLoader (GraphQL)

graphql performance dataloader
by codesnips 3 tabs
ruby
class AddUniqueIndexToInventorySnapshots < ActiveRecord::Migration[7.1]
  disable_ddl_transaction!

  def change
    add_index :inventory_snapshots,
              [:warehouse_id, :sku],

Bulk Upsert with insert_all + Unique Index

rails activerecord postgres
by codesnips 3 tabs
sql
-- Enable pg_stat_statements extension
CREATE EXTENSION IF NOT EXISTS pg_stat_statements;

-- postgresql.conf:
-- shared_preload_libraries = 'pg_stat_statements'
-- pg_stat_statements.track = all

Query performance monitoring and profiling

database monitoring performance
by Maria Garcia 2 tabs
ruby
module CursorPagination
  extend ActiveSupport::Concern

  private

  def paginate_with_cursor(scope, per_page: 20)

Pagination with cursor-based approach

rails api pagination
by Alex Kumar 1 tab
ruby
class AddIndexesToPosts < ActiveRecord::Migration[6.1]
  def change
    add_index :posts, :author_id
    add_index :posts, :published_at
    add_index :posts, [:author_id, :published_at]
    add_index :posts, :created_at, order: { created_at: :desc }

Database indexes for query optimization

rails postgresql database
by Alex Kumar 1 tab
sql
-- Product.active.touch_all(:cache_synced_at) for catalog 42
UPDATE "products"
SET "updated_at" = '2024-05-01 12:00:00.123456',
    "cache_synced_at" = '2024-05-01 12:00:00.123456'
WHERE "products"."catalog_id" = 42
  AND "products"."status" = 'active';

Use `touch_all` for Efficient “Bump Updated At”

rails activerecord performance
by codesnips 4 tabs
ruby
module KeysetPageable
  extend ActiveSupport::Concern

  included do
    scope :keyset_page, ->(cursor: nil, per: 20) do
      per = per.to_i.clamp(1, 100)

Safe Pagination with Keyset (No OFFSET)

rails activerecord performance
by codesnips 3 tabs