在休眠状态下从表中批量获取

发布于 2024-11-15 02:49:59 字数 168 浏览 6 评论 0原文

我有一个表,我正在从该表中获取大约 250,000 条记录,这需要大约 25 分钟,有没有办法减少获取时间。我正在使用如下代码:-

query.setParameterList("studentId", StudentIdList);

有没有办法优化它?

I have a table and from that I am fetching records somewhere around 250,000 records and this is taking some 25 mins, is there a way to decrease the fetch time. I am using some code like below :-

query.setParameterList("studentId", StudentIdList);

Is there a way to optimize it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

春夜浅 2024-11-22 02:49:59

如果从 SQL 命令行花费的时间不到 10 分钟,而从 Hibernate 花费的时间超过 25 分钟,那么您可能需要查看 批处理。特别是,您可能会从无状态会话中受益(特别是如果您没有做太多工作)。如果您确实没有做太多工作,您也许可以使用 DML 语句(也在该 URL 中)。

If it takes less than 10 minutes from the SQL command line and more than 25 minutes from Hibernate then you may want to look into the information on batching. In particular you might benefit from a stateless session (especially if you're not doing much work). If you're really not doing much work you may be able to use a DML statement (also in that URL).

喵星人汪星人 2024-11-22 02:49:59

有没有办法优化它?

如果您想让速度更快,您可以批量导出并从文件加载。或者只是不使用数据库;)

您可以使用其他方法来存储数据,速度会更快。您可能会通过数据库获得这样的性能,但有时最简单的方法是最好的。

public static void main(String... args) throws IOException {
    File file = new File("students.csv");

    PrintWriter pw = new PrintWriter(file);
    for (int i = 0; i < 250 * 1000; i++)
        pw.println("student " + i + ", last " + i + ", email.address" + i + "@my-school.com," + i);
    pw.close();

    long start = System.nanoTime();
    BufferedReader br = new BufferedReader(new FileReader(file));
    List<Student> students = new ArrayList<Student>();
    for (String line; ((line = br.readLine()) != null);) {
        int pos = line.indexOf(',');
        int pos2 = line.indexOf(',', pos + 1);
        int pos3 = line.indexOf(',', pos2 + 1);
        students.add(new Student(line.substring(0, pos), line.substring(pos + 1, pos2), line.substring(pos2 + 1, pos3), Integer.parseInt(line.substring(pos3 + 1))));
    }
    br.close();
    long time = System.nanoTime() - start;
    System.out.printf("Time to load %,d students was %.1f ms.%n", students.size(), time / 1e6);
}

我使用 http://introcs.cs.princeton.edu/java 中的 Student /32class/Student.java.html

打印

Time to load 250,000 students was 228.7 ms.

Is there a way to optimize it?

If you want to make it faster you can do a bulk export and load from a file. Or just not use the data base ;)

You can go much faster with other approaches to storing data. You might get performance like this with a database, but sometimes the simplest approaches are best.

public static void main(String... args) throws IOException {
    File file = new File("students.csv");

    PrintWriter pw = new PrintWriter(file);
    for (int i = 0; i < 250 * 1000; i++)
        pw.println("student " + i + ", last " + i + ", email.address" + i + "@my-school.com," + i);
    pw.close();

    long start = System.nanoTime();
    BufferedReader br = new BufferedReader(new FileReader(file));
    List<Student> students = new ArrayList<Student>();
    for (String line; ((line = br.readLine()) != null);) {
        int pos = line.indexOf(',');
        int pos2 = line.indexOf(',', pos + 1);
        int pos3 = line.indexOf(',', pos2 + 1);
        students.add(new Student(line.substring(0, pos), line.substring(pos + 1, pos2), line.substring(pos2 + 1, pos3), Integer.parseInt(line.substring(pos3 + 1))));
    }
    br.close();
    long time = System.nanoTime() - start;
    System.out.printf("Time to load %,d students was %.1f ms.%n", students.size(), time / 1e6);
}

I used Student from http://introcs.cs.princeton.edu/java/32class/Student.java.html

prints

Time to load 250,000 students was 228.7 ms.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文