我有一个100兆字节的sqlite db文件,我想在执行sql查询之前加载到内存中。在python中可以做到这一点吗?
谢谢
发布于 2010-09-30 07:20:44
apsw是sqlite的替代包装器,它允许您在执行操作之前将磁盘上的数据库备份到内存。
从docs
###
### Backup to memory
###
# We will copy the disk database into a memory database
memcon=apsw.Connection(":memory:")
# Copy into memory
with memcon.backup("main", connection, "main") as backup:
backup.step() # copy whole database in one go
# There will be no disk accesses for this query
for row in memcon.cursor().execute("select * from s"):
pass上面的connection是你的磁盘数据库。
发布于 2010-09-30 07:19:22
运行内存中的数据库(标准的磁盘数据库(file).
)
下面是Tcl中的一个例子(取from here) (对于理解大意可能很有用):
proc loadDB {dbhandle filename} {
if {$filename != ""} {
#attach persistent DB to target DB
$dbhandle eval "ATTACH DATABASE '$filename' AS loadfrom"
#copy each table to the target DB
foreach {tablename} [$dbhandle eval "SELECT name FROM loadfrom.sqlite_master WHERE type = 'table'"] {
$dbhandle eval "CREATE TABLE '$tablename' AS SELECT * FROM loadfrom.'$tablename'"
}
#create indizes in loaded table
foreach {sql_exp} [$dbhandle eval "SELECT sql FROM loadfrom.sqlite_master WHERE type = 'index'"] {
$dbhandle eval $sql_exp
}
#detach the source DB
$dbhandle eval {DETACH loadfrom}
}
}发布于 2014-08-27 15:52:50
如果您使用的是Linux,可以尝试使用tmpfs,它是一个基于内存的文件系统。
它非常容易使用:
将tmpfs装载到directory.
请记住,tmpfs中的所有内容都将在重新启动后丢失。因此,如果db文件发生更改,您可以将其复制回磁盘。
https://stackoverflow.com/questions/3826552
复制相似问题