db4o Developer Community

db4o open source object database, native to Java and .NET
Welcome to db4o Developer Community Sign in | Join
in Search
More Search Options

Query Optimization

Last post 07-28-2008, 09:27 PM by mattbishop. 13 replies.
Sort Posts: Previous Next
  •  07-16-2008, 04:20 PM 50165

    Query Optimization

    Hi,

     Can anybody plz tell me,  why this simple query is not optimized? I have an Order-class with an Id attribute and want to find an order with a specific Id. but the checkOptimization-method shows that this query is UNOPTIMIZED.

     

        public void getOrderByID(ObjectContainer db, final String id) {
            checkOptimization(db);
            ObjectSet<Order> results = db.query(new Predicate<Order>() {
                public boolean match(Order candidate) {
                    return candidate.getReferenceId().equals(id);
                }
            });
            for (Order order : results) {
                LOGGER.info(order);
            }
        }

     ////////////   checkOptimization method ////////////

        public static void checkOptimization(ObjectContainer db){
            ((ObjectContainerBase)db).getNativeQueryHandler().addListener(new Db4oQueryExecutionListener() {
                public void notifyQueryExecuted(NQOptimizationInfo info) {
                    System.err.println(info);
                }
            });
        }

     

    thanks.

  •  07-16-2008, 05:11 PM 50167 in reply to 50165

    Re: Query Optimization

    Have you tried == instead of .equals()?

    Brett 

  •  07-18-2008, 08:31 AM 50202 in reply to 50167

    Re: Query Optimization

    Yes, but it does not help. I can not even find my object with "==" and the query is furthermore  not optimized and a full-search will be done.
  •  07-18-2008, 09:31 AM 50205 in reply to 50202

    Re: Query Optimization

    Hi!

    Can you check against the field instead of get method? It seems the optimizer doesn't handle it.

    Goran

  •  07-18-2008, 09:42 AM 50206 in reply to 50205

    Re: Query Optimization

    Goran:
    Can you check against the field instead of get method? It seems the optimizer doesn't handle it.

    Hi Goran,

    just to make things clear: if bean-conventions are used, it should work with accessors, too... at least I think so...;-)

    So: if the field is called referenceId, getReferenceId() should be resolved by the NQ-Parser to fetch the appropriate field. But if the field is called id, but the accessor is still called getReferenceId(), the query cannot be optimized. Maybe this is the problem.

    And just another one: If I remember correctly, the accessor needs to be a "real simple" accessor, which consists simply of a "return referenceId"... if there's more code involved in the accessor, the NQ-Parser does a good job if it doesn't optimize the query...

    Cheers, Maik


    http://db4o.blogspot.com/
  •  07-18-2008, 01:40 PM 50209 in reply to 50206

    Re: Query Optimization

    thanks 4 replies.

    @  Gora:  I have checked against the field, but it is still unoptimized.

    @ Maik: I have the bean-conventions in my code:

     

    public  abstract class Order {
       
        private String referenceId;

        public String getReferenceId() {
            return referenceId;
        } ...

    I have about 100.000 Objects, and you can imagine how long does it take to run a full-search. I have no Idea.

     

     

  •  07-18-2008, 02:35 PM 50210 in reply to 50209

    Re: Query Optimization

    Just two ideas:

    • Do you have bloat.jar and db4o-nq.jar in your classpath?
    • Please change your class definition to: public abstract class Order {}. I'm not totally sure, but if I remember correctly there were some issues with abstract classes and nq-optimazation.
    Cheers, Maik

    http://db4o.blogspot.com/
  •  07-18-2008, 04:24 PM 50216 in reply to 50210

    Re: Query Optimization

    thanks Maik for your help, but unfortunately it did not help. I have had the both jars in my classpath. With or without the abstract definition, the result is the same.

    cheers, Mehdi

  •  07-18-2008, 06:42 PM 50218 in reply to 50216

    Re: Query Optimization

    Hi,

    the following simple test runs optimized. Can you provide a similiar test which shows the problem?

    Cheers, Maik

     

    import java.io.File;

    import com.db4o.Db4o;
    import com.db4o.ObjectContainer;
    import com.db4o.internal.ObjectContainerBase;
    import com.db4o.internal.query.Db4oQueryExecutionListener;
    import com.db4o.internal.query.NQOptimizationInfo;
    import com.db4o.query.Predicate;

    public class NativeQueryByStringTest {

        private String id;

        public NativeQueryByStringTest(String id) {
            this.id = id;
        }

        public String getId() {
            return id;
        }

        public static void main(String[] args) {
            new File("test.yap").delete();
            
            ObjectContainer db4o = Db4o.openFile("test.yap");
            for (int i = 0; i < 100000; i++) {
                db4o.store(new NativeQueryByStringTest("" + i));
            }
            db4o.commit();
            db4o.close();

            db4o = Db4o.openFile("test.yap");
            ((ObjectContainerBase) db4o).getNativeQueryHandler().addListener(
                    new Db4oQueryExecutionListener() {
                        public void notifyQueryExecuted(NQOptimizationInfo info) {
                            System.err.println(info);
                        }
                    });
            
            final String id = "12345";
            System.out.println(db4o.query(new Predicate<NativeQueryByStringTest>() {
                public boolean match(NativeQueryByStringTest candidate) {
                    return candidate.getId().equals(id);
                }        
            }));        
            db4o.close();
        }
    }


    http://db4o.blogspot.com/
  •  07-19-2008, 12:56 PM 50223 in reply to 50218

    Re: Query Optimization

    Hi Maik, thanks for ur post. I have the same code and after I have run your code the output was:

     UNOPTIMIZED/null
    [api.models.com.db4o.performance.NativeQueryByStringTest@139f2f6]

     

    Can you plz post your classpath file? mine is as follows:

    <?xml version="1.0" encoding="UTF-8"?>
    <classpath>
        <classpathentry kind="src" path="src/main/java"/>
        <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
        <classpathentry kind="lib" path="lib/log4j-1.2.11.jar"/>
        <classpathentry kind="lib" path="lib/bloat-1.0.jar"/>
        <classpathentry kind="lib" path="lib/db4o-7.2.39.10644-nqopt.jar"/>
        <classpathentry kind="lib" path="lib/db4o-7.2.39.10644-java5.jar"/>
        <classpathentry kind="output" path="bin"/>
    </classpath>
     

     

    cheers, Mehdi

  •  07-19-2008, 04:15 PM 50229 in reply to 50223

    Re: Query Optimization

    My output is:

    DYNOPTIMIZED/CANDIDATE.id == "12345"
    [NativeQueryByStringTest@1e4fc14]

    .classpath is nearly the same, at least no essential difference... 

    <?xml version="1.0" encoding="UTF-8"?>
    <classpath>
        <classpathentry kind="src" path="src"/>
        <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
        <classpathentry kind="lib" path="/home/mjablonski/db4o-7.2/lib/bloat-1.0.jar"/>
        <classpathentry kind="lib" path="/home/mjablonski/db4o-7.2/lib/db4o-7.2.39.10644-java5.jar"/>
        <classpathentry kind="lib" path="/home/mjablonski/db4o-7.2/lib/db4o-7.2.39.10644-nqopt.jar"/>
        <classpathentry kind="output" path="bin"/>
    </classpath>

    Very strange... I'm running the test from within Eclipse using JDK 1.6.07. Do you use any "unusual" settings for your project?

    Cheers, Maik


    http://db4o.blogspot.com/
  •  07-20-2008, 10:38 AM 50237 in reply to 50229

    Re: Query Optimization

    yes, it is strange. I m using Eclipse too. My JDK is 1.5, but that could not be the reason. thanks anyway Maik.

    Mehdi

  •  07-28-2008, 09:11 PM 50359 in reply to 50237

    Re: Query Optimization

    I am having the same kind of problem as this thread. Using db40 7.2.10988, JDK 5.

     

  •  07-28-2008, 09:27 PM 50360 in reply to 50359

    Re: Query Optimization

    I found the solution to my particular problem.

    My Data bean has private fields that begin with '_', like:

        private String _author;

    And the accessor methods do not use the underbar:

        public String getAuthor() { return _author; }

     

    When I change my private member to plain 'author', the queries start being optimized.

View as RSS news feed in XML