2024/08/17

MonetDB

MonetDB is an open-source column-oriented relational database management system (RDBMS) originally developed at the Centrum Wiskunde & Informatica (CWI) in the Netherlands. It is designed to provide high performance on complex queries against large databases, such as combining tables with hundreds of columns and millions of rows.

MonetDB architecture is represented in three layers, each with its own set of optimizers. The front end is the top layer, providing query interface for SQL. Queries are parsed into domain-specific representations, like relational algebra for SQL, and optimized. The generated logical execution plans are then translated into MonetDB Assembly Language (MAL) instructions, which are passed to the next layer. The middle or back-end layer provides a number of cost-based optimizers for the MAL. The bottom layer is the database kernel, which provides access to the data stored in Binary Association Tables (BATs). Each BAT is a table consisting of an Object-identifier and value columns, representing a single column in the database.

MonetDB internal data representation also relies on the memory addressing ranges of contemporary CPUs using demand paging of memory mapped files, and thus departing from traditional DBMS designs involving complex management of large data stores in limited memory.

MonetDB is primarily an analytical (OLAP) database, where most operations are SELECT-FROM-WHERE-GROUPBY queries. With that in mind, its transaction management scheme is tuned for reading large chunks of data, rather than writing. The MonetDB transaction management is based on optimistic concurrency control (OCC). OCC is particularly useful for read-heavy applications, with the design goal that transaction management overhead should only be paid when necessary.


使用 RPM 安裝以後,使用下列的方式開啟資料庫服務:

sudo systemctl start monetdbd.service

使用下列的方式查詢目前的狀態:

sudo systemctl status monetdbd.service

使用下列的方式停止服務:

sudo systemctl stop monetdbd.service

下面建立一個 demo 資料庫。

sudo monetdb create demo

還需要 release 才能夠使用:

sudo monetdb release demo

然後就可以使用 mclient 連線進行測試:

mclient -u monetdb -d demo

一般而言可以將 user/password 記錄在 .monetdb 中,這樣就不需要輸入帳號密碼,只需要提供資料庫名稱。 下面是內容設定的例子:

user=monetdb
password=monetdb

如果想要刪除資料庫,可以使用 stop 與 destroy:

sudo monetdb stop demo
sudo monetdb destroy demo

使用下列的指令建立一個使用者 danilo:

CREATE USER "danilo" WITH PASSWORD 'danilo' NAME 'Danilo' SCHEMA "sys";
CREATE SCHEMA "danilo" AUTHORIZATION "danilo";
ALTER USER "danilo" SET SCHEMA "danilo";

使用 mclient 連線驗證:

mclient -u danilo demo

使用指令查詢 MonetDB 的版本資訊:

select value from sys.environment where name = 'monet_version';

ODBC

MonetDB 提供了 ODBC driver,下面是使用 Tcl TDBC 取得目前 MonetDB 目前版本的測試程式。

package require tdbc::odbc

set connStr "DSN=MonetDB;UID=monetdb;PWD=monetdb;"
if {[catch {tdbc::odbc::connection create db $connStr}]} {
   puts "Connection failed."
   exit
}

set statement [db prepare {select value from sys.environment where name = 'monet_version'}]

puts "Fetch result:"
$statement foreach row {
    puts "version: [dict get $row value]"
}

$statement close

db close

下面是一個使用 SOCI ODBC 取得 MonetDB 版本的測試程式:

#include <soci/odbc/soci-odbc.h>
#include <soci/soci.h>

#include <iostream>
#include <string>

int main() {
    try {
        soci::session sql("odbc", "DSN=MonetDB;UID=monetdb;PWD=monetdb;");

        std::string version;
        soci::rowset<std::string> rs =
            (sql.prepare << "select value from sys.environment where name = "
                            "'monet_version'");

        for (soci::rowset<std::string>::const_iterator it = rs.begin();
             it != rs.end(); ++it) {
            std::cout << *it << std::endl;
        }
    } catch (soci::soci_error const &e) {
        std::cerr << "Failed: " << e.what() << std::endl;
    } catch (std::runtime_error const &e) {
        std::cerr << "Unexpected standard exception occurred: " << e.what()
                  << std::endl;
    } catch (...) {
        std::cerr << "Unexpected unknown exception occurred." << std::endl;
    }

    return 0;
}

使用下列的指令編譯:

g++ version.cpp -lsoci_core -lsoci_odbc -o version

MonetDB/e Embedded

MonetDB/e is the embedded version of MonetDB, embed the power of an analytical SQL database engine in your Python or C/C++ applications.

相關連結

沒有留言:

張貼留言

注意:只有此網誌的成員可以留言。