select A.idx, A.mem_id, A.mem_name
, (select count(id) from tb_order 
 where ad_flag=N'Y' and mem_id=A.mem_id and gender='M') as count_m
, (select count(id) from tb_order 
 where ad_flag=N'Y' and mem_id=A.mem_id and gender='W') as count_w

, (select count(id) from tb_order
 where ad_flag=N'Y' and mem_id=A.mem_id and oldg='10') as count_10

, (select count(id) from tb_order
 where ad_flag=N'Y' and mem_id=A.mem_id and oldg='20') as count_20

, (select count(id) from tb_order
 where ad_flag=N'Y' and mem_id=A.mem_id and oldg='30') as count_30

, (select count(id) from tb_order
 where ad_flag=N'Y' and mem_id=A.mem_id and oldg='40') as count_40

, (select count(id) from tb_order
 where ad_flag=N'Y' and mem_id=A.mem_id and oldg='50') as count_50

, (select count(id) from tb_order
 where ad_flag=N'Y' and mem_id=A.mem_id and oldg='60') as count_60


from tb_member A


where문 부분은 제외하고 대충 이런식으로 통계 테이블을 뿌려주고 있는데요.

데이터가 수백만건이 되니까 이런 서브쿼리로는 속도가 너무 느려요.

집계 카운팅을 하는 대상은 tb_order 테이블 하나뿐인데, 이걸 효율적으로 select 해올수 있는

쿼리는 어떻게하면 될까요?

No. Subject Author Date Views
Notice 2023년 1월 - SQLER의 업데이트 강좌 리스트 코난(김대우) 2023.01.02 1198
8180 Query 문 질문 드립니다. [2] 슈토파이터 2014.09.15 2373
8179 종속관계 삭제를 어떻게 해야하나요? 실실이 2014.09.12 2487
8178 top 관련 [5] 미라클 2014.09.12 2318
8177 2012 백업 방법 좀 알려주세요 [1] 전혜연 2014.09.12 1951
8176 수량 분할 조회 쿼리 질문있습니다. [2] 닝쿠 2014.09.11 3286
8175 Index 설정 전.. [3] SQL어렵.. 2014.09.11 2462
8174 스칼라반환함수관련 [2] HSQL 2014.09.11 2324
8173 두가지 상황에서 쿼리 속도 차이가 있을까요. [5] 애즈원 2014.09.11 2862
8172 두 서버간의 데이터 업데이트 [5] 파인드쿠폰 2014.09.11 3474
8171 LOG 파일 관리 [1] taz2315 2014.09.10 4328
8170 DB 서버명 변경 [1] taz2315 2014.09.10 3317
8169 log.ldf 파일 줄이기 [1] taz2315 2014.09.06 2607
8168 서버이전 후 IIS - MSSQL 간 연결 지연 문제 질문드립니다. [2] 야채호빵v 2014.09.05 3730
8167 두개의 쿼리문을 join 시켜 하나로 합치려 하는데 어렵네요.. [4] AutoCad 2014.09.05 3926
8166 dbnetlib connectionopen (connect()). sql server가 없거나 액세스할 수 없습니다 [1] 오락대장 2014.09.05 6629
8165 Query 질문 드립니다. [4] 슈토파이터 2014.09.05 2387
8164 테이블 두개의 차집합을 구하고 싶습니다. 도와주세요ㅠㅠ [3] 사이드사이드 2014.09.03 2920
8163 agent 실행주기 [4] afddsa 2014.09.03 2483
» 무식한 통계 서브쿼리 최적화 방법 좀; [4] 애즈원 2014.09.03 2872
8161 데이터 베이스 용량을 줄이고자 합니다. [2] 호짱 2014.09.03 3610





XE Login