As there are 100 000 records so I need to fetch top 10
records by joining two tables, am using mssql
server,i have written @Query
, but its making 11 queries for the same which is causing performance issue.
It is taking around 9-10 seconds
.
@Getter
@Setter
@Entity
@Table(name = "TelevisionSource")
public class MyTelevisionSource {
@Id
private Long SourceId;
@Column(columnDefinition = "nvarchar2 (2000)")
private String TvsourceLongName;
@Column(columnDefinition = "nvarchar2 (2000)")
private String TvsourceDisplayName;
@OneToOne(fetch=FetchType.LAZY)
@JoinColumn(name = "SourceId")
private RCMSource rcmSource;
@Override
public String toString() {
return "MyTelevisionSource [SourceId=" + SourceId + ", TvsourceLongName=" + TvsourceLongName
+ ", TvsourceDisplayName=" + TvsourceDisplayName + ", rcmSource=" + rcmSource + "]";
}
}
@Getter
@Setter
@ToString
@Entity
@Table(name = "Source")
public class RCMSource {
@Id
private Long SourceId;
@Column(columnDefinition = "nvarchar2 (2000)")
private String SourceName;
}
Service layer
***************
@Service
public class TelevisionSourceService {
@Autowired
private TelevisionSourceRepository televisionSourceRepo;
public List<MyTelevisionSource> getTelevisionSource(){
Pageable pageable = PageRequest.of(0, 10);
Page<MyTelevisionSource> tvSource = televisionSourceRepo.findAll(pageable);
List<MyTelevisionSource> sourceList= tvSource.getContent();
sourceList.forEach(System.out::println);
return sourceList;
}
}
Repo
****
public interface TelevisionSourceRepository extends JpaRepository<MyTelevisionSource, Long> {
Page<MyTelevisionSource> findAll(Pageable pageable);
}
and also i tried by writing @Query
for the same, but it is also making 11 queries,
@Query("select t from MyTelevisionSource t inner join RCMSource s ON s.SourceId = t.SourceId")
Page<MyTelevisionSource> findAll(Pageable pageable);
without using pageable, is there any other way to fetch top 10 records, by joining two tables
Hibernate:
select
TOP(?) mytelevisi0_.SourceId as SourceId1_1_,
mytelevisi0_.TvsourceDisplayName as Tvsource2_1_,
mytelevisi0_.TvsourceLongName as Tvsource3_1_
from
dbo.TelevisionSource mytelevisi0_
inner join
dbo.Source rcmsource1_
on (
rcmsource1_.SourceId=mytelevisi0_.SourceId
)
Hibernate:
select
rcmsource0_.SourceId as SourceId1_0_0_,
rcmsource0_.SourceName as SourceNa2_0_0_
from
dbo.Source rcmsource0_
where
rcmsource0_.SourceId=?
Hibernate:
select
rcmsource0_.SourceId as SourceId1_0_0_,
rcmsource0_.SourceName as SourceNa2_0_0_
from
dbo.Source rcmsource0_
where
rcmsource0_.SourceId=?
Hibernate:
select
rcmsource0_.SourceId as SourceId1_0_0_,
rcmsource0_.SourceName as SourceNa2_0_0_
from
dbo.Source rcmsource0_
where
rcmsource0_.SourceId=?
Hibernate:
select
rcmsource0_.SourceId as SourceId1_0_0_,
rcmsource0_.SourceName as SourceNa2_0_0_
from
dbo.Source rcmsource0_
where
rcmsource0_.SourceId=?
Hibernate:
select
rcmsource0_.SourceId as SourceId1_0_0_,
rcmsource0_.SourceName as SourceNa2_0_0_
from
dbo.Source rcmsource0_
where
rcmsource0_.SourceId=?
Hibernate:
select
rcmsource0_.SourceId as SourceId1_0_0_,
rcmsource0_.SourceName as SourceNa2_0_0_
from
dbo.Source rcmsource0_
where
rcmsource0_.SourceId=?
Hibernate:
select
rcmsource0_.SourceId as SourceId1_0_0_,
rcmsource0_.SourceName as SourceNa2_0_0_
from
dbo.Source rcmsource0_
where
rcmsource0_.SourceId=?
Hibernate:
select
rcmsource0_.SourceId as SourceId1_0_0_,
rcmsource0_.SourceName as SourceNa2_0_0_
from
dbo.Source rcmsource0_
where
rcmsource0_.SourceId=?
Hibernate:
select
rcmsource0_.SourceId as SourceId1_0_0_,
rcmsource0_.SourceName as SourceNa2_0_0_
from
dbo.Source rcmsource0_
where
rcmsource0_.SourceId=?
Hibernate:
select
rcmsource0_.SourceId as SourceId1_0_0_,
rcmsource0_.SourceName as SourceNa2_0_0_
from
dbo.Source rcmsource0_
where
rcmsource0_.SourceId=?
Hibernate:
select
count(mytelevisi0_.SourceId) as col_0_0_
from
dbo.TelevisionSource mytelevisi0_
inner join
dbo.Source rcmsource1_
on (
rcmsource1_.SourceId=mytelevisi0_.SourceId
)