Spring Boot JPA - 数据库查询性能

发布于 2025-01-09 09:56:28 字数 1305 浏览 0 评论 0原文

@Language("SQL")
@Query(
    """
    SELECT DISTINCT o.*, ooc.*
    FROM WHITE_TABLE o
        LEFT JOIN BLUE_TABLE oow ON o.BLUE_ID= oow.ID
        LEFT JOIN RED_TABLE ooc on o.ID = ooc.RED_ID
        LEFT JOIN GREEN_TABLE ce on ce.GREEN_ID= ooc.RED_ID
        LEFT JOIN ORANGE TABLE d on d.ORANGE_GREEN_ID= ce.ID
     WHERE d.STATE= 'A'
        AND o.NO_MATTER IN :#{#param.setIds1}
        AND o.NO_MATTER IN :#{#param.setIds2}
        AND ooc.NO_MATTER IN :#{#param.setIds3}
        AND oow.NO_MATTER IN :#{#param.setIds4}
        AND (
                (
                    o.TIME is not null AND 
                    trunc(cast(o.TIME as DATE)) BETWEEN 
                    :#{#param.time_value} AND :#{#param.time_value2}
                ) OR 
                (
                    o.EXPIRE_TIME is null AND 
                    :#{#param.time} BETWEEN 
                    trunc(cast(d.time_value as Date)) AND trunc(cast(d.time_value2 as Date))
                )
        )
        """, nativeQuery = true
)

我有一个包含多个内部联接的查询,并且存在性能问题。即使我在邮递员中获取 2 个对象,该查询的加载时间也是 2/3 秒。您知道优化查询的可能性有哪些?在这种情况下,我不可能使用“非本机查询”。绿色和橙色表在代码中没有实体表示。

据报道 TRUNC CAST 需要花费大量时间并且效率低下,但是还能怎么写呢?

How can I speed up the query?反射 ?意见?你有什么例子吗?

我补充一下,数据库是Oracle,并且有索引。

@Language("SQL")
@Query(
    """
    SELECT DISTINCT o.*, ooc.*
    FROM WHITE_TABLE o
        LEFT JOIN BLUE_TABLE oow ON o.BLUE_ID= oow.ID
        LEFT JOIN RED_TABLE ooc on o.ID = ooc.RED_ID
        LEFT JOIN GREEN_TABLE ce on ce.GREEN_ID= ooc.RED_ID
        LEFT JOIN ORANGE TABLE d on d.ORANGE_GREEN_ID= ce.ID
     WHERE d.STATE= 'A'
        AND o.NO_MATTER IN :#{#param.setIds1}
        AND o.NO_MATTER IN :#{#param.setIds2}
        AND ooc.NO_MATTER IN :#{#param.setIds3}
        AND oow.NO_MATTER IN :#{#param.setIds4}
        AND (
                (
                    o.TIME is not null AND 
                    trunc(cast(o.TIME as DATE)) BETWEEN 
                    :#{#param.time_value} AND :#{#param.time_value2}
                ) OR 
                (
                    o.EXPIRE_TIME is null AND 
                    :#{#param.time} BETWEEN 
                    trunc(cast(d.time_value as Date)) AND trunc(cast(d.time_value2 as Date))
                )
        )
        """, nativeQuery = true
)

I have a query with several inner joins and I have a performance problem. The query even if I fetch 2 objects in postman the loading time is 2/3 seconds. What possibilities do you know to optimize the query ? In this case I have no possibility to use "NON native query". GREEN and ORANGE tables do not have their entity representation in the code.

Reportedly TRUNC CAST takes a lot of time and is inefficient but how else to write it ?

How can I speed up the query? Reflection ? Views? Do you have any examples?

I will add that the database is Oracle and there are indexes.

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

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

发布评论

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

评论(1

怪异←思 2025-01-16 09:56:28

如果您有这样的动态查询,我建议使用 Criteria Builder API。如果您的数据库版本发生变化怎么办?您将不得不返回并在您的查询中进行一些更改。这将导致浪费时间或精神崩溃,并且在某些情况下需要加快您的查询速度。

https://ubiq.co/database-blog/how -to-speed-up-sql-queries/

我建议使用视图可以提高性能。

<一href="https://stackoverflow.com/questions/439056/is-a-view-faster-than-a-simple-query#:%7E:text=Yes%2C%20views%20can%20have%20a,那个%20观看次数%20可以%20提高%20性能.">https://stackoverflow.com/questions/439056/is-a-view-faster-than-a-simple-query#:~:text=Yes%2C%20views%20can%20have%20a,that% 20 观看次数%20 可以%20 提高%20 性能。

If you have dynamic queries like this, I recommend using the Criteria Builder API. What if your database version changes? You will have to return and make some changes in your query.and this will cause a waste of time or a nervous breakdown and there is some cases to speed up for your queries .

https://ubiq.co/database-blog/how-to-speed-up-sql-queries/

and I suggested using Views can improve performance.

https://stackoverflow.com/questions/439056/is-a-view-faster-than-a-simple-query#:~:text=Yes%2C%20views%20can%20have%20a,that%20Views%20can%20improve%20performance.

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