1. Overview
1.概述
Often, we find it difficult to decide whether a calculation should be performed in the database (RDBMS) or application code to get good performance along with convenience at the same time.
通常,我们发现很难决定一个计算应该在数据库(RDBMS)还是在应用程序代码中进行,以便同时获得良好的性能和便利。
In this article, we’ll explore the advantages and disadvantages of performing calculations in the database and application code.
在这篇文章中,我们将探讨在数据库和应用程序代码中进行计算的优势和劣势。
We’ll consider a few factors that can influence this decision, and we’ll discuss which layer (database or application) is better suited to handle them.
我们将考虑一些可能影响这一决定的因素,我们将讨论哪一层(数据库或应用程序)更适合处理这些因素。
2. Calculation in the Database
2.数据库中的计算
2.1. Data Selection and Aggregation
2.1.数据选择和汇总
Relational databases are highly optimized for the handling, selection, and aggregation of data. We can easily group, order, filter, and aggregate data using SQL.
关系型数据库对数据的处理、选择和聚合进行了高度优化。我们可以使用SQL轻松地对数据进行分组、排序、过滤和汇总。
For example, we can easily select and deselect datasets from multiple tables using LEFT and RIGHT JOIN.
例如,我们可以使用LEFT和RIGHT JOIN轻松地从多个表中选择和取消选择数据集。
Similarly, aggregate functions like MIN, MAX, SUM, and AVG are quite handy and faster than a Java implementation.
同样,像MIN、MAX、SUM和AVG这样的集合函数也相当方便,而且比Java实现的速度快。
Also, we can fine-tune the performance of the disk IO by leveraging indexes while aggregating data.
此外,我们还可以通过在聚合数据时利用索引来对磁盘IO的性能进行微调。
2.2. Volume of Data
2.2.数据量
All popular RDBMS provide unmatched performance in handling a large volume of data from tables for performing a calculation.
所有流行的RDBMS在处理大量的表格数据以进行计算时都具有无可比拟的性能。
However, we’ll require a lot of resources like memory and CPU processing to process a similar volume of data in the application as compared to a database.
然而,与数据库相比,我们需要大量的资源,如内存和CPU处理来处理应用程序中类似的数据量。
Also, to save bandwidth, it’s advised to perform data-centric calculations in the database, thereby avoiding the transfer of large volumes of data over the network.
另外,为了节省带宽,建议在数据库中进行以数据为中心的计算,从而避免通过网络传输大量数据。
3. Calculation in the Application
3.应用中的计算
3.1. Complexity
3.1.复杂性
Unlike the database, higher-level languages like Java are well equipped in dealing with complex calculations.
与数据库不同,像Java这样的高级语言在处理复杂的计算方面有很好的能力。
For example, we can leverage asynchronous programming, parallel execution, and multithreading in Java to solve a complex problem.
例如,我们可以利用Java中的异步编程、并行执行和多线程来解决一个复杂问题。
Similarly, the database provides minimal support for logging and debugging. However, today’s higher-level languages have excellent support for such critical features, which are often handy in implementing a complex calculation.
同样,数据库对日志和调试的支持也是最小的。然而,今天的高级语言对这种关键功能有很好的支持,这在实现复杂的计算时往往很方便。
For instance, we can easily add logging in a Java application by using SLF4J and use popular IDEs like Eclipse and IntelliJ IDEA for debugging. Therefore, performing a calculation in the application is a convenient option for a developer as compared to the database.
例如,我们可以通过使用SLF4J轻松地在Java应用程序中添加日志,并使用流行的IDE,如Eclipse和IntelliJ IDEA进行调试。因此,与数据库相比,在应用程序中进行计算对开发人员来说是一个方便的选择。
Likewise, another argument is that we can easily unit test our calculations in the application code, which is fairly complex to perform in the database.
同样地,另一个论点是,我们可以很容易地在应用程序代码中对我们的计算进行单元测试,而在数据库中进行计算是相当复杂的。
Unit testing proves quite handy in keeping a check on the changes in the implementations. So, when performing a calculation in the Java application, we can use JUnit to add unit tests.
单元测试被证明在保持检查实现中的变化方面相当方便。因此,在Java应用程序中进行计算时,我们可以使用JUnit来添加单元测试。
3.2. Advanced Data Analysis and Transformation
3.2.高级数据分析和转换
The database provides limited support for advanced data analysis and transformation. However, it’s simple to perform complex computations using the application code.
该数据库对高级数据分析和转换提供了有限的支持。然而,使用应用程序代码进行复杂的计算很简单。
For instance, a variety of libraries like Deeplearning4J, Weka, and TensorFlow are available for advanced statistics and machine learning support.
例如,各种库,如Deeplearning4J、Weka和TensorFlow都可用于高级统计和机器学习支持。
Another common use-case is that we can easily objectify the data using ORM technologies like Hibernate, use APIs like Java Streams to process it, and produce results in various formats through XML or JSON parsing libraries.
另一个常见的用例是,我们可以使用Hibernate等ORM技术轻松地对象化数据,使用Java Streams等API来处理它,并通过XML或JSON解析库产生不同格式的结果。
3.3. Scalability
3.3.可扩展性
Achieving database scalability can be a daunting task as RDBMS can only scale up. However, the application code offers a more scalable solution.
实现数据库的可扩展性可能是一项艰巨的任务,因为RDBMS只能向上扩展。然而,应用程序代码提供了一个更可扩展的解决方案。
We can easily scale out the app-servers and handle a large number of requests using a load balancer.
我们可以轻松地扩大应用服务器的规模,并使用负载平衡器处理大量的请求。
4. Database vs. Application
4.数据库vs.应用程序
Now that we’ve seen the advantages of performing a calculation based on certain factors at each of the layers, let’s summarize their differences:
现在我们已经看到了在每个层的某些因素基础上进行计算的优势,让我们总结一下它们的区别。
- The database is a preferred choice for data selection, aggregation, and handling large volumes
- However, performing a calculation in the application code looks a better candidate when considering factors like complexity, advanced-data transformation, third-party integrations, and scalability
- Also, higher-level languages provide extra benefits like logging, debugging, error handling, and unit testing capabilities
It’s always a good idea to mix and leverage the best of both layers to solve a complex problem.
混合并利用两层的优点来解决一个复杂的问题,总是一个好主意。
In other words, use the database for selection and aggregation of data, then transmit useful lean data to the application and perform complex operations over it using an efficient higher-level language.
换句话说,使用数据库进行数据的选择和汇总,然后将有用的精简数据传输给应用程序,并使用高效的高级语言对其进行复杂操作。
5. Conclusion
5.总结
In this article, we explored the pros and cons of performing calculations in the application and database.
在这篇文章中,我们探讨了在应用程序和数据库中进行计算的利与弊。
First, we discussed the advantages of performing calculations in both the database and application layers. Then, we summarized our conclusions about performing a calculation based on all the factors we discussed.
首先,我们讨论了在数据库和应用层进行计算的优势。然后,我们总结了基于我们讨论的所有因素进行计算的结论。