Hi Hendrik,
Thank you for providing this case. This is certainly something that we want to improve. Would it be possible for you to send us a test project so we can investigate why this sql was generated by mendix? (you can send it the andrej.koelewijn@mendix.com)
Also, did you also try replacing the IN clause with an EXISTS clause? What query plan does that produce?
Update: some feedback from our R&D devs - Interesting point, if you look at the query cost, the query generated by Mendix is a lot cheaper than the second version.
Another update: it looks like the Index Scan Backward is killing performance of the first query. Can you try replacing the index with an descending index (idx_field$datapoint_datadate)?
Thanks,
Andrej
(product manager for the datastorage component)
Hendrik,
The statements being used are created by the Mendix runtime. This is a translation of what the Mendix developer creates in the modeler. So rewriting the query directly is not an option for the Mendix developer, only possibility is as Nikel states writing it in java directly. I can understand that this is something you would like to avoid and should only be done if all else fails.
The mendix developer doesn't have direct influence on the translation of the action in the modeler to sql queries, but they could try to find a different logical; way to retrieve the information that would cause less of a burden on the db. But as I don't have the context of the app or query, I'm not able to provide you with additional options, but your mendix developers might have alternative way to get the same results in their app, but with different queries.
Hi Hendrik,
I have a few thoughts here. Firstly, I agree with you that the Mendix generated query should be faster, assuming the retrieve command and Mendix data model are built the way I think. Could you have your developer share the domain model? Specifically, I’m interested in the relationship between Profile and Datapoint. Is it 1 Profile to many Datapoint? And what indexes have been created on the Datapoint entity?
Secondly, in this case it seems you’re retrieving the latest data point for a particular Profile. The developer could implement a new (additional) 1-1 association between Profile and Datapoint, and set that association during each write of a new Datapoint. A retrieve using that new association should be faster than either query here.
Since you're not running in the Mendix cloud you actually have a few options:
- Write your own implementation of the jdbc driver you're using, and replace the existing one with it. This driver would recognize queries like the first one and change them into the second one. This is hard to do but makes for a good story.
- Implement the retrieve using your own Java actions that connect to the database directly. You would basically be writing your own ORM library. As long as you only do this for a few specific queries this is not too hard to do.
Both these options are definitely not something a regular Mendix developer would be able to do and would rightly be advised against by most people. A more sensible approach might be to upgrade your database server so it has more than 256GB of ram.