ORM or SQL in a large, scalable and supported web application?

I know that in this thread there are already many posts posted on the Internet on this topic.

However, many people tend to focus on different things when they talk about it. My main goal is to create a scalable web application that is easy to maintain. The speed of development and support is much more appreciated by ME than raw performance (or I could use Java instead).

This is because I noticed that when a project grows in code size, you must have supported code. When I first wrote my application in a procedural way and without any frameworks, it became a nightmare only after 1 month. I was completely lost in the jungle of spaghetti code lines. I had no structure at all, although I was so afraid to realize it.

Then I realized that I had to have the structure and code correctly. I started using CodeIgniter. It really gave me structure and supported code. Many users say that the framework slows down, but I think they missed the picture. The code should be convenient and understandable.

Framework + OOP + MVC made my web application so structured that adding features was no longer a problem.

When I create a model, I tend to think that it represents a data object. Maybe a form or even a table / database. So I thought of ORM (doctrine). Perhaps this will be another great implementation in my web application, giving it more structure so that I can focus on the functions and not repeat myself.

However, I have never used ORM before, and I only learned its basics, why it's good to use, etc.

So now I ask all of you guys who, just like me, strive for supported code and know how important this is, is ORM (doctrine) mandatory for supported code in the same way as framework + mvc + oop?

I want more life experience tips than โ€œraw sql fasterโ€ advice, because if I only care about raw performance, I had to first abandon the + mvc + oop framework and continue to live in code code.

It seems to be so good at the MVC framework, where the models are tables.

Right now I have 150 sql queries in one file that make things easier like getting record by id, getting record by name, getting record by email, getting record X, etc. I thought that ORM could shorten these lines, otherwise I am sure that in the future it will grow to 1000 sql-lines. And if I change one column, I have to change them all! what a nightmare I thought again. And perhaps this can also give me good models that fit the MVC pattern.

Is ORM the right way for structure and supported code?

+4
source share
5 answers

Ajsie

My vote for ORM. I am using NHibernate. This is not ideal, and there is a significant learning curve. But the code is much more convenient to maintain, much more OOP. It is almost impossible to create an application using OOP without ORM if you do not like a lot of duplicate code. This will certainly eliminate the possibly vast majority of your SQL code.

And here is another thing. If you are going to create an OOP system, you still write your own O / R Mapper. You will need to call dynamic SQL or saved procs, get the data as a reader or data set, convert it to an object, relate relations with other objects, change object modifications to sql-inserts / updates, etc. What you write will be slower and more buggy than NHibernate or something that has been on the market for a long time.

The only other choice is to create a very procedural data-oriented application. Yes, in some areas it can work faster. I agree that performance is important. But the important thing is that its FAST ENOUGH. If you save a few milliseconds here and there while doing procedural code, your users will not notice a performance increase. But you will notice cheesy code.

The greatest bottleneck performance in ORM is the right way to pre-extract and lazily load objects. This concerns issues with n-query with ORM. However, they are easily solved. You just need to configure your object queries and limit the number of calls in the database, talk about it when to use joins, etc. NHibernate also supports a rich caching mechanism, so you donโ€™t get into the database at any time.

I also disagree with what they say about user productivity, and service about encoders. If your code is not just supported, it will be a bug and a slow addition of functions. Your users will take care of this.

I will not say that every application should have an ORM, but I think most of them will benefit. Also, don't be afraid to use your own SQL or stored procedures with ORM from time to time where necessary. If you need to do batch updates for millions of records or write a very complex report (hopefully against a separate, denormalized report database), then direct SQL is the way to go. Use ORM for OOP, transactional, business logic, and CRUD and use SQL for exceptions and edge cases.

I would recommend reading the material by Jeffrey Palermo on NHibernate and Onion architecture. Also, take its flexible boot camp or other classes to learn O / R Mapping, NHibernate, and OOP. What we use: NHibernate, MVC, TDD, Injection Dependency.

+6
source

Many users say that slowing down, but I think they missed the big picture. The code SHOULD BE DECENT AND EASY TO UNDERSTAND.

A well-structured, highly supported system is useless if its performance is sucking!

Manipulation is what is beneficial for the coders who create the application. Raw performance benefits those people who use the application for their work (or something else). So whose problems should be of utmost importance: those who build the system or those who pay for it?

I know that this is not so simple, because the client will ultimately pay for a poorly structured system - perhaps more errors, and, of course, more time to fix them, more time to implement improvements in the application. As it usually happens, all this is a compromise.

+5
source

I began to develop, like you, without orm tools.

Then I worked in companies where software development was more industrialized, and they all use some kind of orm mapping tool (with more or less functions). Development is much simpler, faster, creates more convenient code, etc.

But I also saw the flaws of these tools: very slow performance. But it was mainly an abuse of the instrument (in this case, sleep mode).

The Orm tool is a very complex tool, so itโ€™s easy to use, but if you have experience working with them, you can get almost the same characteristics as with raw sql. I would have three tips for you:

  • If performance is not critical, use the orm tool (choose a good one, I am not developing php, so I cannot give you a name)
  • Be sure to add each added function to check the sql that the orm tool creates and sends to the database (for example, using the logging tool). Think about how you would write your requests. Most of the inefficiencies of the orm tools come from unwanted data that is collected from db, a unique query divided into several, etc. Slowness rarely comes from the tool itself
  • Do not use the tool for everything. It's wise to choose when not to use it (you reduce maintainability every time you access raw db), but sometimes it's not just the worst to get the orm tool to do something for which it was not designed.

Edit: The Orm tool is most useful with a very complex model: many relationships between objects. In most cases, this occurs in the application configuration part or in the complex business part of the application. Thus, it is less useful if you have only a few entities, and if there is less chance, they will be changed (reorganized).

The boundary between several entities and many is unclear. I would say that more than 50 different types (sql tables, without connection tables) are many, and less than 10.

I do not know what was used to create stackoverflow, but it must have been very carefully tested before.

If you want to create a website that will get such a heavy load, and if you do not have experience with this, try to attract someone from your team that has already worked on such sites (performance testing using a real data set and a representative the number of simultaneous users is not an easy and fast task to implement). Having someone who has work experience will greatly speed up the process.

+4
source

It is very important to have high reliability. Ive developed a large scalable web application with a high degree of excellence. The big drawback was the maintenance of the system, that is, the development of new functions. If you want to slow down the development of customers, they will look for other systems / applications. His trade. Most orms have functions if you need to do optmized queries directly on sql. Ohm itself is not a bottleneck. I talk more about good design.

+1
source

I think you missed the picture. Performance is everyday for your users; they do not care about maintainability. You are ethnocentric, only your personal concerns worry you, not the people who pay for the system. This is not all about your convenience.

Perhaps you should sit down at the users and see how they use your system for a day or two. Then you should sit down at a computer that has the same power as the ones they use (and not a dev machine), and spend the whole week doing nothing but use your system all day. Then you can understand their point of view.

0
source

Source: https://habr.com/ru/post/1306911/


All Articles