与SQLite相比,为什么Realm这么快? [英] Why is Realm so fast compared to SQLite?

查看:366
本文介绍了与SQLite相比,为什么Realm这么快?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在 Realm网站上看到了基准测试,与SQLite相比,Realm如此之快,具有悠久的发展历史并于2000年发布?

I have seen the benchmarks on the Realm website, how come Realm is so fast compared to SQLite, which has a long development history, being released back in 2000?

我想知道有知识的人是否可以分享Realm在数据库层内部使用的一些通用技术?截至2016年3月,只有绑定是开源的,并且db层仍处于关闭状态.

I was wondering if someone with knowledge could share some general techniques that Realm uses underneath the hood in the database layer? As of March, 2016, only the bindings are open-sourced and the db layer is still closed off.

推荐答案

领域博客进行了很好的技术讨论关于这个主题,但是没有一个关于其性能背后原因的深入的存储库.这篇文章旨在以易于访问的方式压缩视频和文章.

The Realm blog has great technical talks on the subject, but there was no single repository that goes into depth about the reasons behind its performance. This post is an effort to condense the videos and articles in an easily accessible manner.

Realm是从一开始就为移动设备设计的,因此可以在移动平台上进行权衡取舍,而SQLite是已移植到移动设备的通用解决方案.

Realm is designed for mobile from the get-go that uses and therefore makes trade-offs that make sense in the mobile platform whereas SQLite is a generic solution that was ported to mobile.

当然,Realm会进行很多小的优化(整数包装,将常见的字符串转换为枚举),但是在这篇文章中,我将尝试着重于那些带来最大性能优势的差异.

There are of course a lot of small optimizations that Realm does (integer packing, converting common strings to enums), but in this post I'll try to focus on the differences that account for the most performance benefits.

  1. 传统的 SQLite + ORM抽象是泄漏的,因为ORM只是将对象及其方法转换为SQL语句.结果,这是以性能为代价来权衡开发人员生产力的.另一方面,领域是对象数据库,这意味着您的对象直接反映了您的数据库.对数据库的这种直接访问导致了下一个主题:零复制.
  2. 零复制:从数据库读取数据的传统方式会导致不必要的复制(原始数据->反序列化表示->语言级对象). Realm通过使用B +树在内存中映射整个数据来避免这种情况,只要查询数据,Realm只需计算偏移量,从内存映射区域中读取并返回原始值.
  3. 延迟加载:由于属性以列而不是行表示,因此可以根据需要延迟加载属性,并且由于列结构的原因,读取速度更快,而插入速度则较慢.但这是在移动应用程序上下文中进行的一个很好的权衡.
  4. MVCC(多版本并发控制):Realm还可以使用MVCC模型很好地处理并发,这意味着可以同时完成多个读取事务,而在进行写入事务时也可以读取数据正在提交.
  1. The traditional SQLite + ORM abstraction is leaky because ORM simply converts Objects and their methods into SQL statements. As a result, it is a trade-off for developer productivity at the cost of performance. Realm, on the other hand, is an object database, meaning your objects directly reflect your database. This direct access to the database, leads to the next topic: zero-copy.
  2. Zero-copy: the traditional way of reading data from a database leads to unnecessary copying (raw data -> deserialized representation -> language-level objects). Realm avoids this by mapping the whole data in-memory, using B+ trees and whenever data is queried, Realm simply calculates the offset, reads from the memory mapped region and returns the raw value.
  3. Lazy loading: because the properties are represented in columns instead of rows, it can lazy load the properties as necessary and because of the column structure, reads are much faster, while inserts are slower. But that is a good trade-off to make in the context of a mobile application.
  4. MVCC (Multiversion Concurrency Control): Realm also handles concurrency well using the MVCC model, which means that multiple read transactions can be done at the same time and reads can also be done while a write transaction is being committed.

这篇关于与SQLite相比,为什么Realm这么快?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆