I have a Spring Boot application with a JpaRepository. I am trying right now to obtain all the unique values for a certain field in my db.
Let's suppose I have an object Foo with an Integer PK id and a non-unique Integer field bar. I want to get all the unique values for bar and I'm wondering which query from the following is the most efficient:
- Use DISTINCTin theSELECTstatement and store the result in a List.
@Query("select distinct f.bar from Foo f")
List<Integer> getUniqueBar();
- Use DISTINCTin theSELECTand store the result in a Set.
@Query("select distinct f.bar from Foo f")
Set<Integer> getUniqueBar();
- Don't use DISTINCTin the select and store the result in a Set.
@Query("select f.bar from Foo f")
Set<Integer> getUniqueBar();
All the 3 queries yield the same result. Which one is the most efficient?
IMO, it should be 1, since it does the duplicate filtering in the db, but I'm not sure if using a List over a Set brings much gain.
(I'm not interested in the actual container I'm storing the result into, as long as it is a Collection.)
 
    