{"success":true,"name":"batch-sql-inserts-instead-of-one-row-per-statement","description":"Writing code that inserts many rows into SQL in a loop.","category":"ai-brimind","path":"ai-brimind/batch-sql-inserts-instead-of-one-row-per-statement/SKILL.md","file":"SKILL.md","content":"---\nname: batch-sql-inserts-instead-of-one-row-per-statement\ndescription: \"Writing code that inserts many rows into SQL in a loop.\"\n---\n\n# Batch SQL INSERTs Instead of One Row Per Statement\n\n## When to use\nProject uses SQL, and you are inserting hundreds or more rows and about to execute one INSERT statement per row.\n\n## Example request\n10,000 single INSERTs in a loop → each one pays a network round trip; multi-row INSERT cuts it to a handful. Importing a CSV row by row → the database has a bulk loader (COPY, LOAD DATA); the built-in path is orders of magnitude faster. Each insert auto-committed → 10,000 tiny transactions; one transaction around the batch changes everything. Batch of 500,000 rows in one statement → the packet or memory limit breaks it; batches have a sweet-spot size.\n\n## Instructions\nAct like a logistics planner who ships pallets, not single envelopes. Before writing an insert loop: count the rows — beyond a few dozen, switch to multi-row INSERT statements in chunks (commonly 500–1,000 rows), wrapped in a transaction, or use the database's native bulk mechanism (COPY in PostgreSQL, LOAD DATA in MySQL) for real imports. Check whether your ORM has a bulk-create method before hand-rolling one. Plan failure handling per chunk: know whether a bad row should fail the chunk or be skipped and logged. Consider deferring index-heavy work if the load is massive. Only ship once you can state the number of round trips and transactions the import now makes, and why that number no longer scales one-to-one with rows.\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #1065\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}