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 byaboutat 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 databaseknowsthat 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 usingpackages 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: (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
