[question] How to query a subset of a resultset efficiently

From: Laszlo Spoor (lspoor_cayenn..otmail.com)
Date: Wed Mar 02 2005 - 15:20:21 EST

  • Next message: Andrus Adamchik: "Re: [question] How to query a subset of a resultset efficiently"

    Hi everybody,

    I have noticed that when I use a 'paged query' and I try to query the
    results of the current resultset, Cayenne fires queries for all results
    found. I expected that Cayenne would only fire a query for the current page.
    By example:

    pPageSize = new Integer(15);

    [...]

      SelectQuery qry = new SelectQuery(MyClass.class);
      qry.setPageSize(pPageSize.intValue());

      // There are approximately 3000 records in total. However I set a page
    size of 15.
      List lst = mContext.performQuery(qry);

      List tmpList = new ArrayList();
      if (lst.size() > 0)
      {
        FotoHandler ftoh = new FotoHandlerImpl(mContext);
        int start = pCurrentRow.intValue();
        int end = pCurrentRow.intValue() + pPageSize.intValue();

        for (int i=start; i < end; i ++)
        {
          // only check for foto's that are actually shown.
          MyClass cls = (MyClass)lst.get(i);
          Foto fto = ftoh.getFirstForWoning(cls, fte);
          tmpList.add(i, new VrijstaandeWoning(cls, fto));
        } //if
      } //if
    [...]

    The code above will result in the correct results but also in firing 3000+
    queries. The code that follows now, will result in 'just' 15 queries, but
    this is expected. However, now I am looping trough 3000+ results for
    nothing.

    [...]
      SelectQuery qry = new SelectQuery(MyClass.class);
      qry.setPageSize(pPageSize.intValue());

      List lst = mContext.performQuery(qry);

      List tmpList = new ArrayList(lst.size());
      if (lst.size() > 0)
      {
        FotoHandler ftoh = new FotoHandlerImpl(mContext);
        int start = pCurrentRow.intValue();
        int end = pCurrentRow.intValue() + pPageSize.intValue();

        for (int i=0; i < lst.size(); i ++)
        {
          // only check for foto's that are actually shown.
          if (i >= start && i < end)
          {
            MyClass cls = (MyClass)lst.get(i);
            Foto fto = ftoh.getFirstForWoning(cls, fte);
            tmpList.add(i, new VrijstaandeWoning(cls, fto));
           } //if
           else
           {
             tmpList.add(i, null);
            } //if
          } //if
        } //if
    [...]

    My question is if this is expected behaviour?

    Thanks in advance, Laszlo

    _________________________________________________________________
    FREE pop-up blocking with the new MSN Toolbar - get it now!
    http://toolbar.msn.click-url.com/go/onm00200415ave/direct/01/



    This archive was generated by hypermail 2.0.0 : Wed Mar 02 2005 - 15:21:06 EST