Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

数据库表中json字段使用的TEXT类型存储,是否尝试过存BLOB类型的NSData #24

Open
yehot opened this issue Mar 23, 2016 · 4 comments

Comments

@yehot
Copy link

yehot commented Mar 23, 2016

使用过程中,发现单张table的大小有些夸张:
1、存储1万条数据,使用NSJSONSerialization转TEXT类型存储后的大小是4.4M;
2、改变表里存的json string数据类型为data后,改用NSKeyedArchiver将jsonObject转NSData,大小为61k;

请问是否有做过此类的测试?

测试代码如下:
YTKKeyValueStore *store = [[YTKKeyValueStore alloc] initDBWithName:@"test.db"];
NSString *tableName = @"zm_table";
[store createTableWithName:tableName];

// 测试存储 1万条数据的大小
NSString *key = @"1";
NSDictionary *user = @{@"id": @1,
                       @"name": @"tangqiao",
                       @"age": @30,
                       @"news": @"测试存储 1万条数据的大小测试存储 1万条数据的大小测试存储 1万条数据的大小测试存储 1万条数据的大小测试存储 1万条数据的大小",
                       @"detail":@"测试存储 1万条数据的大小测试存储 1万条数据的大小测试存储 1万条数据的大小测试存储 1万条数据的大小测试存储 1万条数据的大小测试存储 1万条数据的大小"};
NSMutableArray *array = [NSMutableArray array];
for (int i = 0; i < 10000; i++) {
    [array addObject:user];
}
NSDictionary *dict = @{@"news": array};

[store putObject:dict withId:key intoTable:tableName];

测试形式:
NSData * data = [NSJSONSerialization dataWithJSONObject:object options:0 error:&error];
NSData *data1 = [NSKeyedArchiver archivedDataWithRootObject:object];

NSLog(@"JSONSerialization Data Size = %@",
      [NSByteCountFormatter stringFromByteCount:data.length
                                     countStyle:NSByteCountFormatterCountStyleFile]);
NSLog(@"KeyedArchiver Data Size = %@",
      [NSByteCountFormatter stringFromByteCount:data.length
                                     countStyle:NSByteCountFormatterCountStyleFile]);

测试结果:
2016-03-23 10:40:14.154 YTKKeyValueStore[68148:2019750] JSONSerialization Data Size = 4.4 MB
2016-03-23 10:40:14.154 YTKKeyValueStore[68148:2019750] KeyedArchiver Data Size = 61 KB

谢谢!

@maxfong
Copy link

maxfong commented Jun 27, 2016

@yehot 存储形式不同,还好我的MFSCache用的NSKeyedArchiver 😂😂😂

@tangqiaoboy
Copy link
Member

苹果的 NSJSONSerialization 确实效率很低下。这是一个问题。。。

2016-06-27 16:36 GMT+08:00 Witty Young notifications@github.com:

@yehot https://github.com/yehot 存储形式不同,还好我的MFSCache
https://github.com/maxfong/MFSCache用的NSKeyedArchiver 😂😂😂


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
#24 (comment),
or mute the thread
https://github.com/notifications/unsubscribe/AAsvqdKScgXVcz73u3cFY_TlrzMzLnQvks5qP4uKgaJpZM4H2uyz
.

@lanjing99
Copy link

@tangqiaoboy 结论是啥?不改成NSKeyedArchiver的原因是不是兼容已有的代码?如果我要对数据进行版本迁移,目前还不支持。例如数据从未加密加密,甚至是包括存储从NSJSONSerialization改成NSKeyedArchiver。只能简单粗暴地删除所有的缓存key value内容?

@tangqiaoboy
Copy link
Member

对,主要考虑的是兼容性问题,我不能简单就改数据结构,不然那些用了这个库的同学会被坑的,他们可能会发现简单升了一下级,以前的数据都无法读取了。但是要支持升级功能会比较麻烦,即使是号称升级方便的 realm,也需要程序员做额外的工作。

如果你没有兼容性的顾虑的话,我个人建议你直接 fork 一个分支把这个改掉。
对于其他使用的同学,我估计遇到性能问题的情形也很少,因为这个库本身就是为简单的存储需求设计的。

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants