In my last article I wrote an unnecessary JOIN by hand to see what it cost. That test came from a worry: after a long time of letting GORM write my SQL, I didn't trust myself to write it efficiently.
I ended that article with a promise to check the other half of the worry, which was my assumption that GORM's own SQL is efficient.
So I took the same task and wrote it three ways in GORM. The version I would have reached for at work, GORM's association joins, generated the same wasteful join I had been afraid of writing myself. It also finished first in every run with default settings, which makes it easy to miss.
Limited to one CPU core, it took about twice as long as the alternatives.
The task, written three ways
The data is the same as last time: users , addresses (one per user, with a city ) and orders , with 5 million rows in each.…