Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I run LocalDb "in-memory"

Tags:

localdb

I am using LocalDb in an integration testing environment - instantiating and disposing of my instance before and after my tests run.

However, when I create a database within my instance, it still flushes my tables and data to disk. Is it possible to run LocalDb in an "in-memory" mode? And if so how?

like image 277
Lawrence Avatar asked Apr 02 '15 16:04

Lawrence


People also ask

Can I use LocalDB in production?

LocalDB is absolutely supported in production. From the performance point of view it is identical to SQL Server Express, as they share the same database engine. Now, for a site running in full IIS I would recommend using service-based instance of SQL Server Express.

Where is SQL LocalDB stored?

The system database files for the database are stored in the local AppData path, which is normally hidden. For example, C:\Users\<user>\AppData\Local\Microsoft\Microsoft SQL Server Local DB\Instances\LocalDBApp1\ .

Should I use in memory database for testing?

This database provider allows Entity Framework Core to be used with an in-memory database. While some users use the in-memory database for testing, this is generally discouraged; the SQLite provider in in-memory mode is a more appropriate test replacement for relational databases.

What is the difference between LocalDB and SQL Express?

LocalDB is a lightweight version of the SQL Server Express Database Engine that is targeted for program development. LocalDB starts on demand and runs in user mode, so there is no complex configuration. SQL Server is the Microsoft-driven relational database management system.


2 Answers

You could run your tests against a database in a RAM disk. I have done that a while ago and it seemed to improve integration tests performance by a factor of 2 or 3! This was on a Windows 7 VM hosted in a MacBook Pro with an SSD. Your milage will probably be better if you have a mechanical HDD.

Since SQL Server allows you to specify the mdf file in a connection string via "AttachDbFileName=", you can leverage that by pointing to an mdf in a RAM disk.

The RAM disk device driver I used was ImDisk, which is available for both 64 and 32 bit Windows. Linux counterparts are numerous.

like image 138
Ameer Gaafar Avatar answered Oct 13 '22 12:10

Ameer Gaafar


No. LocalDB is still SQL Server, and in SQL Server there is no concept of an in-memory database. All databases are disk-backed, with in-memory cache sitting on top of them.

You could probably write a custom step in your testing harness to drop your databases after your tests are completed and delete database files. Maybe even it's already there if you're using TFS for build and test runs? But there's nothing in LocalDB to make it automatic.

like image 40
Krzysztof Kozielczyk Avatar answered Oct 13 '22 14:10

Krzysztof Kozielczyk