Python中的内存数据库[英] in-memory database in Python

本文是小编为大家收集整理的关于Python中的内存数据库的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我正在大型数据库中在Python中进行一些查询,以从数据库中获取一些统计信息.我希望这些统计数据是内存的,因此其他程序可以使用它们而无需进入数据库.

我正在考虑如何构造它们,并在尝试设置一些复杂的嵌套词典后,我意识到一个好的表示形式将是一个SQL表.不过,我不想将数据存储回持续数据库. SQL数据库是否有任何内存实现,该数据库支持使用SQL语法查询数据?

推荐答案

sqlite3可能起作用. python接口确实支持 SQLite3 C API提供的内部内实现.

从规格:

您也可以提供特殊名称 :内存:在RAM中创建数据库.

根据您的工作,交易的价格也相对便宜.开始,只有:

import sqlite3
conn = sqlite3.connect(':memory:')

您可以像使用常规数据库一样继续进行.

取决于您的数据 - 如果您可以使用键/值(字符串,哈希,列表,集合,排序集等) - redis 可能是探索的另一个选择(如您所提到的,您想与其他程序共享).

其他推荐答案

这似乎并不明显,但是 pandas 具有很多关系功能.参见与SQL

其他推荐答案

我猜,sqlite3将是最好的选择.

如果可能的话,请看一下 memcached . (对于钥匙值对,快速照明!)

更新1:

hsqldb for SQL喜欢表. (无python支持)

本文地址:https://www.itbaoku.cn/post/597551.html

问题描述

I'm doing some queries in Python on a large database to get some stats out of the database. I want these stats to be in-memory so other programs can use them without going to a database.

I was thinking of how to structure them, and after trying to set up some complicated nested dictionaries, I realized that a good representation would be an SQL table. I don't want to store the data back into the persistent database, though. Are there any in-memory implementations of an SQL database that supports querying the data with SQL syntax?

推荐答案

SQLite3 might work. The Python interface does support the in-memory implementation that the SQLite3 C API offers.

From the spec:

You can also supply the special name :memory: to create a database in RAM.

It's also relatively cheap with transactions, depending on what you are doing. To get going, just:

import sqlite3
conn = sqlite3.connect(':memory:')

You can then proceed like you were using a regular database.

Depending on your data - if you can get by with key/value (strings, hashes, lists, sets, sorted sets, etc) - Redis might be another option to explore (as you mentioned that you wanted to share with other programs).

其他推荐答案

It may not seem obvious, but pandas has a lot of relational capabilities. See comparison with SQL

其他推荐答案

I guess, SQLite3 will be the best option then.

If possible, take a look at memcached. (for key-value pair, lighting fast!)

UPDATE 1:

HSQLDB for SQL Like tables. (no python support)