mysql 列式数据库_列式数据库的简单分析

#include

#include

#include

#include

#include

#include

#include

#include

#define MAXINT RAND_MAX

#define MAXROWS 1000000

#define MINVAL 1

#define MAXVAL 150000000

using namespace std;

/*计时器*/

class Timer {

public :

//构造函数

Timer ();

//析构函数

~Timer ();

//开始计时

void begin();

//计时结束

void end();

//获取时间,ms

double get_time();

private :

clock_t start, finish;

double time;

};

Timer::Timer () {

start = 0;

finish = 0;

}

Timer::~Timer () {

start = 0;

finish = 0;

}

void Timer::begin () {

start = clock();

}

void Timer::end () {

finish = clock();

}

double Timer::get_time() {

time = (double)(finish-start)/CLOCKS_PER_SEC*1000;

return time;

}

//计时器

Timer timer;

/*记录各种结构表的空间占用*/

struct Size {

struct {

struct {

int _static;

int _dynamic;

} col,row;

} mem,file;

} size;

/*记录各种结构表的文件指针*/

struct File {

struct {

struct {

fstream _static;

fstream _dynamic;

} table,index;

} col,row;

} file;

/*静态行式表结构*/

struct StaticRowTable {

int id;

char name[255];

int num;

double score;

bool flag;

} * static_row_table;

/*静态行式表索引*/

struct StaticRowTableIndex {

multimap id;

multimap name;

multimap num;

multimap score;

multimap flag;

} static_row_table_index;

/*静态列式表结构*/

struct StaticColTable {

int* id;

char (*name)[255];

int* num;

double* score;

bool* flag;

} static_col_table;

/*动态行式表结构*/

struct DynamicRowTable {

int id;

int char_len;

char *name;

int num;

double score;

bool flag;

} * dynamic_row_table;

/*动态行式表索引*/

struct DynamicRowTableIndex {

multimap id;

multimap name;

multimap num;

multimap score;

multimap flag;

} dynamic_row_table_index;

/*动态列式表结构*/

struct DynamicColTable {

int* id;

int* char_len;

char** name;

int* num;

double* score;

bool* flag;

} * dynamic_col_table;

/*随机字符*/

char randChar() {

return rand()%26+'A';

}

/*随机字符串*/

void randString(char col[], int len) {

for(int i=0; i

col[i] = randChar();

}

}

/*初始化表数据*/

void init_StaticTable() {

double time;

cout << "+-----静态数据-----+" << endl;

//分配空间

cout << "分配空间中......" << endl;

timer.begin();

static_row_table = new StaticRowTable[MAXROWS];

static_col_table.id = new int[MAXROWS];

static_col_table.name = new char[MAXROWS][255];

static_col_table.num = new int[MAXROWS];

static_col_table.score = new double[MAXROWS];

static_col_table.flag = new bool[MAXROWS];

timer.end();

time = timer.get_time();

cout << "空间分配完毕!" << endl

<< "分配空间耗时: "

<< time << "ms" << endl;

//产生随机数据和索引

cout << "生成数据中......" << endl;

timer.begin();

for(int i=0; i

static_col_table.id[i] =

static_row_table[i].id = i;

static_row_table_index.id.insert(pair(static_row_table[i].id,i));

randString(static_row_table[i].name, rand()%20+1);

strcpy(static_col_table.name[i],static_row_table[i].name);

static_row_table_index.name.insert(pair(static_col_table.name[i],i));

static_col_table.num[i] =

static_row_table[i].num = rand();

static_row_table_index.num.insert(pair(static_row_table[i].num,i));

static_col_table.score[i] =

static_row_table[i].score = rand()/rand();

static_row_table_index.score.insert(pair(static_row_table[i].score,i));

static_col_table.flag[i] =

static_row_table[i].flag = rand()%2;

static_row_table_index.flag.insert(pair(static_row_table[i].flag,i));

}

timer.end();

time = timer.get_time();

cout << "数据生成完毕!" << endl;

cout << "生成数据耗时: "

<< time << "ms" << endl;

//初始化文件指针

timer.begin();

file.row.table._static.open("row_table_static.dat", ios::binary | ios::out);

file.row.index._static.open("row_index_static.dat", ios::binary | ios::out);

file.col.table._static.open("col_table_static.dat", ios::binary | ios::out);

if( !file.row.table._static ||

!file.row.index._static ||

!file.col.table._static) {

cout << "打开文件失败" << endl;

}

cout << "正在将数据写入文件......" << endl;

for(int i=0; i

file.row.table._static.write(reinterpret_cast(&static_row_table[i]),

sizeof(StaticRowTable));

}

file.row.table._static.close();

for(int i=0; i

file.row.index._static.write(reinterpret_cast(&static_row_table_index),

sizeof(StaticRowTableIndex));

}

file.row.index._static.close();

for(int i=0; i

file.col.table._static.write(reinterpret_cast(&static_col_table.id[i]),

sizeof(int));

}

for(int i=0; i

file.col.table._static.write(reinterpret_cast(static_col_table.name[i]),

sizeof(char[255]));

}

for(int i=0; i

file.col.table._static.write(reinterpret_cast(&static_col_table.num[i]),

sizeof(int));

}

for(int i=0; i

file.col.table._static.write(reinterpret_cast(&static_col_table.score[i]),

sizeof(double));

}

for(int i=0; i

file.col.table._static.write(reinterpret_cast(&static_col_table.flag[i]),

sizeof(bool));

}

file.col.table._static.close();

timer.end();

time = timer.get_time();

cout << "数据写入完毕!" << endl;

cout << "写入数据耗时: "

<< time << "ms" << endl;

//计算总占用空间

size.mem.row._static = sizeof(*static_row_table)*MAXROWS

+sizeof(static_row_table_index)*MAXROWS;

size.mem.col._static = (sizeof(int)*2+sizeof(double)

+sizeof(bool)

+sizeof(char)*255)*MAXROWS;

cout << "静态行式存储耗费空间: "

<< size.mem.row._static/1024/1024 << "M" << endl;

cout << "静态列式存储耗费空间: "

<< size.mem.col._static/1024/1024 << "M" << endl;

}

void init_DynamicTable() {

double time;

cout << "+-----动态数据-----+" << endl;

}

void init() {

double time1, time2;

srand(time(0));

cout << "======生成数据======" << endl;

init_StaticTable();

init_DynamicTable();

}

/*

SELECT name

FROM table

WHERE num BETWEEN MINVAL AND MAXVAL;

*/

/*测试内存中静态行存储*/

int Mem_Static_testRow() {

double time;

int count = 0;

int id;

multimap::iterator it,itlow,itup;

cout << "正在测试内存中读取行式静态表......" << endl;

timer.begin();

itlow = static_row_table_index.num.lower_bound (MINVAL);

itup = static_row_table_index.num.upper_bound (MAXVAL);

for (it=itlow; it!=itup; ++it) {

id = (*it).second;

StaticRowTable row = static_row_table[id];

//结果

//cout << row.id;

/*cout << '\t' << */row.name;

//cout << '\t' << row.num;

//cout << endl;

//计数

++count;

}

timer.end();

time = timer.get_time();

cout << "内存中行式静态表读取测试完毕!" << endl;

cout << "读取耗时:" << time << " ms" << endl;

return count;

}

/*测试磁盘中静态行存储*/

int File_Static_testRow() {

double time;

int count = 0;

int id;

char *name;

int num;

int pos;

StaticRowTable row;

multimap::iterator it,itlow,itup;

//初始化文件指针

cout << "正在测试磁盘中读取行式静态表......" << endl;

timer.begin();

file.row.table._static.open("row_table_static.dat", ios::binary | ios::in);

//file.row.index._static.open("row_index_static.dat", ios::binary | ios::in);

if(!file.row.table._static) {

cout << "打开文件失败" << endl;

}

//假设索引在内存中

itlow = static_row_table_index.num.lower_bound (MINVAL);

itup = static_row_table_index.num.upper_bound (MAXVAL);

for (it=itlow; it!=itup; ++it) {

id = (*it).second;

pos = sizeof(StaticRowTable)*id;

file.row.table._static.seekg(pos);

file.row.table._static.read(reinterpret_cast(&row),

sizeof(StaticRowTable));

//结果

//cout << row.id;

/*cout << '\t' << */row.name;

//cout << '\t' << row.num;

//cout << endl;

//计数

++count;

}

file.row.table._static.close();

//file.row.index._static.close();

timer.end();

time = timer.get_time();

cout << "磁盘中行式静态表读取测试完毕!" << endl;

cout << "读取耗时:" << time << " ms" << endl;

return count;

}

/*测试磁盘中静态列存储*/

int Mem_Static_testCol() {

double time;

int count = 0;

int id;

int num;

char *name;

cout << "正在测试内存中列式静态表读取......" << endl;

timer.begin();

for(int i=0; i

int num = static_col_table.num[i];

if(num>MINVAL and num

//结果

//cout << i;

/*cout << '\t' << */static_col_table.name[i];

//cout << '\t' << static_col_table.num[i];

//cout << endl;

//计数

++count;

}

}

timer.end();

time = timer.get_time();

cout << "内存中列式静态存储表读取测试完毕!" << endl;

cout << "读取耗时:" << time << " ms" << endl;

return count;

}

/*测试磁盘中静态列存储*/

int File_Static_testCol() {

double time;

int count = 0;

int id;

int num;

char *name = new char[255];

int pos_num;

int pos_name;

int pos;

cout << "正在测试磁盘中列式静态表读取......" << endl;

timer.begin();

file.col.table._static.open("col_table_static.dat", ios::binary | ios::in);

fstream tmpfile("col_table_static.dat", ios::binary | ios::in);

if(!file.col.table._static || !tmpfile) {

cout << "打开文件失败" << endl;

}

pos_name = sizeof(int)*MAXROWS;

pos_num = (sizeof(int)

+sizeof(char[255]))*MAXROWS;

file.col.table._static.seekg(pos_num);

for(int i=0; i

file.col.table._static.read(reinterpret_cast(&num),

sizeof(int));

if(num>MINVAL and num

//结果

id = i;

//cout << id;

pos = pos_name+sizeof(char[255])*id;

tmpfile.seekg(pos);

tmpfile.read(reinterpret_cast(name),

sizeof(char[255]));

/*cout << '\t' << */name;

//cout << '\t' << num;

//cout << endl;

//计数

++count;

}

}

file.col.table._static.close();

timer.end();

time = timer.get_time();

cout << "磁盘中列式静态存储表读取测试完毕!" << endl;

cout << "读取耗时:" << time << " ms" << endl;

return count;

}

void test() {

int count1, count2, count3, count4;

cout << "=====内存存取测试=====" << endl;

cout << "+----静态表测试中----+" << endl;

cout << "*行式存储*" << endl;

//内存中静态行式存储表

count1 = Mem_Static_testRow();

//内存中静态列式存储表

count2 = Mem_Static_testCol();

cout << "*列式存储*" << endl;

//磁盘中静态行式存储表

count3 = File_Static_testRow();

//磁盘中静态行式存储表

count4 = File_Static_testCol();

if (count1==count2 and count2==count3 and count3==count4) {

cout << "共匹配:" << count1 << " 行" << endl;

} else {

cout << "错误:每次匹配行数不同" << endl;

}

}

int main() {

init();

test();

cout << "All OK!" << endl;

return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值