analitics

Pages

Friday, September 6, 2024

Python 3.12.4 : DuckDB operate in in-memory mode.

DuckDB aims to automatically achieve high performance by using well-chosen default configurations and having a forgiving architecture. Of course, there are still opportunities for tuning the system for specific workloads. The Performance Guide's page contain guidelines and tips for achieving good performance when loading and processing data with DuckDB.
DuckDB can operate in in-memory mode. In most clients, this can be activated by passing the special value :memory: as the database file or omitting the database file argument.
You can use with python or another programming language.
I used Python 3.13.0rc1 version and pip tool ...
pip install duckdb --upgrade
Collecting duckdb
...
Installing collected packages: duckdb
Successfully installed duckdb-1.0.0
I created a python script :
python test_memory_duckdb_sqlite_db.py
[]
[('table', 'users', 'users', 0, 'CREATE TABLE users(id BIGINT PRIMARY KEY, first_name VARCHAR, last_name VARCHAR, occupation VARCHAR, hobby VARCHAR, year_of_birth BIGINT, age BIGINT);')]
This is the source code I used:
import duckdb

# Conectare la baza de date în memorie
con = duckdb.connect(database=':memory:')

# Instalează extensia sqlite
con.execute("INSTALL sqlite")

# Încarcă extensia sqlite
con.execute("LOAD sqlite")

# Verifică dacă extensia este încărcată
result = con.execute("SELECT * FROM sqlite_master").fetchall()
print(result)

# Conectare la baza de date pe disc
con_disk = duckdb.connect(database='sqlite_database.db', read_only=False)

# Instalează extensia sqlite pentru baza de date pe disc
con_disk.execute("INSTALL sqlite")

# Încarcă extensia sqlite pentru baza de date pe disc
con_disk.execute("LOAD sqlite")

# Verifică dacă extensia este încărcată pentru baza de date pe disc
result_disk = con_disk.execute("SELECT * FROM sqlite_master").fetchall()
print(result_disk)