Home » Android » android – Proper way to add index columns in greenDao?

android – Proper way to add index columns in greenDao?

Posted by: admin June 15, 2020 Leave a comment

Questions:

I am building a data model in greenDAO. It is a port of an iOS app that uses Core Data. In iOS we use indexes (indices?) to increase lookup performance in a table of 20 columns (properties) where 5 columns are frequently queried. I know this results in extra storage and provides slower writes into the table.

Digging in the documentation, I came across the addIndex(Index index) method in Entity and the index() method in Property.PropertyBuilder. Which is the correct way to add an index to an Entity?

Entity entity = schema.addEntity("entity");
entity.setSuperclass("SuperClass");
entity.addIdProperty();
entity.addIntProperty("property").index();

or

Entity entity = schema.addEntity("entity");
entity.setSuperclass("SuperClass");
entity.addIdProperty();
Property property = entity.addIntProperty("property").getProperty();
entity.setIndex(property);

Or do they both do the same thing?

How to&Answers:

Use myProperty.index() for single property indexes (because it is most convenient).

For more complex indexes, like multi-column indexes, use addIndex(index):

Index index = new Index();
index.addProperty(property1);
index.addProperty(property2);
entity.addIndex(index);