惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

IT之家
IT之家
Engineering at Meta
Engineering at Meta
腾讯CDC
宝玉的分享
宝玉的分享
H
Help Net Security
I
InfoQ
博客园 - Franky
The GitHub Blog
The GitHub Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Microsoft Security Blog
Microsoft Security Blog
M
MIT News - Artificial intelligence
博客园_首页
美团技术团队
Recent Announcements
Recent Announcements
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
雷峰网
雷峰网
The Cloudflare Blog
博客园 - 司徒正美
Vercel News
Vercel News
MyScale Blog
MyScale Blog
人人都是产品经理
人人都是产品经理
爱范儿
爱范儿
月光博客
月光博客

The Rust Programming Language Forum - Latest topics

Beginner building a Rust backend framework (AI-assisted) — feedback appreciated C++ to Rust -- Exceptions Re-exporting a trait with a custom derive_macro Gold linker is deprecated Why is using PhantomData valid in this case? Code review for Static Pool Allocator Looking for a pool based allocator Why is this legal? What does it mean for Rustc to run "out of TLS keys"? Using async/await internally with no internal runtime, but exposing a nonblocking poll API — is this a reasonable design? Testing functions that use randomness `cargo-path`: improve coding agents&#39; ability to find Rust documentation Rust task runners Lifetime weird case Windows - USB device not detected A random rustc-ice-[...].txt file appeared Develop rust where the environment is setup in a docker Undefined Behavior: in-bounds pointer arithmetic failed: attempting to offset pointer by 20 bytes, but got alloc238 which is only 1 byte from the end of the allocation Unbug 0.5 - Runtime debug assertions Is there a tiny error in section 6.2 Reference types? Lifetime woes implementing ratatui::Widget for a reference Rusqlite + Chrono: How do I simplify code to obtain chrono datetime value From OOP to Rust – struggling with code organization and data structure design Way to avoid a self-referential struct Rust RF and audio resources/communities Whyhttp - HTTP mocks that fail where the bug actually is Using tokio channel permits in a tower service Arc::increment_strong_count design question (cross-post) `&T`, `&mut T`, `Pin<&mut T>` and `&Cell<T>`: Ways of Borrowing a `T` Ratatui detect arrow key press and release
CSV to SQLite - Is it better to write records as they are...
eechris · 2026-04-19 · via The Rust Programming Language Forum - Latest topics

April 18, 2026, 9:15pm 1

I have a rust program that reads a CSV file (approx 3000 rows with 5 pieces of data / row) and inserts it into an SQLite database. Currently I have one function that reads the CSV into a vector of structures and a second function that takes the vector and writes it to an SQLite database. It works. Does it make more sense to write the data to the SQL database as it is being read from the CSV file i.e. record by record, eliminating the need to create a large vector of structures?

mroth April 18, 2026, 9:34pm 2

It depends. It depends on your requirements. Only the requirements.

So ask yourself a few questions:

  • How large will the typical CSV file be?
  • Under realistic assumptions, how large could the largest CSV file be?
  • Will you always process files, or do you also need to read from a stream?
  • What are the memory constraints of the machine your tool runs on?
  • What are the processing constraints of the machine your tool runs on?
  • And so on.

Most importantly: what do you need now?

In this context, “now” could mean this week, this month, or this year. It depends on the environment your program runs in.

kpreid April 18, 2026, 9:39pm 3

There are various factors which can affect whether one or the other is more efficient, and it's difficult to say without taking measurements.

Ways writing all the records at once can be more efficient:

  • The CPU does not have to switch between executing code to read CSVs and executing code to write SQLite, potentially making better use of both instruction cache and data cache.
  • SQLite may be more efficient at writing multiple records in a single transaction. (I don't know if this is true).

Ways writing the records one at a time can be more efficient:

  • Whenever an item is added to a vector exceeding its capacity, the vector has to grow — copying the data into a new memory allocation. If you write the records one at a time, you don't need to do this copying.
  • The process takes less memory overall, so the operating system does not have to displace other potential uses of the memory (e.g. disk cache or other processes’ memory).

A frequently useful compromise between these modes of operation is to pick a number of records to be your buffer size, and read only up to that many records before switching to writing. However, before considering complicating things this way, you should measure the performance of both of the basic strategies, on large files and small files.

1 Like

mroth April 18, 2026, 9:48pm 4

It is true. Depending on whether Write-Ahead Logging is enabled, the difference can be enormous.