Spring Data Exists查询最佳方法编写示例

 更新时间:2022年08月01日 17:10:27   作者:DebugUsery  
这篇文章主要为大家介绍了Spring Data Exists查询最佳方法编写示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

简介

在这篇文章中,我将向你展示编写Spring Data Exists查询的最佳方法,从SQL的角度来看,它是高效的。

在做咨询的时候,我遇到了几个常用的选项,而开发者却不知道其实还有更好的选择。

领域模型

让我们假设我们有以下Post 实体。

slug 属性是一个业务键,意味着它有一个唯一的约束,为此,我们可以用下面的注解来注解它 @NaturalIdHibernate注解。

@Entity
@Entity
@Table(
    name = "post",
    uniqueConstraints = @UniqueConstraint(
        name = "UK_POST_SLUG",
        columnNames = "slug"
    )
)
public class Post {
    @Id
    private Long id;
    private String title;
    @NaturalId
    private String slug;
    public Long getId() {
        return id;
    }
    public Post setId(Long id) {
        this.id = id;
        return this;
    }
    public String getTitle() {
        return title;
    }
    public Post setTitle(String title) {
        this.title = title;
        return this;
    }
    public Post setSlug(String slug) {
        this.slug = slug;
        return this;
    }
}

如何不使用Spring Data来写Exists查询?

首先,让我们从各种方法开始,这些方法虽然很流行,但你最好避免使用。

用findBy查询模拟存在

Spring Data提供了一种从方法名派生查询的方法,所以你可以写一个findBy 查询来模拟存在,就像这样。

@Repository
public interface PostRepository 
        extends JpaRepository<Post, Long> {
    Optional<Post> findBySlug(String slug);   
}

由于findBySlug 方法是用来获取Post 实体的,我见过这样的情况:这个方法被用来进行平等检查,就像下面的例子。

assertTrue(
    postRepository.findBySlug(slug).isPresent()
);

这种方法的问题在于,实体的获取实际上只是为了检查是否有一个与所提供的过滤条件相关的记录。

SELECT 
    p.id AS id1_0_,
    p.slug AS slug2_0_,
    p.title AS title3_0_
FROM 
    post p
WHERE 
    p.slug = 'high-performance-java-persistence'

使用fidnBy 查询来获取实体以检查其存在性是一种资源浪费,因为如果你在slug 属性上有一个索引的话,你不仅不能使用覆盖查询,而且你必须通过网络将实体结果集发送到JDBC驱动程序,只是默默地将其丢弃。

使用实例查询来检查存在性

另一个非常流行的,但效率低下的检查存在性的方法是使用Query By Example功能。

assertTrue(
    postRepository.exists(
        Example.of(
            new Post().setSlug(slug),
            ExampleMatcher.matching()
                .withIgnorePaths(Post_.ID)
                .withMatcher(Post_.SLUG, exact())
        )
    )
);

Query By Example功能建立了一个Post 实体,在匹配所提供的ExampleMatcher 规范给出的属性时,该实体将被用作参考。

当执行上述Query By Example方法时,Spring Data会生成与之前findBy 方法所生成的相同的SQL查询。

SELECT 
    p.id AS id1_0_,
    p.slug AS slug2_0_,
    p.title AS title3_0_
FROM 
    post p
WHERE 
    p.slug = 'high-performance-java-persistence'

虽然Query By Example功能对于获取实体可能很有用,但是将其与Spring Data JPA的exists 通用方法Repository ,效率并不高。

如何使用Spring Data编写Exists查询

有更好的方法来编写Spring Data Exists查询。

用existsBy查询方法检查存在性

Spring Data提供了一个existsBy 查询方法,我们可以在PostRepository ,定义如下。

@Repository
public interface PostRepository 
        extends JpaRepository<Post, Long> {
    boolean existsBySlug(String slug);
}

当在PostgreSQL或MySQL上调用existsBySlug 方法时。

assertTrue(
    postRepository.existsBySlug(slug)
);

Spring Data会生成以下SQL查询。

SELECT 
    p.id AS col_0_0_
FROM 
    post p
WHERE 
    p.slug = 'high-performance-java-persistence'
LIMIT 1

这个查询的PostgreSQL执行计划看起来如下。

Limit  
    (cost=0.28..8.29 rows=1 width=8) 
    (actual time=0.021..0.021 rows=1 loops=1)
  ->  Index Scan using uk_post_slug on post p  
      (cost=0.28..8.29 rows=1 width=8) 
      (actual time=0.020..0.020 rows=1 loops=1)
        Index Cond: ((slug)::text = 'high-performance-java-persistence'::text)
Planning Time: 0.088 ms
Execution Time: 0.033 ms

还有,MySQL的,像这样。

-> Limit: 1 row(s)  
   (cost=0.00 rows=1) 
   (actual time=0.001..0.001 rows=1 loops=1)
    -> Rows fetched before execution  
       (cost=0.00 rows=1) 
       (actual time=0.000..0.000 rows=1 loops=1)

所以,这个查询非常快,而且额外的LIMIT 操作并不影响性能,因为它反正是在一个记录的结果集上完成。

用COUNT SQL查询来检查存在性

模拟存在性的另一个选择是使用COUNT查询。

@Repository
public interface PostRepository 
        extends JpaRepository<Post, Long> {
    @Query(value = """
        select count(p.id) = 1 
        from Post p
        where p.slug = :slug
        """
    )
    boolean existsBySlugWithCount(@Param("slug") String slug);
}

COUNT 查询在这种特殊情况下可以正常工作,因为我们正在匹配一个UNIQUE列值。

然而,一般来说,对于返回有多条记录的结果集的查询,你应该倾向于使用EXISTS ,而不是COUNT ,正如Lukas Eder在这篇文章中所解释的那样。

在PostgreSQL和MySQL上调用existsBySlugWithCount 方法时。

assertTrue(
    postRepository.existsBySlugWithCount(slug)
);

Spring Data会执行以下SQL查询。

SELECT 
    count(p.id) > 0 AS col_0_0_
FROM 
    post p
WHERE 
    p.slug = 'high-performance-java-persistence'

而且,这个查询的PostgreSQL执行计划看起来如下。

Aggregate  
  (cost=8.29..8.31 rows=1 width=1) 
  (actual time=0.023..0.024 rows=1 loops=1)
  ->  Index Scan using uk_post_slug on post p  
      (cost=0.28..8.29 rows=1 width=8) 
      (actual time=0.019..0.020 rows=1 loops=1)
        Index Cond: ((slug)::text = 'high-performance-java-persistence'::text)
Planning Time: 0.091 ms
Execution Time: 0.044 ms

而在MySQL上。

-> Aggregate: count('1')  
   (actual time=0.002..0.002 rows=1 loops=1)
    -> Rows fetched before execution  
       (cost=0.00 rows=1) 
       (actual time=0.000..0.000 rows=1 loops=1)

尽管COUNT操作有一个额外的Aggregate步骤,但由于只有一条记录需要计算,所以这个步骤非常快。

用CASE WHEN EXISTS SQL查询来检查存在性

最后一个模拟存在的选项是使用CASE WHEN EXISTS本地SQL查询。

@Repository
public interface PostRepository 
        extends JpaRepository<Post, Long> {
    @Query(value = """
        SELECT 
            CASE WHEN EXISTS (
                SELECT 1 
                FROM post 
                WHERE slug = :slug
            ) 
            THEN 'true' 
            ELSE 'false'
            END
        """,
        nativeQuery = true
    )
    boolean existsBySlugWithCase(@Param("slug") String slug);
}

而且,我们可以像这样调用existsBySlugWithCase 方法。

assertTrue(
    postRepository.existsBySlugWithCase(slug)
);

这个查询的PostgreSQL执行计划看起来如下。

Result  
  (cost=8.29..8.29 rows=1 width=1) 
  (actual time=0.021..0.022 rows=1 loops=1)
  InitPlan 1 (returns $0)
    ->  Index Only Scan using uk_post_slug on post  
          (cost=0.27..8.29 rows=1 width=0) 
          (actual time=0.020..0.020 rows=1 loops=1)
          Index Cond: (slug = 'high-performance-java-persistence'::text)
          Heap Fetches: 1
Planning Time: 0.097 ms
Execution Time: 0.037 ms

而在MySQL上。

-> Rows fetched before execution  
   (cost=0.00 rows=1) 
   (actual time=0.000..0.000 rows=1 loops=1)
-> Select #2 (subquery in projection; run only once)
    -> Limit: 1 row(s)  
        (cost=0.00 rows=1) 
        (actual time=0.000..0.001 rows=1 loops=1)
        -> Rows fetched before execution  
           (cost=0.00 rows=1) 
           (actual time=0.000..0.000 rows=1 loops=1)

所以,这和之前的LIMITCOUNT 查询一样快。在其他数据库上,你可能要检查一下,看看是否有什么不同。

结论

因此,如果你想用Spring Data检查一条记录的存在,最简单的方法是使用existsBy 查询方法。

而且,如果查询比较复杂,你不能用Spring Data的查询方法来表达,你可以使用COUNT或CASE WHEN EXISTS查询,因为它们同样快速。

以上就是Spring Data Exists查询最佳方法编写示例的详细内容,更多关于Spring Data Exists查询的资料请关注脚本之家其它相关文章!

相关文章

  • dubbo服务链路跟踪方式

    dubbo服务链路跟踪方式

    这篇文章主要介绍了dubbo服务链路跟踪方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-07-07
  • Spring中的事务控制知识总结

    Spring中的事务控制知识总结

    我们讲了转账方法存在着事务问题,当在业务层方法更新转入账户时发现异常,更新收款方账户则会出错.当时是通过自定义事务管理器进行整体事务的处理.其实Spring 提供了业务层的事务处理解决方案,并且 Spring 的事务控制都是基于 AOP 的,需要的朋友可以参考下
    2021-06-06
  • Java连接MySQL数据库并实现数据交互功能

    Java连接MySQL数据库并实现数据交互功能

    在现代应用中,数据库是不可或缺的一部分,Java 作为一种广泛使用的编程语言,提供了丰富的 API 来与各种数据库进行交互,本文将详细介绍如何在 Java 中连接 MySQL 数据库,并实现基本的数据交互功能,需要的朋友可以参考下
    2024-10-10
  • JAVA如何调用Shell脚本

    JAVA如何调用Shell脚本

    本篇文章主要介绍了JAVA如何调用Shell脚本,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-08-08
  • java swing实现QQ账号密码输入框

    java swing实现QQ账号密码输入框

    这篇文章主要为大家详细介绍了Java swing实现QQ账号密码输入框,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-06-06
  • SpringBoot详细讲解视图整合引擎thymeleaf

    SpringBoot详细讲解视图整合引擎thymeleaf

    这篇文章主要分享了Spring Boot整合使用Thymeleaf,Thymeleaf是新一代的Java模板引擎,类似于Velocity、FreeMarker等传统引擎,关于其更多相关内容,需要的小伙伴可以参考一下
    2022-06-06
  • Java中Object.wait()和LockSupport.park()的用法

    Java中Object.wait()和LockSupport.park()的用法

    Object.wait()和LockSupport.park()都是用来使当前线程等待的方法,本文主要介绍了Java中Object.wait()和LockSupport.park()的用法,具有一定的参考价值,感兴趣的可以了解一下
    2024-05-05
  • Java开发中可以防止界面假死的刷新代码

    Java开发中可以防止界面假死的刷新代码

    今天小编就为大家分享一篇关于Java开发中可以防止界面假死的刷新代码,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2019-01-01
  • Java设计模式初识之备忘录模式详解

    Java设计模式初识之备忘录模式详解

    备忘录设计模式(Memento Design Pattern)也叫作快照(Snapshot)模式,主要用于实现防丢失、撤销、恢复等功能。本文将通过示例为大家介绍一些备忘录模式的定义与使用,需要的可以参考一下
    2022-11-11
  • Java程序常见异常及处理汇总

    Java程序常见异常及处理汇总

    这篇文章主要介绍了java程序常见异常及处理汇总,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-10-10

最新评论