In the current way of developing web applications, you must be familiar with the keyword ORM (Object Relational Mapping). As the era of frameworks for languages ​​is taking the throne strongly, ORM is almost a great choice for developers today.

Viewing: What is Orm

ORM makes it easier for us to manipulate data with the Database, making it easier to code and easier to maintain. . . In this article, I will give a general introduction to ORM, dive into the analysis of advantages and disadvantages and when to apply them in real projects.

To make it easier to follow from this point of the article, I would like to use ORM instead of the phrase Object Relational Mapping

1. What is ORM?

According to Wikipedia:

Object-relational mapping (ORM, O/RM, and O/R mapping tool) in computer science is a programming technique for converting data between incompatible type systems using object-oriented programming languages. This creates, in effect, a “virtual object database” that can be used from within the programming language. There are both free and commercial packages available that perform object-relational mapping, although some programmers opt to construct their own ORM tools.

To put it simply

ORM is a programming technique that helps to map data records in a database management system to the type of objects defined in classes – a common concept used in all modern languages ​​these days. such as: Java, PHP, Ruby. You can apply this technique to any project you like.

According to Martin Fowler there are two patterns that are applicable to different ORM designs.

Some famous ORMs that use Active Record can be named: Eloquent, CachePHP, JOOQ, TOPLINK

Some famous ORMs use Data Mapper such as: Doctrine, Hibernate, SqlAlchemy

2. How does ORM work?

The basic feature of ORM is to encapsulate the database in an object. One part of the object will hold the data, and the other part takes care of how the data is processed and turns it into a relational database.

ORM solves the problem of synchronization between different return data types. On one side is a database, where data is represented as a collection of records. On one side are objects, where data is displayed as objects

READ MORE  Meaning Of Scope Of Work

3. ORM . Pros and Cons

Example of searching a list of records with a condition with pure sql

book_list = new List();sql = “SELECT * FROM library WHERE author = “Linus””;data = query(sql);while (row = data.next()){ book = new Book(); book.setAuthor(row.get(“author”); book_list.add(book);}With an ORM it would be as simple as this

book_list = BookTable.query(author=”Linus”);From the above simple example we can easily see the following advantages of using ORM

Advantages

A lot of things are done “automatically”, related to data processing, in the above example we don’t need to care about using loops to convert data from mysql, Just know the input is the output keyword. return a list of books with author=Linus Using an ORM makes you write code according to the MVC model, making your code easier to modify and maintain. You don’t need to know too much about MYSQL, you still easily accessible to ORM.Model is not tightly tied to the application, in other words, Model is flexible, so you can change or use it wherever ORM allows you to take advantage advantages of OOP like data inheritance without headache

Disadvantage

With each framework there will be a different ORM library, of course you have to take time to learn it. And since they are libraries, they will be “heavy”. Performance is fine for normal queries, but Raw SQL will always do better on large projects. ORMs are considered abstracts of the DB. If you don’t care what actually happens when using an ORM. It can be a trap with N + 1 query.4 ORM performance evaluation

As in the cons, I said that ORM will have slower performance than using raw sql. Here is a statistic with Laravel’s Eloquent ORM and raw mysql.Environment:

CPU: Quad core Intel Xeon E31220Network: BroaNetXtreme BCM5722 Gigabit EthePCI ExpressMemory: 8.0 GBHDD: 2000.4 GBOperating System: CentOS

4.1 Insert

Here the testers inserted different number of posts from 1000 to 10000 for both techniques in each loop.

Eloquent ORM

Number of PostsNumber of PostsHit 1 (ms)Hit 2(ms)Three 3 (ms)Average (ms)Difference (ms) 1 1000 665.25 619.5 585.6 623.5 39.97 2 2000 1150 1140 1110 1133.3 20.81 3 3000 1490 1490 1420 1466.7 40.41 4 4000 1770 1790 1670 1743.3 64.29 5 5000 2080 2090 2220 2130.0 78.10 6 6000 2540 2510 2560 2536.7 25, 16 7 7000 2930 3010 3080 3006.7 75.05 8 8000 3360 3520 3380 3420.0 87.17 9 9000 3800 3900 3880 3860.0 52.91 10 10000 4270 4360 4390 4340.0 62.44

READ MORE  Vinhomes Central Park Recruitment, Vinhomes Central Park Recruitment

Raw SQL

Number of PostsHit 1 (ms)Hit 2(ms)Three 3 (ms)Average (ms)Difference (ms) 1 1000 195.81 189.62 180.1 188.5 7.91 2 2000 322, 78 335.64 307.01 321.8 14.33 3 3000 413.4 437.62 458.24 436.4 22.44 4 4000 598.87 567.57 559.01 575.2 20.98 5 5000 725 ,5 740.94 780.15 748.9 28.17 6 6000 867.93 869.12 891.27 876.1 13.14 7 7000 1000 996.14 949.05 981.7 28.36 8 8000 1150 1120 1130 1133.3 15.27 9 9000 1190 1270 1220 1226.7 40.41 10 10000 1430 1380 1450 1420, 0 36.05

From the table above, we have the following graph

4.2 Update

Here the testers updated different number of posts from 1000 to 10000 for both techniques in each loop.

See also: What is the Destiny – How to calculate your own destiny according to the ages

Eloquent ORM

Number of PostsNumber of PostsHit 1 (ms)Hit 2(ms)Hit 3 (ms)Average(ms)Difference(ms) 1 1000 305.59 316.06 297.93 306.5 9.10 2 2000 522, 06 565.12 543.43 543.5 21.53 3 3000 778.4 772.33 790.52 780.4 9.26 4 4000 1040 1010 1080 1043.3 35.11 5 5000 1290 1240 1260 1263.3 25 ,16 6 6000 2540 2510 2560 2536.7 25.16 7 7000 2930 3010 3080 3006.7 75.05 8 8000 3360 3520 3380 3420.0 87.17 9 9000 3800 3900 3880 3860.0 52.91 10 10000 4270 4360 4390 4340.0 62.44

Raw SQL

Number of PostsHit 1 (ms)Hit 2(ms)Three 3 (ms)Average (ms)Difference (ms) 1 1000 195.81 189.62 180.1 188.5 7.91 2 2000 322, 78 335.64 307.01 321.8 14.33 3 3000 413.4 437.62 458.24 436.4 22.44 4 4000 598.87 567.57 559.01 575.2 20.98 5 5000 725 ,5 740.94 780.15 748.9 28.17 6 6000 867.93 869.12 891.27 876.1 13.14 7 7000 1000 996.14 949.05 981.7 28.36 8 8000 1150 1120 1130 1133.3 15.27 9 9000 1190 1270 1220 1226.7 40.41 10 10000 1430 1380 1450 1420.0 36.05

From the table above, we have the following graph

READ MORE  "King" Of Hardstyle Music

4.3 Select

Here the solutions are divided into 3 turns

4.4 Evaluation and analysis

From the above figures, it is easy to see that the performance of raw sql is clearly better than that of ORM in all select, insert, and update actions.

The reason here is: ORM needs time to create Model instances, create properties for the model, and transform the model object into relational data before communicating with the database. All that’s behind it is that Laravel converted each Eloquent ORM code into the proper SQL statement and then executed it on the database tier and returned the results back to the application tier. So the time to execute a task always takes longer than using raw sql.

But wait, don’t rush to look at the numbers above and say that ORM is a waste.

In practice, no one selects several thousand records, or does the creation of several thousand records at a time.

That is, when the number of records is less, the performance difference is significantly reduced. Then add in the use of ORM’s caching technique that makes your application acceptable compared to the benefits of application development speed (development time), eliminating the use of repetitive code. over and over, better security into many other great things.

5. Summary

In short, going through the whole article, I have described how ORM works, outlined the advantages and disadvantages of ORM. Whether to use ORM or not is still the subject of unending debate.

Regarding my own experience when working on projects, I often mix ORM and Raw SQL effectively, with normal queries, ORM is my choice. For situations that have to work with many records, raw SQL seems more reasonable. But when using raw sql you have to be really careful about the sql injection problem.

See also: What is Co Form E – C/o Form E and China Rules of Origin

Thank you for following the above article, in the article there are references from a number of sources.

And pick up miscellaneous things without remembering

. See you in the next posts