Using sqlc Instead of an ORM in Go
Why I moved from ORMs to sqlc on my Go projects: plain SQL compiled into type-safe Go code, and what it did for query speed and runtime mapping bugs.
· 2 min read · Erik Kubica
For a long time I was a happy ORM user. In PHP, I used Doctrine and Eloquent. In JavaScript, I used TypeORM and Prisma. In early Go projects, I reached for GORM.
Every ORM promises two things: you rarely have to write raw SQL, and you can swap your database engine if you ever want to.
On the projects I've worked on over 13 years, I can't remember a client swapping PostgreSQL for MySQL. What I did see fairly often was ORM query builders generating multi-join queries without a matching index, which turned a 3ms index scan into a 280ms query on one project.
The Cost of Reflection
The ORM's SQL is not the only thing that bothered me. There is also what happens inside the application after the database returns data.
When GORM or Eloquent hydrates 5,000 rows into entity objects, it relies on runtime reflection. It parses struct tags, allocates intermediate interface wrappers, and validates property types in memory. On one busy API endpoint, that came to roughly 14 MB of heap allocations per request and CPU usage around 35% on a standard production server.
In that case I was paying for a bigger server partly to map records the database had already structured for me.
Compiling SQL to Native Go
About two years ago I switched to sqlc on my Go projects.
sqlc works the other way around. Instead of writing Go code that generates SQL at runtime, you write plain SQL that generates Go code at compile time:
-- name: GetActiveSubscriptions :many
-- Pure SQL with compile-time type validation
SELECT
id,
user_id,
plan_tier,
monthly_price_eur,
created_at
FROM subscriptions
WHERE status = $1
ORDER BY created_at DESC;
When I run sqlc generate, the tool parses my Postgres schema DDL and generates type-safe Go structs and query methods:
// Code generated by sqlc. DO NOT EDIT.
type GetActiveSubscriptionsRow struct {
ID int64
UserID int64
PlanTier string
MonthlyPriceEur int32
CreatedAt time.Time
}
func (q *Queries) GetActiveSubscriptions(ctx context.Context, status string) ([]GetActiveSubscriptionsRow, error) {
rows, err := q.db.QueryContext(ctx, getActiveSubscriptions, status)
// Direct scanning without runtime reflection or interface allocations
}
The Numbers I Saw
Here is roughly what I measured on my setup when comparing ORM hydration with sqlc generated scanners. Your numbers will depend on the schema, the driver and the ORM, so treat this as one data point:
| Execution Method | 5,000 Rows Latency | Memory Allocations | CPU Overhead |
|---|---|---|---|
| GORM / Prisma | 48.2 ms | 14.2 MB / req | 28% CPU |
| sqlc (Direct Scan) | 1.9 ms | 0.4 MB / req | 3% CPU |
Because sqlc uses standard database driver scanners (rows.Scan(&row.ID, &row.UserID...)), there is no reflection involved, and memory allocations were much lower.
The part I like most has little to do with speed. If I make a typo in a column name or pass an integer where Postgres expects a UUID, sqlc generate fails in my terminal, so I catch those errors before the code builds.
It is not free, though. You write more SQL by hand, and for small apps or quick prototypes an ORM can still be the quicker option. If you actually need to support several database engines, the ORM abstraction is also doing real work for you.