简体   繁体   English

使用spring data jpa从db过滤和检索数据

[英]Filtering and retrieving data from db using spring data jpa

I am using reactjs as a front-end and spring boot as back-end. 我正在使用reactjs作为前端,并使用spring boot作为后端。 I am using spring data jpa. 我正在使用Spring Data JPA。

Here is my dilemma.. and I know the way I have written the logic is inefficient and would like to know and understand a better approach to address the issue. 这是我的两难选择。我知道编写逻辑的方式效率很低,并且想知道和理解解决此问题的更好方法。

Lets say there are thousands of records in the db and I need to retrieve them constantly and instantly (if possible) 可以说数据库中有成千上万条记录,我需要不断且即时地检索它们(如果可能)

I have 2 fields in UI. 我在UI中有2个字段。 One is String and the other is Integer. 一个是字符串,另一个是整数。

Name (String) SomeKindOfId (int) ------------- ------------------- 名称(字符串)SomeKindOfId(int)------------- -------------------

I have to filter the records only after 3 characters are entered in Name field and/or in SomeKindOfId field. 仅在“名称”字段和/或“ SomeKindOfId”字段中输入3个字符后,才需要过滤记录。

How can I achieve this efficiently? 如何有效地做到这一点?

currently, I have 目前,我有

findAllByNameAndSomeKindOfId("%"+name+"%","%"+String.valueOf(SomeKindOfId)+"%") findAllByNameAndSomeKindOfId( “%” +名称+ “%”, “%” +将String.valueOf(SomeKindOfId)+ “%”)

in service 在服

and something like this in repository class 在存储库类中是这样的

Query("Select u from SomeTable u where u.name = :name and CAST(u.someKindOfId As string) like :someKindOfId")
List<CdoDimStPortfolio> findAllByEntityStatusAndDealName(@Param("name") String name,
        @Param("someKindOfId") String someKindOfId);

The reason I am using casting is because I have someKindOfId as int in database 我使用强制转换的原因是因为我在数据库中有someKindOfId作为int

The best way to make efficient database queries is to create indexes by the values you are going to search. 进行有效的数据库查询的最佳方法是根据要搜索的值创建索引。

You could create a index "name" and "someKindOfId" and the search for those values would be immediate, because it's like in a book, if you search a chapter page by page you will take much longer than if you go to the index and search for it. 您可以创建一个索引“ name”和“ someKindOfId”,并且这些值的搜索将是立即进行的,因为就像在书本中一样,如果您逐页搜索章节,则比查找索引要花费更长的时间,并且寻找它。

For example for postgresql: 例如postgresql:

https://www.postgresql.org/docs/9.1/sql-createindex.html https://www.postgresql.org/docs/9.1/sql-createindex.html

CREATE INDEX constructs an index on the specified column(s) of the specified table. CREATE INDEX在指定表的指定列上构造索引。 Indexes are primarily used to enhance database performance 索引主要用于增强数据库性能

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

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