A big draw for using an off-the-shelf ORM was so that we didn't have to write our own (kind of bad and slightly wrong -- those darn transactions) caching implementations for the custom one-off ORM that existed previously. A lot of our internal meta-model is very static, so clearly caching would be a HUGE win for performance, right?
Not so fast, turbo. Let me continue...
The headline feature for the 2.0 Monitor is "Query Analysis." Coupled with the MySQL Proxy, the Monitor receives captured query data to/from a MySQL server. Once at the monitor, the data can be aggregated, analyzed, and reported upon. What better test for this feature than to use it on ourselves, to tune ourselves!
And this brings me back to hibernate caching. In the course of monitoring ourselves, I noticed that a certain query was happening WAY more than it should be, just based on my gut feeling. The query in question loaded an object that was generally static -- save for one value that represents the frequency of how often some data should be collected. Its the only mutable value, and once in place, it rarely changes.
Hrm... how to debug. First, we checked the cache settings. Whoops -- WAY too low for both the cache expiry timeout and the max cache elements. Fix that. Still sucks. Some cursory hibernate source and log perusal showed that the cache for these objects was being invalidated at a rapid rate. Yes, the entire cache. Even though the objects are essentially static, the query cache takes the safe route and says any change to a related table invalidates any and all queries that reference that table. This makes sense, from a generic cache standpoint. But I thought to myself -- surely there has to be a way. *I* am smarter than hibernate in this case, and *I* can more rightly determine when the query results should be invalidated. Lucky for me, hibernate allows you to extend the StandardQueryCache "up to date" policy checks. w00t. I implemented one, overrode the timeout policy for the object(s) in question, and re-ran tests. FAILURE. Turns out I am not smarter than hibernate.
However, in the process of implementing the custom query cache policy, I had debugged through some more hibernate code and noticed that "natural id" queries are treated special. Some more google-fu, and quickly I come across Queries by Natural Identifier in the hibernate docs.
Now, the docs just aren't real clear on what optimizations can be made internally by utilizing the Criteria with a natural id restriction. But, as I was just in that section of code, I could correlate it. Here's the meaty bit -- if you make a natural id / key lookup, and hibernate recognizes it as such, it can bypass the table timestamp invalidation and go directly to second-level cache to fetch the object. Hibernate knows, with an immutable and unique natural key, that a table modification will not effect the composition of the object in question (of course, an object modification would, and it would have been evicted from L2 cache).
I cannot overemphasize the utility of this discovery. You see, we were making frequent inserts into the table. But existing objects (rows) where changed almost never. But without the natural key lookup, the inserts invalidated all results in the query cache. There you have why I was seeing way more selects for the same objects than I had anticipated.
Some quick assurances that we mapped the natural id correctly, some quick refactoring of the HQL into Criteria queries with natural id restrictions, and whammo, we're good. Lets run the tests and query analysis again... ruh roh. OH COME ON! (not my exact reaction, but I think you can guess what it was really like).
Confident that the natural id cache lookup optimization was what I really really really wanted, there had to be something else going on. More debuggage ensued. I set a breakpoint near the same area in StandardQueryCache where I first noticed the query cache optimization in the first place. Lo and behold, the hibernate metadata for saying "i am a natural key lookup" was returning false.
I am not amused. I am confident my hibernate mapping is correct, because the unique index was present in the schema. Think. Think. Think. Well, I had recently been on an effort to move from the hbm xml mappings to hibernate annotations mappings. @NaturalId support was, in fact, the very reason I had recently upgraded the annotations jar. On a hunch, I reverted the persistence mapping back to the xml form
Being the good open-source citizen, I made a hibernate forum post that detailed my findings, including simplified sample code demonstrating the problem. The good folks on the hibernate forum (after questioning the sillyness of my contrived example) were quick to recognize the problem, and I got a hibernate jira issue opened.
The workaround, obviously, is leave the xml mapping in place until the fix makes it into a hibernate release. Not too bad of a deal, I guess, considering the overall win I now have in my cache hit ratio.
In conclusion -- if it makes sense for your data model, the natural id query cache optimization can be a huge performance win for your app. If you have immutable, or rarely changed objects with a constant natural key lookup -- look into the Criteria natural id restriction. And, use the xml mapping until the bug is fixed.
ps -- there is one other performance note to consider, actually. If using the natural id query, and it returns no rows, this NULL result will not be cached. So, if you have more of these than 'object/row found' results, you will still get tons of these queries that you don't expect. Either stop using the natural key optimization (if 'not found' is more common), or extend your object/schema to include a 'not supported' field. In our case, the lack of a row meant "not supported" and we had a flag "not supported" in case it was supported, but then went away. In those cases where something was frequently "not supported" I simply went ahead and created the object/row and just set the flag to false -- thus ensuring the natural key optimization was not subverted.
230 comments:
«Oldest ‹Older 201 – 230 of 230Pretty! This was a truly great post. Much obliged to you for giving these subtleties.best interiors
The article was up to the point and described the information very effectively.
vietnam airlines quốc tế
Có chuyến bay từ Hàn Quốc về Việt Nam không
Cách săn vé máy bay giá rẻ tu Nhat Ban ve Viet Nam
đặt vé máy bay từ singapore về việt nam
Cách săn vé máy bay giá rẻ tu Dai Loan ve Viet Nam
đặt vé máy bay từ canada về việt nam
Dubai Fun Club for luxurious Dubai Escorts and entertainment services. You can easily find the best Escorts in Dubai on our website
www.escortsmate.com
escortsmate.com
https://www.escortsmate.com
Goodness, so excellent and great post! A debt of gratitude is in order for offering a chance to peruse an incredible and fanciful blog. It gives me loads of joy and interest. A debt of gratitude is in order for sharing…
DevOps Training in Hyderabad
Nice blog! Thanks for sharing this valuable information
RPA Training in Bangalore
RPA Training in Pune
I need to thank you for this great read!! I most certainly partook in every single piece of it. I have you book-set apart to check out new stuff you post…
Machine Learning Training in Hyderabad
A Boutique Management Consulting Outfit Providing Actionable Insights
& Strategic Advisory to Clients
Services
C-GUARD has a wide range of scrub suits like the basic scrubs that can be used by all Doctors, medical & para medical staff, medical students, interns etc.Cguard
I am inspired with your post writing style & how continuously you describe this topic...Best MicroNutrients Company in India
I am very much pleased with the contents you have mentioned. I wanted to thank you for this great article. Royal Enfield Jacket
I like your post. I appreciate your blogs because they are really good. Please go to this website for the Data Science Course: For Data Science Course Data Science course in Bangalore. These courses are wonderful for professionalism.
Our the purpose is to share the reviews about the latest Jackets,Coats and Vests also share the related Movies,Gaming, Casual,Faux Leather and Leather materials available Rainbow Sunburst Jacket
Great post. keep sharing such a worthy information.
Swift Developer Course in Chennai
Swift Training in Bangalore
Learn Swift Online
I love to recommend you Where can crawl Exciting Products latest Jackets, Coats and Vests Click Here James Bond Peacoat
tiktok jeton hilesi
tiktok jeton hilesi
referans kimliği nedir
gate güvenilir mi
tiktok jeton hilesi
paribu
btcturk
bitcoin nasıl alınır
yurtdışı kargo
Aimore Technologies is the best Selenium training institute in Chennai with 6+ years of experience. We are offering online and classroom training.
Visit Us: Selenium Training in Chennai
İnstagram takipçi satın al! İnstagram takipçi sitesi ile takipçi satın al sende sosyal medyada fenomen olmaya bir adım at. Sende hemen instagram takipçi satın almak istiyorsan tıkla:
1- takipçi satın al
2- takipçi satın al
3- takipçi satın al
Statistics students and professor are worried to find the deviation calculator because their work depends on it. Billionaire Boys Club Varsity Jacket
Udaipur, formerly the capital of the Mewar Kingdom, is a city in the western Indian state of Rajasthan. Founded by Maharana Udai Singh II in 1559, it’s set around a series of artificial lakes and is known for its lavish royal residences. City Palace, overlooking Lake Pichola, is a monumental complex of 11 palaces, courtyards and gardens, famed for its intricate peacock mosaics.
Golden Triangle Tour with Udaipur (8 Days 7 Nights) FLAT 30% OFF | Golden Triangle Tour Packages With Udaipur | Golden Triangle Tour With Udaipur | 7 Days Delhi Agra Jaipur Udaipur Tour | Golden Triangle With Udaipur Tour
Goodness, this is enticing research. I'm cheerful I seen this as and got to section it. extraordinary occupation upon this content material. I leaned toward it bounty. much thanks to you for the inescapable and specific data. Dll File Fixer Full Version
Hi there, I would like to subscribe for this web
site to take hottest updates, thus where can i do it please
help. BUY HYIPS
That is a valuable instrument and every individual who utilizes the component and downloads information and riding on the web, needs to utilize this application.Malwarebytes Free Activation Key
Great blog. Thanks for sharing such a useful content. fence-companies-raleigh
Great blog. Thanks for sharing. It service west palm beach
Thanks for the blog. fence company milwaukee
https://designingcourses.in/graphic-designing-courses-in-bangalore/
Learn graphic designing courses in bangalore we provide best graphic designing training and 100% placement assistance
Python's syntax is straightforward and quick to learn, which emphasise readability and lowers maintenance costs. This object-oriented, high-level, interpreted programming language has dynamic semantics. click here to check our python training in Bangalore!
If you are considering taking up Data Science training in Bangalore!
then it can help you in shaping a successful career. Today, there isn’t any business sector which is untouched by the data scientist. So, be it the eCommerce sector, IT sector or the Medical sector, each sector is embraced with qualified data scientists who help in processing huge amount of data into valuable information
đại lý China Airlines tphcm
đổi giờ vé máy bay Japan Airlines
cách mua thêm hành lý ký gửi Eva Air
Post a Comment