简体   繁体   English

用Java排序?

[英]Sorting in java?

I have an application use sorting Criteria. 我有一个应用程序使用排序标准。 But the issue is that before i use sorting through database (5000 of record sort). 但是问题是,在我使用数据库排序之前(记录排序为5000)。 Now i have 20,000 records for sorting data. 现在我有20,000条记录用于数据排序。 Someone said to me to use Java for sorting. 有人对我说要使用Java进行排序。 It is supposed to be better for your application. 它应该对您的应用程序更好。

I have two questions: 我有两个问题:

  1. Which one better for performance Use Java and database sort? 使用Java和数据库排序可以提高性能?
  2. Suppose i use Java sorting it independent of db. 假设我使用Java对其进行独立于db的排序。

Can Java sorting handle 20,000 records? Java排序可以处理20,000条记录吗?

If you can, sort your data in your database. 如果可以,请对数据库中的数据进行排序。 It is definitely faster that in memory. 内存中的速度肯定更快。 Sorting depends on the algorithm and not on any specific technology. 排序取决于算法,而不取决于任何特定技术。 In result, java may sort any number of records. 结果,java可以对任何数量的记录进行排序。 What you are interested at however is how to sort your data most efficiently, which in your case is in the database. 但是,您感兴趣的是如何最有效地对数据进行排序,在您的情况下,这是在数据库中。

If you need to learn how to sort date using java in memory you may take read the following: http://docs.oracle.com/javase/tutorial/collections/algorithms/index.html#sorting 如果您需要学习如何使用内存中的Java对日期进行排序,则可以阅读以下内容: http : //docs.oracle.com/javase/tutorial/collections/algorithms/index.html#sorting

根据需要将数据从数据库中取出总是更好的方法(考虑在排序字段上使用索引以提高性能)

The following general rule applies when manipulating data in databases: 在处理数据库中的数据时,适用以下一般规则:

  1. Can it be done with SQL? 可以用SQL完成吗? Do it with SQL . SQL做到这一点。
  2. Can it be done with PLSQL? 可以用PLSQL完成吗? Do it with PLSQL . PLSQL来做
  3. Do it with the programming language of your choice, it will be slow anyways. 使用您选择的编程语言来执行此操作,无论如何都会很慢。

Why is it bad to do it in Java? 为什么用Java做它不好?

To be able to do something like sorting within Java you obviously first have to get the data from the database into your program space and afterwards you need to write it back. 为了能够在Java中进行排序,您显然首先必须将数据库中的数据放入程序空间,然后再将其写回。 This is an obvious overhead that is way too often ignored, and especially becomes difficult, if you work with huge amounts of data. 这是一个显而易见的开销,经常会忽略,特别是在处理大量数据时变得非常困难。 Just think about how long it takes to pull out 2 GB from a database - worst case - over a network connection and then even send the result back. 只需考虑一下通过网络连接从数据库中提取2 GB的数据需要花费多长时间(最坏的情况),然后将结果发送回去。

If you go the SQL/PLSQL way, all data stays in the database and never needs to be forwarded to your program. 如果采用SQL / PLSQL方式,则所有数据都保留在数据库中,并且永远不需要转发到程序。 This not only removes the overhead of transfer, but as well allows the database to handle this in the most optimized form - another overhead that is often ignored. 这不仅消除了传输的开销,而且还允许数据库以最优化的形式处理该开销-另一个通常被忽略的开销。 If you pull out data, the DB doesn't know what you are going to do with it, so it just has to hand over everything to your code. 如果您提取数据,则数据库不知道您将如何处理它,因此它只需要将所有内容交给代码即可。 If you do something like a sort on one table, the DB iE knows that sub-tables and links are not affected anyways, so there is no need to even read that data. 如果在一个表上执行某种排序操作,则DB iE知道子表和链接始终不会受到影响,因此甚至不需要读取该数据。 Yet again a noticeable performance gain. 再一次明显的性能提升。
Just think about what might be faster: your code that you wrote in 5 minutes, or the DB code that hundreds of people wrote in over 10 years, trying to squeeze out even the last bit of performance possible? 试想一下,哪种方法会更快:您在5分钟内编写的代码,还是数百人在10年内编写的DB代码,试图尽可能地降低性能?

In addition if you read data from a database, it will be transfered to you in an insecure way. 另外,如果您从数据库中读取数据,数据将以不安全的方式传输给您。 So if someone does a man-in-the-middle attack while you look through a user's passwords, that man in the middle now knows those passwords as well. 因此,如果有人在您查看用户密码时进行中间人攻击,那么中间的那个人现在也知道这些密码。 Or the other way round: if your program has a bug that can be exploited to gain access to the user's critical data, this is a security issue. 或反过来:如果您的程序存在一个漏洞,可以利用该漏洞来访问用户的关键数据,则这是一个安全问题。 If your code never has that ability in the first place, because all that data is handled internally by the database, then there is nothing that can even be a security issue in your code. 如果您的代码最初没有这种能力,因为所有这些数据都是由数据库内部处理的,那么您的代码中甚至没有安全问题。

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

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