Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Indexed Properties in Realm

I noticed that Realm support indexed properties. But not all database systems support this (or it is done by default without an explicit declaration).

Please explain why it is needed, and what is unlike using of primary keys. And then, and another allows to accelerate queries. But then why not just use only primary keys?

It is clear that there is possible to index multiple properties, but why do it? That is, I do not represent real problems, where no use indexing can not do (or perhaps, but it would be disgusting in terms of programming). Give a couple of examples of tasks that simply explain the advantages of indexing: how it is occurs without indexation, and with indexation. To the result was obvious, "on the face".

I use Swift.

Sorry for my English ٩(◕‿◕)۶

like image 858
Vitaly Vesyolko Avatar asked Oct 27 '25 13:10

Vitaly Vesyolko


2 Answers

Most other database systems need an explicit declaration, the same as Realm, I think.

(e.g. MySQL CREATE TABLE test(id int, no int, INDEX(id,no)); or CREATE INDEX id_index ON test(id); ALTER TABLE test ADD INDEX id_index(id);, SQLite CREATE INDEX nameindex ON user(name);)

Because indexes are a trade-off between space and time, you're sacrificing some extra disk space and a bit of CPU overhead on each INSERT, UPDATE, and DELETE query to make most (if not all) your queries much faster.

In most database systems, creating indexes needs an explicit declaration. Choosing which columns to create indexes on is the responsibility of the programmer.

Realm is also the same. Indexing a property will greatly speed up queries where the property is compared for equality (i.e. the = and IN operators), at the cost of slower insertions.

https://realm.io/docs/swift/latest/#indexed-properties

In most of the mobile application, because searches are performed more than updates, indexes work well in most cases.

like image 64
kishikawa katsumi Avatar answered Oct 30 '25 07:10

kishikawa katsumi


Please explain why it is needed, and what is unlike using of primary keys. >And then, and another allows to accelerate queries. But then why not just use only primary keys?

A primary key is an index that is unique and cannot be null. Use indexes on properties that will be searchable. It will speedup the queries.

like image 45
clauziere Avatar answered Oct 30 '25 07:10

clauziere