跳至主要內容

DuckDB(嵌入式 OLAP)

nicye大约 1 分钟约 406 字

介绍

DuckDB 是一款进程内分析数据库,它可以在无需维护分布式多服务器系统的情况下处理出人意料的大型数据集。

DuckDB has two configurable options for concurrency:

  • One process can both read and write to the database.
  • Multiple processes can read from the database, but no processes can write (access_mode = 'READ_ONLY').

DuckDB | 官网 open in new window

安装包

FreeSql.Provider.Duckdb

.NET CLI

dotnet add package FreeSql.Provider.Duckdb

Package Manager

Install-Package FreeSql.Provider.Duckdb

声明

static IFreeSql fsql = new FreeSql.FreeSqlBuilder()
    .UseConnectionString(FreeSql.DataType.DuckDB, "DataSource = train_services.db")
    .UseMonitorCommand(cmd => Console.WriteLine($"Sql:{cmd.CommandText}"))
    .UseAutoSyncStructure(true) //自动创建表
    .Build();

依赖的第三方 ado.net 驱动源代码:https://github.com/Giorgi/DuckDB.NET

Connection StringDescription
DataSource = :memory:Connect to a new in-memory database
DataSource = :memory:?cache=sharedConnect to a shared, in-memory database
DataSource = train_services.dbConnect to train_services.db
DataSource = train_services.db;ACCESS_MODE=READ_ONLYConnect to train_services.db, make connection read-only
DataSource = :memory:;threads=8;ACCESS_MODE=READ_ONLYConnect to a new in-memory database, limit threads to 8, make connection read-only
DataSource = train_services.db;ACCESS_MODE=READ_ONLY;memory_limit=10GBConnect to train_services.db, make connection read-only, limit RAM usage to 10GB

类型映射

CSharpDuckDB说明
bool/bool?BOOLEANlogical boolean (true/false)
sbyte/sbyte?TINYINTsigned one-byte integer
short/short?SMALLINTsigned two-byte integer
int/int?INTEGERsigned four-byte integer
long/long?BIGINTsigned eight-byte integer
byte/byte?UTINYINT
ushort/ushort?USMALLINT
uint/uint?UINTEGER
ulong/ulong?UBIGINT
double/double?DOUBLEdouble precision floating-point number (8 bytes)
float/float?FLOATsingle precision floating-point number (4 bytes)
decimal/decimal?DECIMAL(10,2)fixed-precision number with the given width (precision) and scale
TimeSpan/TimeSpan?TIMEtime of day (no time zone)
DateTime/DateTime?TIMESTAMPcombination of time and date
TimeOnly/TimeOnly?TIME
DateOnly/DateOnly?DATEcalendar date (year, month day)
byte[]BLOBvariable-length binary data
stringVARCHAR(255)variable-length character string
charCHAR(1)
Guid/Guid?UUIDUUID data type
BitArrayBITstring of 1s and 0s
BigInteger/BigInteger?HUGEINTsigned sixteen-byte integer
T[]ARRAY/LIST如 int[]、string[],不能是可空 int?[]
class + JsonMapSTRUCT{'i': 42, 'j': 'a'}
Dictionary<TKey, TValue>MAPmap([1, 2], ['a', 'b'])

自定义映射,请移步【类型映射】文档。