본문 바로가기
728x90

SQL4

SQL - partition by 성능개선 row_number() over (partition by ~ order by ~) 문법을 사용하는 도중 성능 이슈 문제가 발견되어 몇 가지 개선방법을 기록하기 위해 글을 남깁니다. 1. partition by ~ order by ~ 사용할 때 partition by 컬럼과 order by 컬럼에 index를 걸어줍니다. 2. 만약 partition by 컬럼과 order by 컬럼이 한 테이블에 없다면, with book(brand_cd, wish_dt) as ( select brand_cd ,create_dt as wish_dt from ecomms.tpd_brand_bookmark book where book.mem_no = (select mem_no from "member".tmb_mem where.. 2021. 10. 7.
SQL - Array 정리 SQL Array가 다 비슷비슷해보여서 구분하고 정리할 필요가 있어 포스트합니다. 대표적으로 ARRAY, ARRAY_AGG, ARRAY_TO_STRING, STRING_AGG을 구분하겠습니다. ARRAY ARRAY는 select ARRAY[1,2,3]::int8[]; 와 같이 element를 모아서 배열 형식으로 만들 수 있습니다. 배열을 만들 때 자료형만 주의하면 손쉽게 데이터를 배열로 호출할 수 있습니다. 또한 ARRAY는 서브쿼리를 통해서 배열 데이터를 만들 수 있습니다. select ARRAY(select prd_no from tpd_prd limit 5)::integer[] ARRAY_AGG ARRAY_AGG는 ARRAY와 같이 배열을 만들어주지만 차이점이 있습니다. ARRAY_AGG는 from.. 2021. 10. 5.
SQL - LATERAL Lateral은 PostSql에만 있는 문법입니다. join할 때 각 row 별로 for-each문처럼 join할 row를 고를 수 있습니다. 또한 lateral 안에서는 외부 스코프를 볼 수 있습니다. 즉, 외부에서 변수 개념으로 컬럼 값을 넘겨줄 수 있습니다. select * from tpd_prd prd left join lateral ( select SUM(tstc.real_stck_qty) as stck_qty , SUM(tstc.temp_stck_qty) as temp_stck_qty from ecomms.tpd_stock tstc inner join ecomms.tpd_opt_value val on prd.prd_no=tstc.prd_no and val.stock_no=tstc.stock_n.. 2021. 10. 5.
SQL - WITH RECURSIVE 문(재귀 쿼리) 1. WITH RECURSIVE 기본 구문 with recursive 테이블명 as ( select 1 as p --재귀변수 union all select p+1 from 테이블명 where p 2021. 4. 23.
728x90