java - Optimize the way to get the previous and next record -
optimize way previous , next record.
i made implementation previous record , next, not efficient. enter log detail screen, in case 1 player, whole search of records check next , previous done.
in case search equipment, can case in detail reached through search engine filter paged results obtained. how can improve implementation previous , next record? want not have retrieve records, records need. implementation:
my controller:
@controller public class playercontroller { @autowired private playerservice pservice; @requestmapping(value="/players/{id}", method = requestmethod.get) public final modelandview printplayerdetail(@pathvariable(value = "id") integer id, locale locale) throws controllerexception { modelandview view = new modelandview("players/detail"); player p = null; navigatordto navigator = new navigatordto(); try { p = pservice.getbyid(id); navigator.setid(p.getid()); navigator.setrecords(pservice.getnavigator(p.getteam().getid())); } catch (serviceexception e) { throw new controllerexception(this.messages.getmessage(error_query, null, locale), e); } playerdto dto = new playerdto(); dto.setid(p.getid()); dto.setname(p.getname()); if (p.getteam() != null) { dto.setteam(p.getteam().getid()); } view.addobject("navigator", navigator); view.addobject("player", dto); return view; } }
my service:
@service public class playerserviceimpl implements playerservice { @autowired private playerdao pdao; @override @transactional public list<integer> getnavigator(integer teamid) throws serviceexception { list<integer> result = new arraylist<integer>(); try { list<player> players = pdao.findbyteanm(teamid); (player p : players) { result.add(p.getid()); } } catch (facadeexception e) { throw new serviceexception(e); } return result; } }
my navigation class:
public final class navigatordto implements serializable { private static final long serialversionuid = 1l; private integer id; private list<integer> records = new arraylist<integer>(); public integer getid() { return id; } public void setid(final integer id) { this.id = id; } public list<integer> getrecords() { return records; } public void setrecords(final list<integer> records) { this.records = records; } /** * next id * @return */ public integer getnext() { if ( id!=null ) { integer actualposition = records.indexof(id); try { return records.get(actualposition + 1); } catch (exception e) { return null; } } else { return null; } } /** * previous id * @return */ public integer getprevious() { if (id != null){ integer actualposition = records.indexof(id); try { return records.get(actualposition - 1); } catch (exception e) { return null; } } else { return null; } } /** * first id * @return */ public integer getfirst(){ if (id != null) { try { return records.get(0); } catch (exception e) { return null; } } else { return null; } } /** * last id * @return */ public integer getlast(){ if (id != null) { try{ return records.get(records.size() - 1); } catch (exception e){ return null; } } else { return null; } } /** * total records * @return total */ public int gettotalrecords(){ return (records == null) ? 1 : records.size(); } /** * actual position * @return */ public int getactualposition(){ return (records == null) ? 1 : records.indexof(id) + 1; } }
offset pagination doing inherently slow.
see http://use-the-index-luke.com/sql/partial-results/fetch-next-page
unfortunately jpa doesn't support keyset paging (i think). have use raw sql.
Comments
Post a Comment