LoginSignup
4
4

More than 5 years have passed since last update.

Realm-Swift Int型の最適化について

Posted at

背景

  • Realmには、Int型のそれぞれのbit数に対応している記述がある
  • では、大きな整数が必要ない場合にはInt8を利用するなどすれば、DBのサイズ節約に繋がる?

結論

  • Realmでは、Int型のbit数で確保領域は変わらないので、一律Int指定で良さそう

比較

下記コードでdefault.realmのサイズを比較(30000000件の1カラムレコード)

import UIKit
import RealmSwift

final class Dog: Object {
//    dynamic var age: Int64 = 0 // 393KB
//        dynamic var age: Int8 = 0 // 393KB
//    dynamic var age = false // 393KB
//    dynamic var age = "" // 393KB
}

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        do {
            let realm = try Realm()
            realm.write({
                for _ in 0..<30000000 {
                    let dog = Dog()
                    realm.add(dog)
                }
            })
        } catch {
            print("error")
        }
        print("completed")
    }
}

結果

DBサイズ
Int64 393KB
Int8 393KB
Bool 393KB
String 393KB

全部393KB。

4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4