In-memory databases

Source: Internet
Author: User

In-memory databases

In-memory databases

An SQLite database is normally stored in a single ordinary disk file. However, in certain circumstances, the database might be stored in memory.

The most common way to force an SQLite database to exist purely in memory is to open the database using the special FILENAME": Memory:". In other words, instead of passing the name of a real disk file into one of the sqlite3_open (), sqlite3_open16 (), or sqlite3_open_v2 () functions, pass in the string ": memory: ". for example:

Rc = sqlite3_open (": Memory:", & dB );

When this is done, no disk file is opened. instead, a new database is created purely in memory. the database ceases to exist as soon as the database connection is closed. every: Memory: database is distinct from every other. so, opening two database connections each with the filename ": Memory:" will create two independent in-memory databases.

The special FILENAME ": Memory:" can be used anywhere that a database filename is permitted. For example, it can be used asFilenameIn an attach command:

Attach database ': Memory:' As aux1;

Note that in order for the Special ": Memory:" name to apply and to create a pure in-memory database, there must be no additional text in the filename. thus, a disk-based database can be created in a file by prepending a pathname, like this :". /: Memory :".

The special ": Memory:" FILENAME also works when using URI filenames. For example:

Rc = sqlite3_open ("file: Memory:", & dB );

Or,

Attach database 'file: Memory: 'As aux1;


In-memory databases and shared Cache

In-memory databases are allowed to use shared cache if they are opened using a URI filename. if the unadorned ": Memory:" name is used to specify the in-memory database, then that database always has a private cache and is this only visible to the database connection that originally opened it. however, the same in-memory database can be opened by two or more database connections as follows:

Rc = sqlite3_open ("file: Memory :? Cache = shared ", & dB );

Or,

Attach database 'file: Memory :? Cache = shared 'as aux1;

This allows separate database connections to share the same in-memory database. of course, all database connections sharing the in-Memory Database need to be in the same process. the database is automatically deleted and memory is reclaimed when the last connection to the database closes.

If two or more distinct but retriable in-memory databases are needed in a single process, then the mode = memory query parameter can be used with a URI filename to create a named in-memory database:

Rc = sqlite3_open ("file: memdb1? Mode = memory & cache = shared ", & dB );

Or,

Attach database 'file: memdb1? Mode = memory & cache = shared 'As aux1;

When an in-memory database is named in this way, it will only share its cache with another connection that uses exactly the same name.

Temporary Databases

When the name of the database file handed to sqlite3_open () or to attach is an empty string, then a new temporary file is created to hold the database.

Rc = sqlite3_open ("", & dB );
Attach database ''as aux2;

A different temporary file is created each time, so that just like as with the special ": Memory:" string, two database connections to temporary databases each have their own private database. temporary databases are automatically deleted when the connection that created them closes.

Even though a disk file is allocated for each temporary database, in practice the temporary database usually resides in the in-memory pager cache and hence is very little difference between a pure in-memory database created by ": memory: "and a temporary database created by an empty filename. the sole difference is that a ": memory: "database must remain in memory at all times whereas parts of a temporary database might be flushed to disk if database becomes large or if SQLite comes under memory pressure.

The previous paragraphs describe the behavior of temporary databases under the default SQLite configuration. an application can use the temp_store Pragma and the sqlite_temp_store compile-time parameter to force temporary databases to behave as pure in-memory databases, if desired.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.