Pagination with large amount of data

Asked

Viewed 2,978 times

7

Thinking about performance, what is the best way to paginate a large amount of data?

I’m currently wearing a List<Produtos>, keeping around 500 products in it, and using subList(min,max), returns what I need. However, I believe that this is not the best solution and I would like to know other alternatives to this problem.

Edit

The idea is to maximize performance between unnecessary queries in the bank, because the maximum pool of the bank is 4.

  • Do you use Ibernate? Or the data is not in a comic?

  • Yes, I’m using Ibernate.

  • 2

    You can use the setFirstResult and setMaxResults. http://www.baeldung.com/jpa-pagination

  • Is the object list a result of a filter? Ex: user only wants female products. You are using native SQL or HQL?

  • No, it is the result of an SQL in a table, in the select case *.

  • @adelmo00 In the first screen no, only a Where searching for company. Later in case the customer click, is carried out a filter.

  • 1

    @Matheus will formulate an answer and put later, it will take a while

  • Give more context, what are you doing exactly? something like being looking for 500 products but always only displays 50 of them on the page? if your bank has more than 500 products your code already fails because it cannot display >=501?

  • @Brunorb Because it is a pagination, obviously it will not all be displayed at the same time.

Show 4 more comments

1 answer

8


I had this problem, I solved it with the tip from algaworks:

public List<Produto> filtrados(FiltroProduto filtro) {
    Criteria criteria = criarCriteriaParaFiltro(filtro);

    criteria.setFirstResult(filtro.getPrimeiroRegistro());
    criteria.setMaxResults(filtro.getQuantidadeRegistros());

    if (filtro.isAscendente() && filtro.getPropriedadeOrdenacao() != null) {
        criteria.addOrder(Order.asc(filtro.getPropriedadeOrdenacao()));
    } else if (filtro.getPropriedadeOrdenacao() != null) {
        criteria.addOrder(Order.desc(filtro.getPropriedadeOrdenacao()));
    }

    return criteria.list();
}

Of course you can improve the filter.

https://github.com/algaworks/aula-datatable-lazy-loading-primefaces

  • Tiago, what brings me to performance using criteria instead of loading all products on a list? That’s the question. How to make a pagination has already been answered in other comments.

  • 2

    @Matheus What would be more advantageous, make a query of 80000 items and store in a list or implement direct paging in the query? Imagine all these items being stored in an array, and now imagine a query that always returns the amount of items you need. Ex: 20 to 20.

  • @Diegoaugusto I don’t know what would be better, 20 customers accessing an array that already has the products loaded, or the 20 customers consulting the bank all the time, and only 4 customers can access the bank p/time.

  • 1

    @Matheus The point is not the Criteria but the fact of setting the query to return only a certain amount of records, from a certain line (firstResult and maxResults). The answer uses Criteria because she had to use some method (you didn’t show your code, which query technique you’re using, so any data access technique should serve). If you need a response closer to your current code, show your current code and give more details of your implementation.

  • @Matheus then you will have to analyze, how many times will you load this array? ?

  • @Diegoaugusto I currently load this array when the first client accesses the system home. Other clients only refer to the already loaded array in a cache form. What I don’t understand is whether the best way is to load everything or each customer search in the bank for the page they need, assuming that each page contains 20 products.

  • Okay, and after this first client enters the home the database data is not modified, IE, has no inclusion?

  • @Diegoaugusto No, the inclusion of new products is managed in another way, where when the base is updated, access to the system is blocked. So while the system is released, the products are not modified.

  • 2

    @Matheus then that’s what you have to analyze, the first load of the list will always take a little longer and I also believe it will require a little more than VM. If you make direct paging in the bank you save a little memory and also gain performance in the first query. I would necessarily make direct paging at the bank, because that way I wouldn’t have to do a "useless" load because I would only show the client what was needed

  • @Diegoaugusto Okay, thanks for the responstas.

  • @Matheus For nothing, good luck :)

  • @Matheus the edition was rejected, as the edition cannot add anything in the reply, which the author has not previously added.

Show 7 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.