{"id":19836542,"url":"https://github.com/wawandco/gorm-batch-insert","last_synced_at":"2026-05-30T23:31:47.444Z","repository":{"id":227733117,"uuid":"772267761","full_name":"wawandco/gorm-batch-insert","owner":"wawandco","description":"Batch Insert Example using Gorm","archived":false,"fork":false,"pushed_at":"2024-04-12T17:53:14.000Z","size":22,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-02-28T22:05:44.494Z","etag":null,"topics":["batch-insert","go","gorm"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/wawandco.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-03-14T21:21:43.000Z","updated_at":"2024-05-09T20:29:00.000Z","dependencies_parsed_at":null,"dependency_job_id":"6e47a1e4-66b9-47f1-b427-d433cdfe7d5c","html_url":"https://github.com/wawandco/gorm-batch-insert","commit_stats":null,"previous_names":["wawandco/gorm-batch-insert"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/wawandco/gorm-batch-insert","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wawandco%2Fgorm-batch-insert","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wawandco%2Fgorm-batch-insert/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wawandco%2Fgorm-batch-insert/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wawandco%2Fgorm-batch-insert/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/wawandco","download_url":"https://codeload.github.com/wawandco/gorm-batch-insert/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wawandco%2Fgorm-batch-insert/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":33714033,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-05-30T02:00:06.278Z","response_time":92,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["batch-insert","go","gorm"],"created_at":"2024-11-12T12:11:39.252Z","updated_at":"2026-05-30T23:31:47.429Z","avatar_url":"https://github.com/wawandco.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# GORM vs Go/SQL Batch Insert Benchmark 🧪\n\nThis is a benchmark comparison between the go/sql approach used in this post: [https://wawand.co/blog/posts/go-multi-inserting-in-batches/](https://wawand.co/blog/posts/go-multi-inserting-in-batches/)\n\nInstead of using the sql library provided by Go, this time we're using an ORM called Gorm. Gorm v2.0 was announced a little while ago and one of the things that was mentioned was the [Batch insert support](https://gorm.io/docs/v2_release_note.html#Batch-Insert)\n\n## Requirements 🔎️\n\nTo run the benchmark tests, you need to have the following things installed:\n\n- Go (This project uses version 1.22)\n- PostgresApp\n- PSQL\n- Go Benchstat\n\n## Getting Started ⚙️\n\nYou will need to create an `.env` file and add the following env variables we're using for this project:\n\n```env\nDB_USER\nDB_PASSWORD\nDB_HOST\nDB_PORT\nDB_NAME\nBATCH_SIZE\n```\n\nThe first five are used to build the DB's Data Source Name (DSN) for GORM and the database URL for the go/sql package. The `BATCH_SIZE` serves to configure the size of the group you want to use when inserting in batches(this affects both approaches).\n\n## Running the benchmarks 📋\n\nBoth tests have the same name, hence why one of them is commented at the moment(since you can't have tests with the same name under the same package). They're also in the same package. The reason for all of that is to have benchstat analyze the results for both approaches in the form of an A/B test. So we run one benchmark after the other, saving the results to a text file and then have benchstat compare the results for us.\n\nTo run the benchmark test, use the following command:\n\n```sh\n$ GOMAXPROCS=2 go test -bench=Batches -timeout 30m -count 6 -benchtime=20x ./internal/benchmark | tee results/gorm-bench.txt\n```\n\n\u003e ⚠️ Don't forget to change the name of the output file when trying to run the other benchmark.\n\n`GOMAXPROCS` will tell the test suite to utilize two CPU cores to perform the benchmarks. We're also passing a `timeout` of `30m` since we're inserting quite the amount of records and don't want our benchmark to be interrupted(It's `11m` by default).\n\nWith the `-count` flag, we're asking the benchmark to run each scenario six times, each will have 20 loops to be executed, which is specified through the `benchtime` flag.\n\nThe `tee` command will help us see the results of our benchmarks while saving the results to a text file, so we can pass it to `benchstat` for the comparison.\n\n## Results\n\nTo see the comparison between the two approaches, we used `benchstat` in the following fashion:\n\n```sh\n$ benchstat results/gsql-bench.txt results/gorm-bench.txt\n```\n\nBoth files can be found in the `results` folder but feel free to generate your own if you please. Running the benchstat command should give you results in a the similar shape:\n\n| -                                              | results/gsql-bench.txt | results/gorm-bench.txt |         Delta         |\n| ---------------------------------------------- | ---------------------- | ---------------------- | :-------------------: |\n| -                                              | sec/op                 | sec/op vs base         |           -           |\n| SaveContactsInBatches/records_number_100-2     | 1438.0µ ± 21%          | 974.8µ ± 20%           | -32.22% (p=0.004 n=6) |\n| SaveContactsInBatches/records_number_1000-2    | 5.606m ± 30%           | 5.029m ± 16%           |    ~ (p=0.093 n=6)    |\n| SaveContactsInBatches/records_number_10000-2   | 73.15m ± 25%           | 58.86m ± 4%            |    ~ (p=0.065 n=6)    |\n| SaveContactsInBatches/records_number_100000-2  | 545.5m ± 15%           | 309.6m ± 30%           | -43.25% (p=0.002 n=6) |\n| SaveContactsInBatches/records_number_300000-2  | 1.615 ± 2%             | 1.014 ± 8%             | -37.26% (p=0.002 n=6) |\n| SaveContactsInBatches/records_number_500000-2  | 2.591 ± 6%             | 1.790 ± 12%            | -30.91% (p=0.002 n=6) |\n| SaveContactsInBatches/records_number_1000000-2 | 5.130 ± 6%             | 3.786 ± 4%             | -26.19% (p=0.002 n=6) |\n| geomean                                        | 183.1m                 | 129.6m                 |        -29.24%        |\n\n## Next Steps? 🧐\n\nIt seems that there is yet another alternative to this. From what I've read here and there, pgx has support for a [COPY protocol](https://pkg.go.dev/github.com/jackc/pgx/v4#hdr-Copy_Protocol). I haven't looked at it deeply, but perhaps that can be our next target in this series.\n\nBuilt with ❤️ by Wawandco\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwawandco%2Fgorm-batch-insert","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwawandco%2Fgorm-batch-insert","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwawandco%2Fgorm-batch-insert/lists"}