r/SQL • u/Valuable-Ant3465 • Jun 30 '26
SQL Server Tricky interview question about .ldf size on MS SQL Server
Hi, I was applying for SQL dev position and got this question:
Q: you need to import 2.5T .csv file into MS SQL Server table. What approach you would use to avoid problems with log file size restrictions.
Didn't have experience with this case. I think if you create on the fly package with Right click import, everything will be taken care of. Am I right ? or I can somehow control /shrink .ldf file or break .csv into several pieces.?
Thanks all
VA
10
Upvotes
17
u/kagato87 MS SQL Jun 30 '26
If you import that much data in one go the prod team will be out for your blood, with the prod-dba leading the charge. The storage team would be racing to get you, for that matter. A question like that is a probe to see if you're at least cognizant of the impacts your actions can make, and it's a pretty good one to ask, even for a dev-dba position.
The proper response is... Complex. Batching is only half the answer, while the other half involves you asking questions about the recovery model and backup schedule.
8
u/lmarcantonio Jun 30 '26
There's a BULK INSERT operation which conveniently doesn't fill the log and accept cvs files... there's nothing tricky, it's a t-sql statement created exactly for that
3
u/scoinv6 Jun 30 '26
My question to the question would be, "Have someone here ever filled up a log drive in production because they imported a lot of data?" I want to sniff out the chaos of the environment before I decide to take a job there. Not test a data import process before going into production is a red flag.
2
u/TheMagarity Jun 30 '26
This may be a convoluted way of seeing if you know to tell the import process to commit every n records so a single log file doesn't fill up. The system will do a good job of automatically dealing with log files as long as one supersize commit doesn't exceed a single log file size. Right?
2
u/SingingTrainLover Jun 30 '26 edited Jun 30 '26
First, batch your import, so the transaction size (of each batch) stays within the target log file size.
If your target database is in Bulk Logged or Full recovery mode, make sure the transaction log backups support the rate of import. (You can set alerts to trigger a log backup if the log exceeds a specified threshold.)
If it's in Simple recovery mode, you don't have to worry about the backups, the log will clear completed transactions automatically.
NEVER, EVER, delete your transaction log!! Doing this will corrupt your database. If you want people mad at you, this is one way to do so.
All transactions, regardless of recovery mode, log to the transaction log. This is native behavior of SQL Server, and you can't change it.
tl;dr Batch load using bcp or Copy-DbaDbTableData in PowerShell/dbatools, and set a batch size limit
Edit: remove arrogant comment about other posts. Not relevant to the OP's question.
11
u/No_Resolution_9252 Jun 30 '26
bulk insert with batch size I think is what they were getting at. If bulk logging is enabled it would minimize the amount of logging, but having a DB that is actually in bulk logging is pretty rare now
4
u/Better-Credit6701 Jun 30 '26
I know the answer they are looking for, use bulk logging, but that can't be done in all cases. For example, importing 2.5T of data is just another Tuesday (or pick any day of the week but it is Tuesday today). And we use always on which requires full. We do keep track of the log size and virtual log numbers and have procedures for handling it when it starts growing.
1
u/Disastrous_Fill_5566 Jul 01 '26
You'd also want to check the recovery model - assuming it was Full, you'd need to make sure that transactional backups were taken often enough to be able to prevent the .ldf file from getting so big it doesn't fit on disk. And you'd have to check that the volume where the backups are being taken is big enough to store all the .trn files.
And of course, check that the drive containing the actual data is big enough hold the database when it's all imported. And I'd rebuild the stats once the import is complete.
This to me sounds like the kind of question that's an entry point for discussing all sorts of issues associated with that kind of scale. I'm sure there are lots of places where this isn't considered a lot of data, but in order to operate in the terabyte scale, there are lots of aspects that need to be considered compared to tens of Gb, which is fine for lots of applications.
2
u/[deleted] Jun 30 '26
[removed] — view removed comment