老毕让我们演示内存数据库来着,找了个,SQLite。官方网站上,下到了源码,和安装文件(其实免安装,几百k)。既然是内存数据,那怎么体现出内存数据库的特征?
不涉及输入输出,所有工作都在内存中进行呢
下面是一组对比,开始是把SQLite当磁盘数据库用,把sqlite3.h 和.cpp添加到工程,然后下面程序,很简单,插入25万条数据,打开任务管理器,菜单项-》查看-》选择列。加入IO那些列,待会监测输入输出用
#include <iostream>
#include "sqlite3.h"
#include <string>
using namespace std;
int main(){
sqlite3 *db;
char *err_msg = 0;
char * sql_statement = "create table c(id ,name)";
int recode;
recode = sqlite3_open("D://test.db",&db);
if(recode!=SQLITE_OK)
{
cout<<"Can't Open Database: "<<sqlite3_errmsg(db)<<endl;
sqlite3_close(db);
}
else
{
cout<<"Open OK!"<<endl;
recode = sqlite3_exec(db,sql_statement,0,0,&err_msg);
if(recode!=SQLITE_OK)
{
cout<<"error : "<<err_msg<<endl;
sqlite3_close(db);
}
else
{
cout<<"create OK!/n";
//随便插入的数据,25万条
recode = sqlite3_exec(db,"begin trasaction",0,0,&err_msg);
for(int i=0;i<50000;i++)
{
recode = sqlite3_exec(db,"insert into c values(1,12) ",0,0,&err_msg);
recode = sqlite3_exec(db,"insert into c values(1,132) ",0,0,&err_msg);
recode = sqlite3_exec(db,"insert into c values(1,132) ",0,0,&err_msg);
recode = sqlite3_exec(db,"insert into c values(1,21) ",0,0,&err_msg);
recode = sqlite3_exec(db,"insert into c values(1,12) ",0,0,&err_msg);
}
recode = sqlite3_exec(db,"commit",0,0,&err_msg);
sqlite3_close(db);
}
}
system("pause");
return 0;
}
运行就可以监视到io不断增加,内存没有增加
想再次运行,到D盘里把test.db删除了
这是磁盘数据库
下面是内存数据库
在内存打开sqlite
#include <iostream>
#include "sqlite3.h"
#include <string>
using namespace std;
int
main(){
sqlite3 *db;
char *err_msg = 0;
char * sql_statement = "create table c(id ,name)";
int recode;
recode = sqlite3_open(":memory:",&db);
if(recode!=SQLITE_OK)
{
cout<<"Can`t Open Database: "<<sqlite3_errmsg(db)<<endl;
sqlite3_close(db);
}
else
{
cout<<"Open OK!/n";
recode = sqlite3_exec(db,sql_statement,0,0,&err_msg);
if(recode!=SQLITE_OK)
{
cout<<"error : "<<err_msg<<endl;
sqlite3_close(db);
}
else
{
cout<<"create OK!/n";
recode = sqlite3_exec(db,"begin trasaction",0,0,&err_msg);
for(int i=0;i<500000;i++)
{
recode = sqlite3_exec(db,"insert into c values(1,12) ",0,0,&err_msg);
recode = sqlite3_exec(db,"insert into c values(1,132) ",0,0,&err_msg);
recode = sqlite3_exec(db,"insert into c values(1,132) ",0,0,&err_msg);
recode = sqlite3_exec(db,"insert into c values(1,21) ",0,0,&err_msg);
recode = sqlite3_exec(db,"insert into c values(1,12) ",0,0,&err_msg);
}
recode = sqlite3_exec(db,"commit",0,0,&err_msg);
sqlite3_close(db);
}
}
return 0;
}
这是插入250万条数据。用任务管理器看到,内存在不断增加,没有任何IO
没有任何IO就体现出内存数据库的特征了吧
不知道对不对。
简单的程序体现内存数据特征