SwiftDataで検索効率向上のためにインデックス使う方法を調べる
#swift #2023-12-18 #SwiftData
結論
現状SwiftDataではユニーク制約は使えそうだが、インデックス自体は使えそうにない。
理由
CoreDataの NSFetchIndexに関連するものがインデックスに関するもののようであるが、該当するものがSwiftDataに見当たらない。
調査メモ
Core Dataのドキュメント
https://developer.apple.com/documentation/coredata
Core Data Stackよりschemaを管理するのはNSManagedObjectModelあたりのように仮説をたてる
https://developer.apple.com/documentation/coredata/core_data_stack#3235481
MSManagedObjectModel
https://developer.apple.com/documentation/coredata/nsmanagedobjectmodel
https://developer.apple.com/documentation/coredata/nsmanagedobjectmodel#1654134
NSEntityDescriptionというクラスがテーブル定義と対応を持ちそうであるという仮説をたてる
NSEntityDescription
https://developer.apple.com/documentation/coredata/nsentitydescription
Configuring indexes and constraintsという節があり、制約とindexsの情報をもっていそうである
https://developer.apple.com/documentation/coredata/nsentitydescription#1651419
NSFetchIndexDescriptionがIndexの設定ではないかという仮説をたてる
NSFetchIndexDescription
https://developer.apple.com/documentation/coredata/nsfetchindexdescription
nameはおそらくindexの名前で、さらなる詳細がNSFetchIndexElementDescriptionで表現されそうという仮説を立てる
NSFetchIndexElementDescription
https://developer.apple.com/documentation/coredata/nsfetchindexelementdescription
NSFetchIndexElementTypeでindexの種類を指定できそう
NSPropertyDescriptionはおそらく列を表現するクラスだろう
NSFetchIndexElementType
https://developer.apple.com/documentation/coredata/nsfetchindexelementtype
binary indexとR-Tree indexを指定できそうである
NSPropertyDescription
https://developer.apple.com/documentation/coredata/nspropertydescription
ここまでの結論
NSFetchIndex で始まるクラスがインデックスに関するものだと考えられる。
SwiftDataにポーティングされているか確認すればよさそう。
SwiftDataのドキュメント
https://developer.apple.com/documentation/swiftdata
fetchで検索すると以下の2点が見つかる
FetchDescriptor
FetchResultsCollection
FetchDescrptor
https://developer.apple.com/documentation/swiftdata/fetchdescriptor
predicatorとsortbayをもつあたりから SQLのWHERE句とORDER BY 句に該当するものを表現するものに思われる
FetchResultsCollection
https://developer.apple.com/documentation/swiftdata/fetchresultscollection
SQLの結果セットを返す構造体だと思われる
ここにindexという名前はでてくるが、これは何番目の要素かを示すものだと考えられる。
indexで検索してもクラスや構造体にindexを含むものは存在しない
つまり、該当するものがなさそうである。
別の調査
スキーマのメタデータを記録するような仕組みを探す
データの追記や編集のためのドキュメントがある
https://developer.apple.com/documentation/swiftdata/adding-and-editing-persistent-data-in-your-app
@Attribute(.unique) var name: String という記述がありunique 制約をかける設定のように見える
@Attribute マクロ
https://developer.apple.com/documentation/swiftdata/attribute(_:originalname:hashmodifier:)
Schema.Attribute.Option が設定できる項目のようだ。
Schema.Attribute.Option
https://developer.apple.com/documentation/swiftdata/schema/attribute/option
indexに該当しそうなものはない。
Scheme関連を確認していく
Scheme.Entityがテーブル定義に該当しそうである
https://developer.apple.com/documentation/swiftdata/schema/entity
Scheme.Proptertyが列に該当するものだと仮説を立てられそう
Scheme.Attributeがメタデータに該当するものだと仮説を立てらそう
uniquenessContstrainsとユニーク制約のためのものがある(しかし説明は一切ない)
SchemaProperty
https://developer.apple.com/documentation/swiftdata/schemaproperty
ユニークかどうか確認ができる程度
Schema.Attribute
https://developer.apple.com/documentation/swiftdata/schema/attribute
@Attributeマクロを使って作れる実態と仮説が建てられそう