因此,我们将GreenDao用作我们的ORM,就在昨天,我注意到新功能有些严重滞后。经过一番调查,我发现这不是我们的代码,而是greenDao生成的一些代码。
当我们调用someEntity.getContacts()
时,需要10到15秒才能运行查询。一周前,相同的代码非常快。有谁知道为什么要花这么长时间以及我们可以做什么来解决它?我已经包含了下面的代码。
构建模型的代码(DaoGenerator.java)这是我们实际编写的代码。
/*
* MyEntity Entity
*/
Entity lead = schema.addEntity("MyEntity");
myEntity.setSuperclass("MainSyncableEntity");
myEntity.implementsInterface("ClusterItem");
myEntity.implementsInterface("PointQuadTree.Item");
myEntity.implementsInterface("BaseEntityGetter");
myEntity.addIdProperty().autoincrement();
myEntity.setConstructors(false);
// Attributes
myEntity.addStringProperty("myEntityId").unique().index();
myEntity.addStringProperty("clientUniqueId").unique();
myEntity.addDateProperty("dateModified").notNull().index();
myEntity.addDateProperty("visibleCreated").notNull().index();
myEntity.addDateProperty("visibleModified").notNull().index();
myEntity.addStringProperty("notes").notNull();
myEntity.addStringProperty("businessName").notNull();
myEntity.addStringProperty("statusId").notNull();
myEntity.addStringProperty("ownerId").notNull();
/*
* Contact Entity
*/
Entity contact = schema.addEntity("Contact");
contact.setSuperclass("MainSyncableEntity");
contact.implementsInterface("BaseEntityGetter");
contact.addIdProperty().autoincrement();
contact.setConstructors(false);
// Attributes
contact.addStringProperty("contactId").unique().index();
contact.addStringProperty("clientUniqueId").unique();
contact.addDateProperty("dateModified").notNull().index();
contact.addStringProperty("firstName").notNull();
contact.addStringProperty("lastName").notNull();
contact.addStringProperty("email").notNull();
contact.addStringProperty("phonePrimary").notNull();
contact.addStringProperty("phoneSecondary").notNull();
contact.addStringProperty("ssn").notNull();
contact.addStringProperty("dateOfBirth");
Property leadContactSortOrder = contact.addIntProperty("sortOrder").notNull().getProperty();
/*
* MyEntity Relationships
*/
myEntity.addToMany(contact, contactMyEntityId, "contacts").orderAsc(contactSortOrder);
/*
* Contact Relationships
*/
// Properties
Property contactLeadId = contact.addLongProperty("contactMyEntityId").notNull().index().getProperty();
// Relationships
contact.addToOne(myEntity, contactMyEntityId);
运行缓慢的Generated方法:
public List<LeadContact> getContacts() {
long start = System.currentTimeMillis();
if (contacts == null) {
if (daoSession == null) {
throw new DaoException("Entity is detached from DAO context");
}
LeadContactDao targetDao = daoSession.getLeadContactDao();
List<LeadContact> contactsNew = targetDao._queryLead_Contacts(id);
synchronized (this) {
if (contacts == null) {
contacts = contactsNew;
}
}
}
return contacts;
}
然后使用生成的代码进行查询:
/** Internal query to resolve the "contacts" to-many relationship of Lead. */
public List<LeadContact> _queryLead_Contacts(long leadContactLeadId) {
synchronized (this) {
if (lead_ContactsQuery == null) {
QueryBuilder<LeadContact> queryBuilder = queryBuilder();
queryBuilder.where(Properties.LeadContactLeadId.eq(null));
queryBuilder.orderRaw("T.'SORT_ORDER' ASC");
lead_ContactsQuery = queryBuilder.build();
}
}
Query<LeadContact> query = lead_ContactsQuery.forCurrentThread();
query.setParameter(0, leadContactLeadId);
return query.list();
}