简体   繁体   English

Spring Data JPA:查询优化

[英]Spring Data JPA: Query Optimization

While working on a demo project I stuck at Spring Data JPA method based query. 在进行演示项目时,我停留在基于Spring Data JPA方法的查询上。 I want to search top 5 results which contain my search team in name or address field. 我想搜索名称或地址字段中包含我的搜索团队的top 5结果。

For that my repository method is: 为此,我的存储库方法是:

findTop5ByNameContainingIgnoreCaseOrAddressContainingIgnoreCase(String name,
      String address);

I am just wondering is it most optimized way to achieve results or it can further optimize to improve performance? 我只是想知道这是获得结果的最优化方法,还是可以进一步优化以提高性能?

The method: 方法:

findTop5ByNameContainingIgnoreCaseOrAddressContainingIgnoreCase(String name, String address);

Generates a SQL like (Sql Server example): 生成一个类似SQL(以SQL Server为例):

select top 5 e.* from Entity e 
WHERE UPPER(e.name) LIKE UPPER('%SomeName%') OR UPPER(e.address) LIKE UPPER('%SomeAddress%')

In terms of SQL, this query is already really straightforward. 就SQL而言,此查询已经非常简单。

To improve the performance, you could create a index for this two fields or create two indexed new columns for address and name , both already saved with upper case. 为了提高性能,您可以为此两个字段创建一个索引,或者为addressname创建两个已索引的新列,它们都已经保存为大写。 So you will not have to convert each information of address and name to upper case and compare. 因此,您不必将地址和名称的每个信息都转换为大写并进行比较。

Like: 喜欢:

id | name | name_search | address    | address_search
1  | John | JOHN        | Street Abc | STREET ABC

And the query: 和查询:

findTop5ByNameSearchContainingOrAddressSearchContaining(
    String nameUpperCase, String addressUpperCase);

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

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