Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/select query #24

Merged
merged 4 commits into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,12 @@ List<TrafficDetailEntity> findRecentlyData(

@Query(
value =
"SELECT * FROM traffic_detail td "
+ "INNER JOIN (SELECT t.traffic_id, MAX(t.time_left_reg_dt) AS maxTimeLeftRegDt "
+ " FROM traffic_detail t "
+ " WHERE t.traffic_id IN :trafficIds "
+ " GROUP BY t.traffic_id) maxTd "
+ "ON td.traffic_id = maxTd.traffic_id AND td.time_left_reg_dt = maxTd.maxTimeLeftRegDt "
+ "WHERE td.traffic_id IN :trafficIds",
nativeQuery = true)
" SELECT td1 "
+ " FROM TrafficDetailEntity td1 "
+ " WHERE td1.traffic in (:trafficIds) "
+ " AND td1.timeLeftRegDt = ( "
+ " SELECT MAX(td2.timeLeftRegDt) "
+ " FROM TrafficDetailEntity td2 "
+ " WHERE td2.traffic = td1.traffic)")
List<TrafficDetailEntity> findMostRecenlyData(@Param("trafficIds") List<Long> trafficIds);
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

주어진 코드 패치는 좋아 보입니다. 개선을 제안하자면 'traffic' 컬럼과 'timeLeftRegDt' 컬럼에 대한 정확한 조인 조건이 필요합니다. 현재의 쿼리는 서브쿼리를 사용하여 가장 최근 데이터를 찾습니다. 그러나 성능 개선을 위해 인덱스를 고려할 수 있습니다. 또한 주석을 추가하여 쿼리를 설명하는 것이 도움이 될 수 있습니다.

Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Index;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
Expand All @@ -28,7 +29,9 @@
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@Entity
@SuperBuilder(toBuilder = true)
@Table(name = "traffic_detail")
@Table(
name = "traffic_detail",
indexes = @Index(name = "traffic_id_idx", columnList = "traffic_id"))
@ToString
public class TrafficDetailEntity {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.walking.data.entity.BaseEntity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Index;
import javax.persistence.Table;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
Expand All @@ -17,7 +18,7 @@
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@Entity
@SuperBuilder(toBuilder = true)
@Table(name = "traffic")
@Table(name = "traffic", indexes = @Index(name = "point_idx", columnList = "point_value"))
@SQLDelete(sql = "UPDATE traffic SET deleted=true where id=?")
public class TrafficEntity extends BaseEntity {

Expand Down
Loading