connect to pdbbench

CREATE TABLE application ( \
	id						INT NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, \
	name					VARCHAR(100) NOT NULL, \
	version					VARCHAR(100) DEFAULT '1.0', \
	description				CLOB, \
	language				VARCHAR(100), \
	paradigm				VARCHAR(100), \
	usage_text				CLOB, \
	execution_options		CLOB, \
	userdata				CLOB \
)

CREATE TABLE experiment ( \
    id                      INT NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, \
    application             INT             NOT NULL, \
    name                    VARCHAR(100), \
	system_name             VARCHAR(100), \
	system_machine_type     VARCHAR(100), \
	system_arch             VARCHAR(100), \
	system_os               VARCHAR(100), \
	system_memory_size      VARCHAR(100), \
	system_processor_amt    VARCHAR(100), \
	system_l1_cache_size    VARCHAR(100), \
	system_l2_cache_size    VARCHAR(100), \
    system_userdata         CLOB, \
	compiler_cpp_name       VARCHAR(100), \
	compiler_cpp_version    VARCHAR(100), \
	compiler_cc_name        VARCHAR(100), \
	compiler_cc_version     VARCHAR(100), \
	compiler_java_dirpath   VARCHAR(100), \
	compiler_java_version   VARCHAR(100), \
    compiler_userdata       CLOB, \
	configure_prefix        VARCHAR(100), \
	configure_arch          VARCHAR(100), \
	configure_cpp           VARCHAR(100), \
	configure_cc            VARCHAR(100), \
	configure_jdk           VARCHAR(100), \
	configure_profile       VARCHAR(3), \
    configure_userdata      CLOB, \
    userdata                CLOB, \
	FOREIGN KEY(application) REFERENCES application(id) ON DELETE NO ACTION \
)

CREATE TABLE trial ( \
    id                      INT NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, \
    name                    VARCHAR(100), \
    experiment              INT             NOT NULL, \
    time                    TIMESTAMP,       \
    problem_definition      CLOB, \
    node_count              INT, \
    contexts_per_node       INT, \
    threads_per_context     INT, \
    userdata                CLOB, \
	FOREIGN KEY(experiment) REFERENCES experiment(id) ON DELETE NO ACTION \
)

CREATE TABLE metric ( \
    id                      INT NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, \
    name                    CLOB            NOT NULL, \
    trial                   INT				NOT NULL, \
	FOREIGN KEY(trial) REFERENCES trial(id) ON DELETE NO ACTION \
)

CREATE TABLE function ( \
    id                      INT NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, \
    trial                   INT             NOT NULL, \
    name                    CLOB            NOT NULL, \
    group_name              VARCHAR(50), \
	FOREIGN KEY(trial) REFERENCES trial(id) ON DELETE NO ACTION \
)

CREATE TABLE user_event ( \
    id                      INT NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, \
    trial                   INT              NOT NULL, \
    name                    CLOB             NOT NULL, \
    group_name              VARCHAR(50), \
	FOREIGN KEY(trial) REFERENCES trial(id) ON DELETE NO ACTION \
)

CREATE TABLE interval_location_profile ( \
    id                      INT NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, \
    function                INT              NOT NULL, \
    node                    INT              NOT NULL,              \
    context                 INT              NOT NULL, \
    thread                  INT              NOT NULL, \
    metric                  INT				 NOT NULL, \
    inclusive_percentage    DOUBLE, \
    inclusive               DOUBLE, \
    exclusive_percentage    DOUBLE, \
    exclusive               DOUBLE, \
    call                    DOUBLE, \
    subroutines             DOUBLE, \
    inclusive_per_call      DOUBLE, \
	FOREIGN KEY(function) REFERENCES function(id) ON DELETE NO ACTION, \
	FOREIGN KEY(metric) REFERENCES metric(id) ON DELETE NO ACTION \
)

CREATE TABLE atomic_location_profile ( \
    id                      INT NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, \
    user_event              INT              NOT NULL, \
    node                    INT              NOT NULL,              \
    context                 INT              NOT NULL, \
    thread                  INT              NOT NULL, \
    sample_count            INT    ,          \
    maximum_value           DOUBLE, \
    minimum_value           DOUBLE, \
    mean_value              DOUBLE, \
    standard_deviation      DOUBLE, \
	FOREIGN KEY(user_event) REFERENCES user_event(id) ON DELETE NO ACTION \
)

CREATE TABLE interval_total_summary ( \
    id                		INT NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, \
    function                INT              NOT NULL, \
    metric                  INT				 NOT NULL, \
    inclusive_percentage    DOUBLE, \
    inclusive               DOUBLE, \
    exclusive_percentage    DOUBLE, \
    exclusive               DOUBLE, \
    call                    DOUBLE, \
    subroutines             DOUBLE, \
    inclusive_per_call      DOUBLE, \
	FOREIGN KEY(function) REFERENCES function(id) ON DELETE NO ACTION, \
	FOREIGN KEY(metric) REFERENCES metric(id) ON DELETE NO ACTION \
)

CREATE TABLE interval_mean_summary ( \
    id                		INT NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, \
    function                INT              NOT NULL, \
    metric                  INT				 NOT NULL, \
    inclusive_percentage    DOUBLE, \
    inclusive               DOUBLE, \
    exclusive_percentage    DOUBLE, \
    exclusive               DOUBLE, \
    call                    DOUBLE, \
    subroutines             DOUBLE, \
    inclusive_per_call      DOUBLE, \
	FOREIGN KEY(function) REFERENCES function(id) ON DELETE NO ACTION, \
	FOREIGN KEY(metric) REFERENCES metric(id) ON DELETE NO ACTION \
)

CREATE INDEX exp_app_idx on experiment (application)
CREATE INDEX trial_exp_idx on trial (experiment)
CREATE INDEX fun_trial_idx on function (trial)
CREATE INDEX ilf_met_idx on interval_location_profile (function, metric)
CREATE INDEX its_fun_met_idx on interval_total_summary (function, metric)
CREATE INDEX ims_fun_met_idx on interval_mean_summary (function, metric)
CREATE INDEX ilp_f_m_n_c_t_idx on interval_location_profile (function, metric, node, context, thread)

CREATE VIEW function_detail1 \
	( id, trial, metric, inclusive_percentage, inclusive, \
	exclusive_percentage, exclusive, call, subroutines, \
	inclusive_per_call ) \
	AS SELECT \
	f.id, f.trial, ms.metric, ms.inclusive_percentage, ms.inclusive, \
	ms.exclusive_percentage, ms.exclusive, ms.call, ms.subroutines, \
	ms.inclusive_per_call \
	FROM function f inner join interval_mean_summary ms \
	ON f.id = ms.function
	
CREATE VIEW function_detail \
	( id, trial, metric, mean_inclusive_percentage, mean_inclusive,  \
	mean_exclusive_percentage, mean_exclusive, mean_call, mean_subroutines,  \
	mean_inclusive_per_call, total_inclusive_percentage, total_inclusive,  \
	total_exclusive_percentage, total_exclusive, total_call,  \
	total_subroutines, total_inclusive_per_call ) \
	AS SELECT \
	f.id, f.trial, f.metric, f.inclusive_percentage, f.inclusive,  \
	f.exclusive_percentage, f.exclusive, f.call, f.subroutines,  \
	f.inclusive_per_call, ts.inclusive_percentage, ts.inclusive,  \
	ts.exclusive_percentage, ts.exclusive, ts.call, ts.subroutines,  \
	ts.inclusive_per_call \
	FROM function_detail1 f inner join interval_total_summary ts  \
	ON f.id = ts.function and f.metric = ts.metric
	
CREATE VIEW function_interval \
	( trial, function, metric, node, context, thread, inclusive_percentage,  \
	inclusive, exclusive_percentage, exclusive, call,  \
	subroutines, inclusive_per_call ) \
	AS SELECT \
	f.trial, f.id, p.metric, p.node, p.context, p.thread, \
	p.inclusive_percentage, p.inclusive,  \
	p.exclusive_percentage, p.exclusive, p.call, p.subroutines,  \
	p.inclusive_per_call \
	FROM function f inner join interval_location_profile p \
	ON f.id = p.function
	
terminate
