简体   繁体   English

性能:Java与数据库

[英]Performance: Java vs. Database

When programming there are decisions to make all the time, like: 进行编程时,需要始终做出决策,例如:

  • Should I generate my Menu/Buttons/Navigation dynamically from a db entry or static via code 我应该从数据库条目动态生成菜单/按钮/导航还是通过代码静态生成菜单/按钮/导航
  • Should I count entries of a list by Java or firing a DB query ..or maybe you find more of those DB vs Javacode questions. 我应该用Java来计数列表的条目还是触发数据库查询..或者您可能会发现更多关于DB与Javacode的问题。

(I hope that question is not too common:) (我希望这个问题不太常见:)

What should your DB do and what excercises are better done by your Java-code, regarding performance issues in web applications. 关于Web应用程序中的性能问题,您的数据库应该做什么,用Java代码可以更好地完成哪些工作。

Maybe differing between small and huge projects is reasonable 大型项目之间的差异也许是合理的

Extending Ignacio's answer a bit: the DB typically has a big latency (unless it is physically on the same machine), so you want to hit it as rarely as possible. 稍微扩展一下Ignacio的答案:数据库通常具有较大的延迟(除非它物理上位于同一台计算机上),因此您希望尽可能少地使用它。 If you use an ORM like Hibernate, you get caching and lazy loading for free; 如果使用像Hibernate这样的ORM,则可以免费获得缓存和延迟加载。 otherwise you need to take care of these yourself. 否则,您需要自己照顾这些。

Thus it is OK to fetch GUI element data from the DB once - then cache and reuse it locally. 因此,可以一次从数据库中获取GUI元素数据,然后进行缓存并在本地重用。 And it is better to count entries of a list locally if you already have all the elements. 如果已经拥有所有元素,则最好在本地对列表的条目进行计数。 If you need a query anyway, you can try combining the fetches into one. 如果仍然需要查询,则可以尝试将提取的内容合并为一个。 However, if you have a huge list and you want to select relatively few elements from it, it may be preferred to let the DB do the work and return only the selected entries, as opposed to cramming a large amount of data through a slow network connection. 但是,如果您有一个庞大的列表,并且想要从中选择相对较少的元素,则最好让数据库执行工作并仅返回所选的条目,这与通过慢速网络填充大量数据相反连接。

Database is slow. 数据库速度慢。 Java code is relatively very fast. Java代码相对非常快。 Cache everything pulled from the database in memory if possible, possibly using something like memcache (if relevant; I don't do much Java web code). 如果可能,将从数据库中提取的所有内容都缓存在内存中,可能使用诸如memcache之类的东西(如果相关;我不会做太多Java Web代码)。

Use the database for changeable data. 使用数据库获取可更改的数据。 If the application features being able to change the menu/buttons/navigation on the fly, put them in the database, otherwise, do not. 如果应用程序具有能够即时更改菜单/按钮/导航的功能,则将其放入数据库中,否则请不要。 Content management systems often do. 内容管理系统经常这样做。

Historically, accessing a database is slow. 从历史上看,访问数据库很慢。 This is specially true for databases that are accessed over the network (3-tier architecture). 对于通过网络(3层体系结构)访问的数据库,尤其如此。 That's why you should avoid accessing the database, limiting the number of database calls, and limiting the number of connections to the database (possibly using a connection pool). 这就是为什么应该避免访问数据库,限制数据库调用的次数以及限制与数据库的连接数(可能使用连接池)的原因。 Typical examples are Oracle, IBM DB2, MS SQL Server. 典型示例是Oracle,IBM DB2,MS SQL Server。 Newer databases are MySQL and PostgreSQL. 较新的数据库是MySQL和PostgreSQL。

There are some newer databases that can run in-memory or embedded in the application. 有一些较新的数据库可以在内存中运行或嵌入到应用程序中。 That's much faster. 那要快得多。 Some typical Java databases in this area are HSQLDB and newer the H2 database . 该领域中的一些典型Java数据库是HSQLDB和更新的H2数据库 They also support client/server operations, but they are not as mature as the databases mentioned above. 它们还支持客户端/服务器操作,但是还不如上面提到的数据库成熟。

But even in-memory and embedded databases are not as fast as using the collection API. 但是,即使是内存数据库和嵌入式数据库也没有使用集合API快。

Your database should act as a container that holds data that needs to be persisted for your application. 您的数据库应充当一个容器,用于保存需要为应用程序保留的数据。 You need to make the decision depending on the type of data that you are dealing with: 您需要根据要处理的数据类型做出决定:

For application configurations (menu items, title bar names, button names, etc) - consider using some sort of properties file. 对于应用程序配置(菜单项,标题栏名称,按钮名称等),请考虑使用某种属性文件。 If your application has many users and the configuration will be different for each or an excessive amount of properties, consider using a database to persist. 如果您的应用程序有许多用户,并且每个属性的配置都不相同或属性过多,请考虑使用数据库进行持久化。

For model data (Person, Address etc.) - consider using a database as this is important information for your application. 对于模型数据(人员,地址等)-考虑使用数据库,因为这对于您的应用程序是重要的信息。 Also, you'd benefit from a database here so that you can do reporting and analysis outside of your application. 另外,您将从这里的数据库中受益,因此可以在应用程序外部进行报告和分析。

In general, you should be doing all your computations on the data in the java code. 通常,您应该对Java代码中的数据进行所有计算。 This is the concept of using your application layer for business logic and database layer simply for persistence. 这是将应用程序层用于业务逻辑,而将数据库层仅用于持久性的概念。

There is one exception to using java to perform all actions on your data - most databases are very efficient at sorting data and should be used in your queries to sort your result set if you require it. 使用Java对数据执行所有操作有一个例外-大多数数据库对数据进行排序非常有效,如果需要,应在查询中使用它对结果集进行排序。 Depending on the data set and table implementation, sorting your data on the DB side and putting it into a List in your java application may more efficient than getting your data and then sorting in java. 根据数据集和表实现的不同,在数据库端对数据进行排序并将其放入Java应用程序的列表中可能比获取数据然后在Java中进行排序更有效。

Lastly, if your require frequent use of data, then consider caching it in java rather than querying from database. 最后,如果您需要频繁使用数据,请考虑将其缓存在Java中,而不是从数据库中查询。 As some of the other posts mentioned, creating a db connection, executing a query, parsing results is more expensive than simply accessing in your heap. 正如其他一些文章提到的那样,创建数据库连接,执行查询,解析结果比仅在堆中访问要昂贵。

Create a model. 创建一个模型。 (Bubbles and Line) and identify the entities that is your core data, create classes using that. (气泡和线)并标识作为您的核心数据的实体,然后使用该实体创建类。 This is what you put in your database. 这就是您放入数据库中的内容。 (Or, you can do it the other way, create a relational model from your entity model. But that may lead to a crappy model in your program, more often than not.) (或者,您也可以用另一种方法,从您的实体模型创建一个关系模型。但这通常会导致程序中糟糕的模型。)

Initially, dont bother with caching etc, but I admit that its good to have database performance in mind too when designing the model... 最初,不必理会缓存等问题,但是我承认在设计模型时也要考虑数据库性能,这是一件好事……

Make a clear model that supports what you are trying to do. 建立一个清晰的模型以支持您要尝试的操作。

Regarding "configuration" in the databas, consider this: If your configuration can't be changed without also changing code, and redeploying, then its no point. 关于数据库中的“配置”,请考虑以下问题:如果在不更改代码和重新部署的情况下无法更改您的配置,则毫无意义。 Really. 真。 It will just be harder to maintain. 它将很难维护。

Also read this: 另请阅读:
http://thedailywtf.com/Articles/Soft_Coding.aspx http://thedailywtf.com/Articles/Soft_Coding.aspx

And this: 和这个:

http://thedailywtf.com/Articles/The-Mythical-Business-Layer.aspx http://thedailywtf.com/Articles/The-Mythical-Business-Layer.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM