简体   繁体   English

分配给SQLite内存数据库的内存大小

[英]Size of memory allocated to a SQLite in-memory database

If a create a in-memory sqlite database using syntax below, then what is the size of maximum memory allocated to it? 如果使用以下语法创建内存中的sqlite数据库,那么分配给它的最大内存大小是多少?

my $dbh = DBI->connect('dbi:SQLite:dbname=:memory:');

What will happen if size of in-memory database becomes greater than max available memory. 如果内存数据库的大小大于最大可用内存,将会发生什么。

Suppose I have 1 GB free and the database size become 1.1 GB, then will some data be written to disk or what? 假设我有1 GB的可用空间,并且数据库大小变为1.1 GB,那么将一些数据写入磁盘还是什么?

Is there a way to change the value of max-memory allocated to a in-memory sqlite database? 有没有办法更改分配给内存sqlite数据库的最大内存值?

It will use virtual memory, yes it will write out to the disk once you've exceeded all your freely available memory, when that's exceeded, it may throw an exception, have an undefined behavior, or it may be OS dependent. 它会使用虚拟内存,是的,一旦超出所有可用内存,它就会写出到磁盘上;超过该数量时,它可能会引发异常,具有不确定的行为,或者可能取决于操作系统。 These are SQLite limits: http://www.sqlite.org/limits.html . 这些是SQLite限制: http : //www.sqlite.org/limits.html

If you want to set a maximum database size you can execute the following SQL command: 如果要设置最大数据库大小,则可以执行以下SQL命令:

PRAGMA page_size = 512
PRAGMA max_page_count = 195313

In Perl: 在Perl中:

$dbh = DBI->connect('dbi:SQLite:dbname=:memory:');
$query_handle = $dbh->prepare("PRAGMA page_size = 512");
$query_handle->execute();
$query_handle = $dbh->prepare("PRAGMA max_page_count = 195313");
$query_handle->execute();

You can test it by creating a temporary table and using a loop to insert a large number of objects, until it hits the limit. 您可以通过创建一个临时表并使用循环插入大量对象直到达到极限来对其进行测试。

This will set your max database size to 100,000,256 bytes. 这会将您的最大数据库大小设置为100,000,256字节。

SQlite database are stored in pages, this will set the page size and the max number of pages for your database. SQlite数据库存储在页面中,这将设置数据库的页面大小和最大页面数。 you can play with either parameter to suit your needs; 您可以使用任一参数来满足您的需要; bigger pages means more performance, but faster growth. 较大的页面意味着更高的性能,但增长更快。

These are all the PRAGMAS that sqlite supports: http://www.sqlite.org/pragma.html#pragma_page_size . 这些都是sqlite支持的PRAGMAS: http ://www.sqlite.org/pragma.html#pragma_page_size。

This is a simple test in Python: 这是Python中的一个简单测试:

import sqlite3

con = sqlite3.connect(':memory:')
cur = con.cursor() 
cur.execute("PRAGMA max_page_count = 195313")
cur.execute("PRAGMA page_size = 512")
cur.execute("CREATE TABLE test (random_values TEXT)")
index = 0
query = "INSERT INTO test (random_values) VALUES ('%s')" % "".join(['a' for index in xrange(1000)])
while True:
    try:
        c = cur.execute(query)
        index += 1
    except Exception as ex:
        print str(ex)  
        break       

print index * 1000

Fairly quickly you'll get something like this: 很快,您会得到以下信息:

sqlite3.OperationalError: database or disk is full sqlite3.OperationalError:数据库或磁盘已满

I get 93915000 bytes due to the overhead, so play with the settings if you want exactly 100 megabytes. 由于开销,我得到了93915000字节,因此,如果要精确到100兆字节,请使用设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM