本文将分享nosql数据库STSDB的一般性使用的详细内容,此外,我们还将为大家带来关于iBoxDB性能堪比MongoDB的NOSql数据库、jdbc测试mysql数据库sql预解析(绑定变量)、mo
本文将分享nosql数据库STSDB的一般性使用的详细内容,此外,我们还将为大家带来关于iBoxDB性能堪比MongoDB的NOSql数据库、jdbc测试mysql数据库sql预解析(绑定变量)、mongodb非关系型数据库nosql与关系型数据库sql对应学习、mysql数据库innodb的索引特点的相关知识,希望对你有所帮助。
本文目录一览:- nosql数据库STSDB的一般性使用
- iBoxDB性能堪比MongoDB的NOSql数据库
- jdbc测试mysql数据库sql预解析(绑定变量)
- mongodb非关系型数据库nosql与关系型数据库sql对应学习
- mysql数据库innodb的索引特点
nosql数据库STSDB的一般性使用
update 2015-8-7
介绍另一个vk持久化数据库:
RaptorDB - the Document Store
http://www.codeproject.com/Articles/375413/RaptorDB-the-Document-Store
接着上一篇这里罗列下STSDB的一般性使用
以下内容基于stsdb4.dll(4.0.3.0版本)库(百度分享资源:http://pan.baidu.com/s/1jGxHE3k),截止本文发布,官方最新版本是4.0.5.0,官方地址:http://stsdb.com/
using System; using System.Collections.Generic; namespace STSDB { [Serializable] public class TStudent { public TStudent() { } public string Name { get; set; } public int Age { get; set; } public int GroupNumber { get; set; } public List<TCourse> CourseList { get; set; } } }
using System; namespace STSDB { [Serializable] public class TCourse { public string CourseName { get; set; } public string Teacher { get; set; } public int score { get; set; } } }演示代码:
/* * 1. STSdb 4.0 是一个开源的Nosql 数据库和虚拟文件系统,支持实时索引,完全用c#开发的。 * 引擎原理基于WaterfallTree(瀑布树)数据结构搭建 * * * 2.特性 * 支持几十亿级别的数据存取 * 支持TB级别文件大小 * 实时索引 * 内置压缩 * 内置序列化 * 支持稀疏分散的文件(byte[]) * 存储内存可控 * 支持多线程,且线程安全 * Storage engine instance is thread-safe. Creating (opening) XTable and XFile instances in one storage engine from different threads is thread-safe. XTable and XFile instances are also thread-safe. Manipulating different XTable/XFile instances from different threads is thread-safe. * * 3.缺点 * 不支持事务 * 同时处理所有打开的表 * * 支持多种情况下的数据引擎连接 IStorageEngine engine = STSdb.FromMemory(); //从内存中读取 IStorageEngine engine = STSdb.FromStream(stream); //从数据流中读取 IStorageEngine engine = STSdb.FromHeap(heap); //从堆栈中读取 IStorageEngine engine = STSdb.FromNetwork(host,port); //从远程地址读取 * * */ using System; using System.IO; using System.Linq; using System.Collections.Generic; namespace STSDB { using Newtonsoft.Json; using STSdb4.Data; using STSdb4.Database; using STSdb4.Storage; using STSdb4.WaterfallTree; using STSdb4.Remote.Heap; class Program { static void Main(string[] args) { ExecuteCode(WriteData); ExecuteCode(ReadData); //ExecuteCode(DatabaseSchemeInfo); //ExecuteCode(ReadItem); //ExecuteCode(DeleteItems); //ExecuteCode(ReadItem); //ExecuteCode(GetRecord); //ExecuteCode(PageRecord); //ExecuteCode(Others); //ExecuteCode(ReNaMetable); //ExecuteCode(ExistsTable); //ExecuteCode(DeleteTable); //ExecuteCode(ExistsTable); #region test //bool quit = false; //while (!quit) //{ // Console.Write("get item data: "); // string demo = Console.ReadLine(); // switch (demo) // { // case "Y": // break; // case "Q": // quit = true; // break; // default: // Console.WriteLine("Choose a Word between Y and Q(to quit)"); // break; // } //} #endregion Console.ReadKey(); } /// <summary>执行方法</summary> static void ExecuteCode(Action act) { System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch(); stopwatch.Start(); act(); stopwatch.Stop(); TimeSpan timespan = stopwatch.Elapsed; Console.WriteLine("运行{0}秒",timespan.TotalSeconds); } /// <summary> /// 数据库名 /// </summary> /// <remarks>文件名和扩展名不限制</remarks> protected static string DataBase = "ClassDB.db"; /// <summary> /// 学生表名 /// </summary> protected static string TableName = "tb_student"; /// <summary> /// 【新】学生表名 /// </summary> protected static string NewTableName = "new_tb_student"; /// <summary> /// XFile /// </summary> protected static string XFileName = "tb_file"; #region 基本操作 /// <summary> /// 创建库,写入数据 /// </summary> static void WriteData() { /* * ①:没有数据库会自动创建的,默认目录和应用程序目录一致; * ②:打开表,Key支持组合结构 => OpenXTable<TKey,TRecord> */ using (IStorageEngine engine = STSdb.FromFile(DataBase)) //① { var table = engine.OpenXTable<int,TStudent>(TableName); //② //var table2 = engine.OpenXTable<TKey,TTick>("table2"); //支持key嵌套 for (int i = 0; i < 1000; i++) { table[i] = new TStudent { Name = "Jon_" + i.ToString(),Age = new Random().Next(25,30),GroupNumber = i + (new Random().Next(310,399)),CourseList = new List<TCourse>() { new TCourse{ CourseName="C#高级编程"+i.ToString(),Teacher="老陈"+i.ToString(),score=80 },new TCourse{ CourseName="C#函数式程序设计"+i.ToString(),Teacher="老李"+i.ToString(),score=90 },new TCourse{ CourseName="多线程实战应用"+i.ToString(),Teacher="老张"+i.ToString(),score=95 },} }; } engine.Commit(); } } /// <summary> /// 读取数据 /// </summary> static void ReadData() { using (IStorageEngine engine = STSdb.FromFile(DataBase)) { var table = engine.OpenXTable<int,TStudent>(TableName); //ITable:IEnumerable对象 foreach (var item in table) Console.WriteLine(JsonConvert.SerializeObject(item,Newtonsoft.Json.Formatting.Indented)); Console.WriteLine(table.Count()); //TableName表中有100行数据 } } /// <summary> /// /// </summary> static void DatabaseSchemeInfo() { using (IStorageEngine engine = STSdb.FromFile(DataBase)) //① { IDescriptor descriptor = engine[TableName]; Console.WriteLine(descriptor.CreateTime.ToString("yyyy-MM-dd HH:mm:ss")); Console.WriteLine(descriptor.ModifiedTime.ToString("yyyy-MM-dd HH:mm:ss")); Console.WriteLine(descriptor.Name); //ID是表的唯一标识id,表一旦创建,它就创建了,后面只要表在就不会修改 //重建表它会从新分配 Console.WriteLine(descriptor.ID); //... } } /// <summary> /// 读取单条数据 /// </summary> static void ReadItem() { using (IStorageEngine engine = STSdb.FromFile(DataBase)) { var table = engine.OpenXTable<int,TStudent>(TableName); //ITable: IEnumerable对象 //var item = table.FirstOrDefault(x => x.Key <= 15 && x.Key >= 10); //key是5的记录 //table[10]; var item = table.FirstOrDefault(x => x.Key == 5); //key是5的记录 if (item.Value != null) Console.WriteLine(JsonConvert.SerializeObject(item,Newtonsoft.Json.Formatting.Indented)); else Console.WriteLine("key = 5 的记录不存在!"); //Console.WriteLine("10<= key <= 15 的记录不存在!"); } } static void AddItems() { using (IStorageEngine engine = STSdb.FromFile(DataBase)) { var table = engine.OpenXTable<int,TStudent>(TableName); //table[100] = new TStudent(){....}; table.InsertOrIgnore(2,new TStudent()); engine.Commit(); } } /// <summary> /// 删除表数据 /// </summary> static void DeleteItems() { using (IStorageEngine engine = STSdb.FromFile(DataBase)) { var table = engine.OpenXTable<int,TStudent>(TableName); //ITable:IEnumerable对象 if (table != null) { //table.Clear(); //清空表数据 table.Delete(5); //删掉key是5的记录 //table.Delete(10,15); //删掉key从10到15的记录 engine.Commit(); //提交操作,不能少 } } } /// <summary> /// 按需获取数据 /// </summary> static void GetRecord() { using (IStorageEngine engine = STSdb.FromFile(DataBase)) { /* Forward向前读取,Backward向后读取,它们都有2个重载,下面重点说明第二个重载 * Forward(TKey from,bool hasFrom,TKey to,bool hasTo); * Backward(TKey to,bool hasTo,TKey from,bool hasFrom); * 超出范围的都不会排除,另外,查询范围超出也不会有影响,但是要注意一点,formkey和endkey的大小关系 * * 0<----------[(S)]----------------[(E)]------------->N * */ var table = engine.OpenXTable<int,TStudent>(TableName); var fiterTB = table.Forward(2,true,9,true); //索引从2到9 //var fiterTB = table.Forward(2,false,true); //索引从0到9 //var fiterTB = table.Forward(2,false); //索引从0到表结尾 //var fiterTB = table.Forward(2,false); //索引从2到表结尾 //Backward刚好相反 foreach (var item in fiterTB) Console.WriteLine(JsonConvert.SerializeObject(item,Newtonsoft.Json.Formatting.Indented)); } } /// <summary> /// 数据分页 /// </summary> static void PageRecord() { using (IStorageEngine engine = STSdb.FromFile(DataBase)) { int pageIndex = 2; int pageSize = 10; var table = engine.OpenXTable<int,TStudent>(TableName); var fiterTB = table.Skip(pageSize * (pageIndex - 1)).Take(pageSize); foreach (var item in fiterTB) Console.WriteLine(JsonConvert.SerializeObject(item,Newtonsoft.Json.Formatting.Indented)); } } /// <summary> /// 文件数和记录数 /// </summary> static void Others() { using (IStorageEngine engine = STSdb.FromFile(DataBase)) { //表和虚拟文件的数量 Console.WriteLine("数据库 " + DataBase + " 中有 {0} 张表:{1}",engine.Count,TableName); //表记录数 var table = engine.OpenXTable<int,TStudent>(TableName); Console.WriteLine("表" + TableName + "中有" + table.Count() + "条记录"); } } /// <summary> /// 表是否存在 /// </summary> static void ExistsTable() { using (IStorageEngine engine = STSdb.FromFile(DataBase)) { //判断表存在与否 //bool exists = engine.Exists(NewTableName); //Console.WriteLine(NewTableName + " exist?=>{0}",exists.ToString()); bool exists = engine.Exists(TableName); Console.WriteLine(TableName + " exist?=>{0}",exists.ToString()); } } /// <summary> /// 重命名表名 /// </summary> static void ReNaMetable() { using (IStorageEngine engine = STSdb.FromFile(DataBase)) { //判断表存在与否 bool exists = engine.Exists(TableName); Console.WriteLine(TableName + " exist? =>{0}",exists.ToString()); //表重命名 engine.Rename(TableName,NewTableName); Console.WriteLine("表" + TableName + "被重命名为:" + NewTableName); if (engine.Exists(TableName)) Console.WriteLine("old table name \"" + TableName + "\" exist"); if (engine.Exists(NewTableName)) Console.WriteLine("new table name \"" + NewTableName + "\" exist"); } } /// <summary> /// 删除表 /// </summary> static void DeleteTable() { using (IStorageEngine engine = STSdb.FromFile(DataBase)) { //删除表 engine.Delete(TableName); //engine.Delete(NewTableName); engine.Commit(); } } #endregion #region XFile static void TestXFile() { using (IStorageEngine engine = STSdb.FromFile(DataBase)) { XFile file = engine.OpenXFile(XFileName); Random random = new Random(); byte[] buffer = new byte[] { 1,2,3,4,5,6,7,8,10 }; for (int i = 0; i < 100; i++) { long position = random.Next(); file.Seek(position,SeekOrigin.Begin); file.Write(buffer,buffer.Length); } engine.Commit(); } } //XFile uses special XTable<long,byte[]> implementation to provide effective sparse file functionality. //One storage engine can have many files #endregion #region Client/Server static void ClientUpdateData() { using (IStorageEngine engine = STSdb.FromNetwork("localhost",7182)) { ITable<int,string> table = engine.OpenXTable<int,string>("table"); for (int i = 0; i < 100000; i++) { table[i] = i.ToString(); } engine.Commit(); } } static void ServerHandleData() { string _dbname = "test.stsdb4"; using (IStorageEngine engine = STSdb.FromFile(_dbname)) { var server = STSdb.CreateServer(engine,7182); server.Start(); //server is ready for connections //server.Stop(); } } //The created server instance will listen on the specified port //and receive/send data from/to the clients #endregion #region Memory Usage /* min/max children (branches) in each internal (non-leaf) node max operations in the root node min/max operations in each internal node min/max records in each leaf node number of cached nodes in the memory */ static void MemoryUsageHandle() { using (StorageEngine engine = (StorageEngine)STSdb.FromFile(DataBase)) { //下面的demo都是STS.DB的默认值设置 //min/max children (branches) in each internal node engine.INTERNAL_NODE_MIN_BRANCHES = 2; engine.INTERNAL_NODE_MAX_BRANCHES = 4; //max operations in the root node engine.INTERNAL_NODE_MAX_OPERATIONS_IN_ROOT = 8 * 1024; //min/max operations in each internal node engine.INTERNAL_NODE_MIN_OPERATIONS = 64 * 1024; engine.INTERNAL_NODE_MAX_OPERATIONS = 128 * 1024; //min/max records in each leaf node engine.LEAF_NODE_MIN_RECORDS = 16 * 1024; engine.LEAF_NODE_MAX_RECORDS = 128 * 1024; //at least 2 x MIN_RECORDS //number of cached nodes in memory engine.CacheSize = 32; } } #endregion #region Heap /*using => STSdb4.WaterfallTree; STSdb4.Storage; STSdb4.Remote.Heap; */ static void HeaperEngine() { //Server端 IHeap heap = new Heap(new FileStream("Heap.db",FileMode.OpenorCreate)); HeapServer server = new HeapServer(heap,7183); //监听堆服务器 server.Start(); //开始监听 //从远程堆中创建 IStorageEngine 引擎,并处理数据 //using (IStorageEngine engine = STSdb.FromHeap(new RemoteHeap("host",7183))) //{ // ITable<int,string>("table"); // for (int i = 0; i < 100000; i++) // { // table[i] = i.ToString(); // } // engine.Commit(); //} } #endregion //... } }
iBoxDB性能堪比MongoDB的NOSql数据库
iBoxDB是一款高性能的NOSql数据库,其具备类SQL的一些特性,但的确是个NOSql数据库。高性能、无依赖、线程安 全、先天支持 Java 、.NET、Android、Mono、Unity3D、Xamarin、Nashorn、Linux、Windows Phone等平台、支持索引、事务、主键、零配置,内嵌或独立Server,用类SQL语局查询、支持Linq。官方有跟MongoDB的性能对比,基本 是高于MongoDB的。
官方网站:http://www.iboxdb.com/
下面我们就摘取官方网站的一些例子,方便大家进一步认识:
Examples
using(var box = db.Cube())
{
//select, insert, update, delete ...
var result = box.Commit();
}
常见对象的插入:
box.Bind("Member").Insert(
new Member() {
ID=box.NewId(Member.IncTableID, 1) ,
LoginName = "Andy",
Password = Member.EncodePassowrd("123"),
Tags = new string[]{ "Nice" , "Strong" }
}
);
Dynamic Object (document database)
game["GameType"] = "ACT";
box.Bind("Product").Insert(game);
game.put("GameType", "ACT");
box.bind("Table").insert(game);
Key Value Style Query
box.Bind("Table", 2L).Select<AClass>();
//Composite Key
box.Bind("Table2", 99, "ABC").Select<BClass>();
box.bind("Table", ID).select(Member.class);
//Composite Key
box.bind("Table2",8, "MyID").select(Product.class);
//from TABLE where A>? & B<=? order by C limit 0,10
box.Select<Member>("from Member where LoginName==?", "MyName");
//from [table] where [condition]
// order by [field1] desc,[field2] limit [0,10]
//[Condition:] == != < <= > >= & | ( )
//[IFunction:] =[F1,F2,F3]
box.select(Member.class, "from Member where Name==?", "MyName");
box.Select<Member>("from Member where [Tags]", new QueryArray("Value"));
可以配合linq来使用
from o in box.Select<Class>("from Class")
where o.Text.Contains(text)
select o;
.NET | |
---|---|
|
在32bit系统上 PK MongoDB:
Results:
threadCount=100000 , batchCount=10
MongoDB(Default)
Database Transaction Test: None
Insert:1000000 AVG:10521 objects/s
iBoxDB(File Mode)
Database Transaction Test: Succeeded
Insert:1000000 AVG:17981 objects/s
Update:1000000 AVG:12397 objects/s
Delete:1000000 AVG:22869 objects/s
iBoxDB(MemoryMappedFile Mode)
Database Transaction Test: Succeeded
Insert:1000000 AVG:38458 objects/s
Update:1000000 AVG:20200 objects/s
Delete:1000000 AVG:33342 objects/s
iBoxDB(InMemory Mode)
Database Transaction Test: Succeeded
Insert:1000000 AVG:39132 objects/s
Update:1000000 AVG:20226 objects/s
Delete:1000000 AVG:33658 objects/s
iBoxDB.NET 在64bit System
iBoxDB v1.5 mongodb-win32-x86_64-2008plus-2.4.5 MongoDB.Driver v1.8.2.34
MongoDB(Default)
Database Transaction Test: None
Insert:1,000,000 AVG: 32,702 objects/s
iBoxDB(File Mode)
Database Transaction Test: Succeeded
Insert:1,000,000 AVG:33,368 objects/s
Update:1,000,000 AVG:16,939 objects/s
Delete:1,000,000 AVG:22,504 objects/s
iBoxDB(MemoryMappedFile Mode)
Database Transaction Test: Succeeded
Insert:1,000,000 AVG:42,236 objects/s
Update:1,000,000 AVG:17,765 objects/s
Delete:1,000,000 AVG:23,147 objects/s
iBoxDB(InMemory Mode)
Database Transaction Test: Succeeded
Insert:1,000,000 AVG:47,382 objects/s
Update:1,000,000 AVG:26,806 objects/s
Delete:1,000,000 AVG:35,092 objects/s
实现主从同步:
好了,先整理到这儿吧,感兴趣的朋友,可以去参照官网,动手试试。
jdbc测试mysql数据库sql预解析(绑定变量)
jdbc测试
jdbc测试mysql数据库sql预解析(绑定变量)
用习惯了oracle,学习mysql,想测试一下mysql绑定变量的效果。以前看网上介绍大部份都说mysql没有sql共享池的概念,所以也不存在sql预解析或绑定变量的说法。
今天测试了一下(通过网络抓包、查看服务器端sql日志及分析源码等方法),发现mysql还是有sql预解析的实现。
服务器端是mysql 5.1.58(win32),用jdbc(5.1.18)做客户端,默认的连接方式是不会有sql预解析效果,即使我们用PreparedStatement对象也差不多,它只是把SQL和变量拼接成一个完整的SQL发送给服务器,如下代码:
PreparedStatement pstmt = conn.prepareStatement("select * from t1 where c1=?"); pstmt.setString(1, "abc"); pstmt.execute();
实际上不会有预解析的过程,而是经过简单的拼接,把如下SQL发送给服务器
select * from t1 where c1=''abc''
mysql服务器的sql语句缓存可以通过状态变量Prepared_stmt_count查看
mysql> show status like ''Prepared_stmt_count''; +---------------------+-------+ | Variable_name | Value | +---------------------+-------+ | Prepared_stmt_count | 1 | +---------------------+-------+ 1 row in set
不过mysql的sql语句缓存与oracle有很大不同,它是会话语句级的,不是全局共享,当会话断开或PreparedStatement.close后这个缓存就没有了。我们需要设置Connection的参数cachePrepStmts=true把PreparedStatement缓存起来,prepStmtCacheSize=xxx来设置每个会话缓存语句的最大数量(很多连接池也有类似的功能)。
OK,已经知道如何启用预解析了,想看看启用与不启用预解析性能有多少差别,会不会也像oracle那么明显呢?经过简单的测试,发现当没有PreparedStatement缓存(cachePrepStmts=false)时,打开预解析性能下降很多, 当有PreparedStatement缓存(cachePrepStmts=true)时,两者性能基本一样。这个结果让人很失望,个人分析有几个原因:
启用预解析但没有PreparedStatement缓存时,每次创建PreparedStatement都需要解析一次,execute时又需要交互一次,而预解析的SQL在PreparedStatement.close又不能重用,所以性能反而更差。
当有PreparedStatement缓存时,预解析的SQL文本缓存在服务器端,但是并不会像oracle一样缓存执行计划,所以每次execute时都需要解析SQL和生成执行计划,因此只是减少了每次execute传输SQL的文本大小,性能差别不大。
注:如果SQL语法错误,那么服务器端预解析会出错,但jdbc收到预解析出错的信息后并不提示出错,而是将取消本条语句预解析的状态,execute时直接把SQL接装发送给服务器,mysql jdbc在PreparedStatement构造函数中代码如下,其中返回ServerPreparedStatement类表示使用了绑定变量,返回PreparedStatement表示未使用绑定变量:
try { pStmt = ServerPreparedStatement.getInstance(getLoadBalanceSafeProxy(), nativeSql, this.database, resultSetType, resultSetConcurrency); pStmt.setResultSetType(resultSetType); pStmt.setResultSetConcurrency(resultSetConcurrency); } catch (SQLException sqlEx) { // Punt, if necessary if (getEmulateUnsupportedPstmts()) { pStmt = (PreparedStatement) clientPrepareStatement(nativeSql, resultSetType, resultSetConcurrency, false); } else { throw sqlEx; } }
2012-02-17
mongodb非关系型数据库nosql与关系型数据库sql对应学习
https://docs.mongodb.org/manual/reference/sql-comparison/
mysql数据库innodb的索引特点
innodb存储引擎使用B+树实现索引的
B+树不同于B树的规则:
1、数据或者数据的指针只存放在叶子节点,非叶子节点只存储关键字
2、叶子节点中关键字由小到大排列,左叶子末尾数据会保存右叶子开始数据的指针;
所以,B+树的优点:
1、由于非叶子节点只存储关键字,可以存放的关键字大大增加,因此树的层级更少
2、查询任何一个数据都需要从叶子节点获取数据的地址,因此每次数据的查询次数完全一致,因此查询的速度更稳定
3、叶子节点天然就是有序链表
4、全表遍历时,只需要遍历叶子节点链表即可,不需要像B树一样需要遍历每一层,性能更好
当然B树也有B+树所不具备的有点:
如果要查询的某条数据离树的根节点更近,这时查询速度比较B+树更快
关于nosql数据库STSDB的一般性使用的问题我们已经讲解完毕,感谢您的阅读,如果还想了解更多关于iBoxDB性能堪比MongoDB的NOSql数据库、jdbc测试mysql数据库sql预解析(绑定变量)、mongodb非关系型数据库nosql与关系型数据库sql对应学习、mysql数据库innodb的索引特点等相关内容,可以在本站寻找。
本文标签: