欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

性能提升--意见收集贴(数据超过4000条,就明显慢了),代码已贴出

程序员文章站 2022-06-02 13:02:00
...
简单说明下:用hibernate查询数据,OuterTask表记录越来越多(超4000条),感觉查询速度越来越慢.

已经用hibernate分页机制了,为什么还这么慢哦,恼火!
各位走过路过,一定要给点意见啊,谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢啦~

实际效果可见[url]http://www.ctask.cn/[/url]项目,别的menu一点就来,就它慢,个NN的。[再加个:数据库是mysql5.0]

package com.affectu.dao.pagination;

import java.util.List;
import org.hibernate.Session;
import com.affectu.category.Category;
import com.affectu.common.util.Helper;
import com.affectu.task.InnerTask;
import com.affectu.task.Task;

/**
* Oct 13, 2008
*
* @author daniel nathan
*/
public class CategoryTaskDataModel extends BaseDataModel<Task, Long> {

private Session em = Helper.getInstance().getSession();
// not use view for optimizing performance
private String sql = "select t1.* from OuterTask t1 where t1.tag regexp :tags union all select t2.* from InnerTask t2 where t2.tag regexp :tags order by publishDate desc ";

private String sqlCount = "select count(t.id) from ("
+ " select t1.id from OuterTask t1 where t1.tag regexp :tags union all select t2.id from InnerTask t2 where t2.tag regexp :tags ) t";

private Category category;

public CategoryTaskDataModel(Category category) {
this.category = category;
}

@Override
public Task findById(Long id) {
return (Task) em.get(Task.class, id);
}

@Override
public Long getCount() {
// TODO Auto-generated method stub
Long count;
count = Long.valueOf(em.createSQLQuery(sqlCount).setParameter("tags",
this.getTags()).uniqueResult().toString());
return count;
}

@SuppressWarnings("unchecked")
@Override
public List<Task> getList(Integer firstRow, Integer maxResults) {
// TODO Auto-generated method stub
return em.createSQLQuery(sql).addEntity("t", InnerTask.class)
.setParameter("tags", this.getTags()).setFirstResult(firstRow)
.setMaxResults(maxResults).list();
}

private String getTags() {
List<Category> children = category.getChildren();
String tags = "";
for (int i = 0; i < children.size(); i++) {
tags += "." + children.get(i).getName() + ".|";
}
tags += "." + category.getName() + ".";
tags = tags.replaceAll("[+]*", "");
return tags;
}

public void finalize() {
em.close();
}

public void destroy() {
em.close();
}
}


对应的entiy

package com.affectu.task;

import java.io.Serializable;
import javax.persistence.Entity;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.search.annotations.Indexed;
import org.jboss.seam.annotations.Name;
/*
* 2008-8-15
* @author daniel nathan
*/
@Entity
@Name("outerTask")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@Indexed
public class OuterTask extends Task implements Serializable {

}



/*
* 2008-8-15
* @author daniel nathan
*/
//@Entity
@Name("task")
@MappedSuperclass()
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class Task extends EntityBase implements Serializable {
@Field(name = "tag", index = Index.TOKENIZED, analyzer = @Analyzer(impl = ChineseAnalyzer.class))
private String tag;
private int budget;
private int bidDay;
private int finishDay;
@Temporal(value = TemporalType.TIMESTAMP)
private Date publishDate;
private String attachName;
private String url;
private Type type;
public enum Type {
INNER, OUTER
}
相关标签: mysql 优化