Search code examples
mdriven

PSQuery Performance


I have following model:

  A
  1
  |
  *
  B
^   ^
|   |
B1  B2

where B is an abstract superclass of B1 and B2. I now have a PSQuery like:

var data = es.PSQuery<A>().Where(..).SelectMany(x => x.Bs).ToList();

The generated SQL looks like following:

SELECT ALIAS.ID FROM B ALIAS WHERE ....

Then MDriven gets all discriminators in chunks to determine the type of the objects, so these can be quite some SQLs:

SELECT ALIAS.ID, ALIAS.DISCRIMINATOR FROM B ALIAS WHERE ID in (?, ? ....)

Now MDriven knows the types and can load the data. This process all in all takes much longer than a handmade SQL. So my questions are:

  1. Can I get the SQL from a PSQuery so that I can execute it by myself? In this case I build up a quite complicated expression and I really prefer building up the PSQuery to building the SQL string.
  2. If MDriven would load the descriminator in the first SQL, the deferred loading would not be necessary. We use a custom OR-Mapping for this table. Is it possible to include the discriminator into the first SQL?
  3. Is there a different hint to prevent this time consuming load behaviour (keeping the model unchanged)?

Solution

  • First : If you need a result set for multiple classes (an abstract result set) your load times would benefit from having the sub classes parent mapped (Subclasses written in one - parent - table)

    To get the generated SQL from a PSQuery:

     var sqltext = this.EcoSpace.OclPs.ReturnPlatformQuery(null, vars, model.OclPS);
    

    You can have Parent, Child or Own mapping of Subclasses. When you have "Own" you get 1 table per subclass + 1 table per superclass. In this scenario we first reduce to the specific ids based on discriminator - then fetch the data per subclass