简体   繁体   中英

Filtering and retrieving data from db using spring data jpa

I am using reactjs as a front-end and spring boot as back-end. I am using 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. One is String and the other is Integer.

Name (String) SomeKindOfId (int) ------------- -------------------

I have to filter the records only after 3 characters are entered in Name field and/or in SomeKindOfId field.

How can I achieve this efficiently?

currently, I have

findAllByNameAndSomeKindOfId("%"+name+"%","%"+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

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.

For example for postgresql:

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

CREATE INDEX constructs an index on the specified column(s) of the specified table. Indexes are primarily used to enhance database performance

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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