Infinite loop for pagination's automatic management when querying with limit and order by #1381

fredyw opened this Issue Mar 7, 2013 · 2 comments


None yet
4 participants

fredyw commented Mar 7, 2013

When an index exists in the database and we perform a query with a limit and an order by clause using pagination's automatic management, this code will go to an infinite loop.

public class OrientDBTest {
    public static void main(String[] args) {
        ODatabaseDocumentTx db = new ODatabaseDocumentTx("local:testdb/foo");
        if (db.exists()) {
  "admin", "admin");

        OClass fooClass = db.getMetadata().getSchema().createClass("foo");
        fooClass.createProperty("id", OType.INTEGER);
        // if the index is removed, the automatic management works
        fooClass.createIndex("id_idx", OClass.INDEX_TYPE.UNIQUE, "id");

        int n = 100;
        for (int i = 0; i < n; i++) {
            ODocument doc = new ODocument("foo");
            doc.field("id", i);

        // This will create an infinite loop because
        // the id property specified in the order by clause is indexed
        OSQLSynchQuery<ODocument> query = new OSQLSynchQuery<ODocument>(
            "select from foo order by id asc", 10);
        for (List<ODocument> docs = db.query(query); !docs.isEmpty(); docs = db.query(query)) {
            for (ODocument doc : docs) {


@ghost ghost assigned laa Mar 7, 2013

vemod commented Jun 18, 2013

How about using Iterator design pattern to avoid nested loop?
I have implemented infinite loop for mysql and don't have nested loops. just single loop over an Interator object.query(). this keeps code simpler, shorter and easier to read.

one could give a paginator object as param for the iterator which would know how to paginate: if its limit 0, 100 or some id > LastId as if data is changing dynamically having limit 0, 100 may not always return expected result (e.g. deleting all expired records. so having date < "2013-01-01" limit 1000, 100 will not return results while date < "2013-01-01" and id > LastId limit 100 will return records to delete.

so having "Paginator" would add a lot of flexibility to how the actual pagination is being performed.


lvca commented Mar 28, 2014

Fixed log time ago.

@lvca lvca closed this Mar 28, 2014

@lvca lvca added the bug label Mar 28, 2014

@lvca lvca modified the milestones: 1.7, 2.0 May 28, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment