Yet Another 10 Common Mistakes Java Developers Make When Wri
(Sorry for that click-bait heading. Couldn’t resist ;-) )
Getting SQL right or wrong shouldn’t be about that You’re-Doing-It-Wrong? attitude that can be encountered often when evangelists promote their object of evangelism. Getting SQL right should be about the fun you’ll have once you do get it right. The things you start appreciating when you notice that you can easily replace 2000 lines of slow,hard-to-maintain,and ugly imperative (or object-oriented) code with 300 lines of lean functional code (e.g. using?),or even better,with 50 lines of SQL. We’re glad to see that our blogging friends have started appreciating SQL,and most specifically,window functions after reading our posts. For instance,take
So,after our previous,very popular posts: … we’ll bring you: Yet Another 10 Common Mistakes Java Developers Make When Writing SQLAnd of course,this doesn’t apply to Java developers alone,but it’s written from the perspective of a Java (and SQL) developer. So here we go (again): 1. Not Using Window FunctionsAfter all that we’ve been preaching,this must be our number 1 mistake in this series.??of them all. They’re so incredibly useful,they should be the number one reason for anyone to switch to a better database,e.g. PostgreSQL: Mind bending talk by??about??at tonight's?. My new resolution: Install PostgreSQL and study SQL standard at once.
— Peter Kofler (@codecopkofler)?
If free and/or Open Source is important to you,you have absolutely no better choice than using??(and you’ll even get to use the free?,if you’re a Java developer). And if you’re lucky enough to work in an environment with Oracle or SQL Server (or DB2,Sybase) licenses,you get even more out of your new favourite tool. We won’t repeat all the window function goodness in this section,we’ve blogged about them often enough: The Cure: Remove MySQL. Take a decent database. And start playing with window functions. You’ll never go back,guaranteed. 2. Not declaring NOT NULL constraintsThis one was already part of a previous list where we claimed that you should add as much metadata as possible to your schema,because your database will be able to leverage that metadata for optimisations. For instance,if your database?knows?that a foreign key value in Now let’s have another look at?
3. Using PL/SQL Package StateNow,this is a boring one if you’re not using Oracle,but if you are (and you’re a Java developer),be very wary of PL/SQL package state. Are you really doing what you think you’re doing? ,e.g. FUNCTION next_n RETURN NUMBER; Wonderful,so you’ve created yourself an in-memory counter that generates a new number every time you call? But no,it’s probably not the session you might have thought of. We Java developers connect to databases through connection pools. When we obtain a JDBC Connection from such a pool,we recycle that connection from a previous “session”,e.g. a previous HTTP Request (not HTTP Session!). But that’s not the same. The database session (probably) outlives the HTTP Request and will be inherited by the next request,possibly from an entirely different user. Now,imagine you had a credit card number in that package…? Not The Cure: Nope. Don’t just jump to using??packages FUNCTION next_n RETURN NUMBER; Because:
So,don’t. Not The Cure: I know. PL/SQL can be a beast. It often seems like such a quirky language. But face it. Many things run much much faster when written in PL/SQL,so don’t give up,just yet. Dropping PL/SQL is not the solution either. The Cure: At all costs,try to avoid package state in PL/SQL. Think of package state as of? Instead,pass state as arguments through procedures and functions. This will avoid side-effects and make your code much cleaner and more predictable. Or,obviuously,persist state to some table. 4. Running the same query all the timeMaster data is boring. You probably wrote some utility to get the latest version of your master data (e.g. language,locale,translations,tenant,system settings),and you can query it every time,once it is available. At all costs,don’t do that. You don’t have to cache many things in your application,as modern databases have grown to be extremely fast when it comes to caching:
So,for your average query,there’s virtually no need for an ORM second-level cache,at least from a performance perspective (ORM caches mainly fulfil other purposes,of course). But when you query master data,i.e. data that never changes,then,network latency,traffic and many other factors will impair your database experience. The Cure: Please do take 10 minutes,?,and use its?,that ships with various built-in invalidation strategies. Choose time-based invalidation (i.e. polling),choose?,or?NOTIFY?for event-based invalidation,or just make your cache permanent,if it doesn’t matter. But?don’t?issue an identical master data query all the time. … This obviously brings us to 5. Not knowing about the N+1 problemYou had a choice. At the beginning of your software product,you had to choose between:
So,obviously,you chose an ORM,because otherwise you wouldn’t be suffering from?“N+1″. What does “N+1″ mean? . Essentially,you’re running: -- And then,for each book: Of course,you could go and tweak your hundreds of annotations to correctly prefetch or eager fetch each book’s associated author information to produce something along the lines of:
Since you’re already using an ORM,this might just mean resorting to native SQL – or maybe you manage to express your query with JPQL. Of course,we agree with Alessio Harri in believing that you should use jOOQ together with JPA: Loved the type safety of??today. OpenJPA is the workhorse and??is the artist :) #80/20
— Alessio Harri (@alessioh)?
The Takeaway: While the above will certainly help you work around some real world issues that you may have with your favourite ORM,you could also take it one step further and think about it this way. After all these years of pain and suffering from the?,the JPA 2.1 expert group is now trying to tweak their way out of this annotation madness by adding more declarative,annotation-based fetch graph hints to JPQL queries,that no one can debug,let alone maintain. The alternative is simple and straight-forward SQL. And with Java 8,we’ll add functional transformation through the Streams API.?. But obviuosly,your views and experiences on that subject may differ from ours,so let’s head on to a more objective discussion about… 6. Not using Common Table ExpressionsWhile common table expressions obviously offer readability improvements,they may also offer performance improvements. Consider the following query that I have recently encountered in a customer’s PL/SQL package (not the actual query):
Let’s consider a bit of history. MySQL has always been very easy to install,and it has had a great and active community. This has lead to MySQL still being the RDBMS of choice with virtually every web hoster on this planet. Those hosters also host PHP,which was equally easy to install,and maintain. BUT! We Java developers tend to have an opinion about PHP,right? It’s summarised by this image here:
Well,it works,but how does it work? The same can be said about MySQL. MySQL has always?worked somehow,but while commercial databases like Oracle have made tremendous progress both in terms of query optimisation and feature scope,MySQL has hardly moved in the last decade. Many people choose MySQL primarily because of its price (USD $ 0.00). But often,the same people have found MySQL to be slow and quickly concluded that SQL is slow per se – without evaluating the options. This is also why all NoSQL stores compare themselves with MySQL,not with Oracle,the database that has been winning the??benchmarks almost forever. Some examples: While the last article bluntly adds?“(and other RDBMS)”?it doesn’t go into any sort of detail whatsoever,what those?“other RDBMS”?do wrong. It really only compares MongoDB with MySQL. The Cure: We say: Stop complaining about SQL,when in fact,you’re really complaining about MySQL. There are at least four very popular databases out there that are incredibly good,and millions of times better than MySQL. These are: (just kidding about the last one,of course) The Takeaway: Don’t fall for agressive NoSQL marketing.?????company,even if MongoDB continues to disappoint,technically. ??. Both companies are solving a problem that few people have. They’re selling us niche products as commodity,making us think that our?real?commodity databases (the RDBMS) no longer fulfil our needs. They are well-funded and have big marketing teams to throw around with blunt claims. In the mean time,??just got even better,and you,as a reader of this blog / post,are about to bet on the winning team?:-) … just to cite??once more: History of NoSQL according to?
— Edd Dumbill (@edd)?
The Disclaimer: This article has been quite strongly against MySQL. We don’t mean to talk badly about a database that perfectly fulfils its purpose,as this isn’t a black and white world. Heck,you can get happy with SQLite in some situations. MySQL,being the cheap and easy to use,easy to install commodity database. We just wanted to make you aware of the fact,that you’re reference:http://java.dzone.com/articles/yet-another-10-common-mistakes (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- mysql – 将所有记录保存在“WHERE IN()”子句中,即使找不
- Mysql学习几个缩减MySQL以节省磁盘空间的建议
- python – 根据Django中的项目分组进行分页
- MYSQL数据库MySQL删除外键问题小结
- SQL Server数据库分区表
- javascript – 尝试调查页面加载性能错误:什么是“脚本”
- django.db.utils.InternalError: (1153, "Got a packet
- MYSQL数据库MySQL Left JOIN时指定NULL列返回特定值详解
- MySQL UNION 与 UNION ALL 语法与用法
- Mysql实例MySQL 5.6 如何更改安全的处理密码探讨