Improve query efficiency

 

original:

List<Convention> conventions = test2Boy.getConventions();
            for (Convention convention : conventions) {
                //Because \n will not wrap in html, so convert \n to br
                convention.setAnswer(ConventionUtil.convertBr(convention.getAnswer()));
                VoteLog voteLogTmp=this.voteLogDao.get( "user.id", user2.getId(),"convention.id",convention.getId());
                if(null!=voteLogTmp){
                    convention.setHasStar(true);
                }
            }

 There are as many elements as there are conventions, and the sql statement must be executed as many times as possible

optimization:

List<Convention> conventions = test2Boy.getConventions();
            int size=conventions.size();
            Object[]objects=new Object[size];
            for (int i=0;i<size;i++){
                Convention convention=conventions.get(i);
                objects[i]=convention.getId();
            }
            List voteLogTmps=this.voteLogDao.getList("user.id", user2.getId(),"convention.id",objects);
            int voteLogSize=voteLogTmps.size();
            for (int j=0;j<voteLogSize;j++){
                VoteLog voteLogTmp=(VoteLog)voteLogTmps.get(j);
                Convention convention=voteLogTmp.getConvention();
                if(null!=convention){
                    convention.setHasStar(true);
                }
            }

 

The executed sql statement is:

/* criteria query */ select
        this_.id as id1_8_2_,
        this_.conventionId as conventi4_8_2_,
        this_.status as status2_8_2_,
        this_.userId as userId5_8_2_,
        this_.vote_time as vote3_8_2_,
        convention2_.id as id1_2_0_,
        convention2_.answer as answer2_2_0_,
        convention2_.pic as pic3_2_0_,
        convention2_.stars as stars4_2_0_,
        convention2_.status as status5_2_0_,
        convention2_.update_time as update6_2_0_,
        user3_.id as id1_7_1_,
        user3_.email as email2_7_1_,
        user3_.level as level3_7_1_,
        user3_.nickname as nickname4_7_1_,
        user3_.password as password5_7_1_,
        user3_.potrait as potrait6_7_1_,
        user3_.username as username7_7_1_
    from
        t_vote_log this_
    left outer join
        t_convention convention2_
            on this_.conventionId=convention2_.id
    left outer join
        t_user user3_
            on this_.userId=user3_.id
    where
        this_.userId=?
        and this_.conventionId in (
            ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?
        )

 

 

 

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326856270&siteId=291194637