database.sarang.net
UserID
Passwd
Database
DBMS
MySQL
ㆍPostgreSQL
Firebird
Oracle
Informix
Sybase
MS-SQL
DB2
Cache
CUBRID
LDAP
ALTIBASE
Tibero
DB 문서들
스터디
Community
공지사항
자유게시판
구인|구직
DSN 갤러리
도움주신분들
Admin
운영게시판
최근게시물
PostgreSQL Q&A 8624 게시물 읽기
No. 8624
쿼리속도 비교
작성자
황수현(withmuse)
작성일
2010-02-19 09:03ⓒ
2010-02-20 10:04ⓜ
조회수
9,203

제가 가지고 있는 두가지 쿼리는 조건문의 값만 틀리고 다른 곳은 똑같습니다.

그런데 1번쿼리는 속도가 빠른데 2번 쿼리는 속도가 느리네요.  10분도 더 걸립니다.

조언을 좀 부탁드려요.

sampleTime 컬럼의 인덱스를 다른걸로 잡아줘야 할까요?

----------1번 쿼리: incarnaion 값이 500개 정도 , 실행속도: 1초미만 , 결과값 700행 정도

SELECT site_id,
                          "Incarnation",
                          EXTRACT(EPOCH FROM "SampleTime") * 1000 as "SampleTime",
                          "Sample_id",
                          "StatisticID",
                          "Value"
                     FROM "usdcLogDetails_1" AS uld
                     JOIN "usdcLogStatistics" AS uls
                       ON uld.id=uls."Sample_id" WHERE (site_id=1 AND (("Incarnation"=18 AND "StatisticID"=140)))
                       AND "SampleTime"
                       BETWEEN TO_TIMESTAMP(1266307200000 / 1000) AND TO_TIMESTAMP(1266393600000 / 1000)

ORDER BY "SampleTime" ASC

 

CREATE TABLE "usdcLogDetails_1"
(
  id bigserial NOT NULL,
  site_id integer NOT NULL DEFAULT 0,
  "Log_id" integer NOT NULL,
  "SampleTime" timestamp with time zone NOT NULL,
  "Incarnation" integer NOT NULL,
  CONSTRAINT "usdcLogDetails_1_pkey" PRIMARY KEY (id),
  CONSTRAINT "usdcLogDetails_1_Log_id_fkey" FOREIGN KEY ("Log_id")
      REFERENCES "usdcLogMaster" (id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION DEFERRABLE INITIALLY DEFERRED,
  CONSTRAINT "usdcLogDetails_1_Log_id_fkey1" FOREIGN KEY ("Log_id", "Incarnation")
      REFERENCES "usdcLogSourceDescription_1" ("Log_id", "Incarnation") MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION
)
WITH (OIDS=FALSE);
ALTER TABLE "usdcLogDetails_1" OWNER TO mhwang;

 

CREATE INDEX "usdcLogDetails_1_index_Incarnation"
  ON "usdcLogDetails_1"
  USING btree
  ("Incarnation");

 

CREATE INDEX "usdcLogDetails_1_index_SampleTime"
  ON "usdcLogDetails_1"
  USING btree
  ("SampleTime");

explain analyze 결과

"Sort  (cost=490.21..490.21 rows=1 width=36) (actual time=1471.000..1471.000 rows=732 loops=1)"
"  Sort Key: (date_part('epoch'::text, uld."SampleTime") * 1000::double precision)"
"  ->  Nested Loop  (cost=135.08..490.20 rows=1 width=36) (actual time=188.000..1471.000 rows=732 loops=1)"
"        Join Filter: (uld.id = uls."Sample_id")"
"        ->  Bitmap Heap Scan on "usdcLogDetails_1" uld  (cost=129.35..191.05 rows=1 width=24) (actual time=188.000..190.000 rows=746 loops=1)"
"              Recheck Cond: (("Incarnation" = 18) AND ("SampleTime" >= '2010-02-16 00:00:00-08'::timestamp with time zone) AND ("SampleTime" <= '2010-02-17 00:00:00-08'::timestamp with time zone))"
"              Filter: (site_id = 1)"
"              ->  BitmapAnd  (cost=129.35..129.35 rows=16 width=0) (actual time=188.000..188.000 rows=0 loops=1)"
"                    ->  Bitmap Index Scan on "usdcLogDetails_1_index_Incarnation"  (cost=0.00..60.52 rows=3226 width=0) (actual time=0.000..0.000 rows=746 loops=1)"
"                          Index Cond: ("Incarnation" = 18)"
"                    ->  Bitmap Index Scan on "usdcLogDetails_1_index_SampleTime"  (cost=0.00..68.58 rows=3226 width=0) (actual time=188.000..188.000 rows=645156 loops=1)"
"                          Index Cond: (("SampleTime" >= '2010-02-16 00:00:00-08'::timestamp with time zone) AND ("SampleTime" <= '2010-02-17 00:00:00-08'::timestamp with time zone))"
"        ->  Bitmap Heap Scan on "usdcLogStatistics" uls  (cost=5.73..296.76 rows=191 width=20) (actual time=0.332..1.087 rows=1915 loops=746)"
"              Recheck Cond: ("StatisticID" = 140)"
"              ->  Bitmap Index Scan on "usdcLogStatistics_index_StatisticID"  (cost=0.00..5.69 rows=191 width=0) (actual time=0.276..0.276 rows=1915 loops=746)"
"                    Index Cond: ("StatisticID" = 140)"
"Total runtime: 1471.000 ms"
 

----------------2번 쿼리:  incarnaion 값이 10만개 정도, 실행속도: 너무 길어 기억이 안날 정도임, between 절을 빼고 쿼리를 날리면 속도가 엄청 빠름  , 결과값 200행정도

SELECT site_id,
                          "Incarnation",
                          --EXTRACT(EPOCH FROM "SampleTime") * 1000 as "SampleTime",
                          "SampleTime",
                          "Sample_id",
                          "StatisticID",
                          "Value"
                     FROM "usdcLogDetails_1" AS uld
                     JOIN "usdcLogStatistics" AS uls
                       ON uld.id=uls."Sample_id" WHERE (site_id=1 AND (("Incarnation"=2 AND "StatisticID"=140)))
                       AND "SampleTime"
                       BETWEEN  '2010-02-16 15:05:30.393-08' AND '2010-02-16 16:49:40.384-08'
 

ORDER BY "SampleTime" ASC

 

CREATE TABLE "usdcLogStatistics"
(
  id bigserial NOT NULL,
  "Sample_id" bigint NOT NULL,
  "StatisticID" integer,
  "Value" bigint NOT NULL,
  CONSTRAINT "usdcLogStatistics_pkey" PRIMARY KEY (id),
  CONSTRAINT "usdcLogStatistics_Sample_id_fkey" FOREIGN KEY ("Sample_id")
      REFERENCES "usdcLogDetails_1" (id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION DEFERRABLE INITIALLY DEFERRED
)
WITH (OIDS=FALSE);
ALTER TABLE "usdcLogStatistics" OWNER TO mhwang;

 

CREATE INDEX "usdcLogStatistics_index_StatisticID"
  ON "usdcLogStatistics"
  USING btree
  ("StatisticID");                      

explain analyze 결과

"Sort  (cost=490.20..490.21 rows=1 width=36) (actual time=848705.000..848705.000 rows=210 loops=1)"
"  Sort Key: uld."SampleTime""
"  ->  Nested Loop  (cost=135.08..490.19 rows=1 width=36) (actual time=336.000..848705.000 rows=210 loops=1)"
"        Join Filter: (uld.id = uls."Sample_id")"
"        ->  Bitmap Heap Scan on "usdcLogDetails_1" uld  (cost=129.35..191.05 rows=1 width=24) (actual time=336.000..843.000 rows=501246 loops=1)"
"              Recheck Cond: (("Incarnation" = 2) AND ("SampleTime" >= '2010-02-16 15:05:30.393-08'::timestamp with time zone) AND ("SampleTime" <= '2010-02-16 16:49:40.384-08'::timestamp with time zone))"
"              Filter: (site_id = 1)"
"              ->  BitmapAnd  (cost=129.35..129.35 rows=16 width=0) (actual time=334.000..334.000 rows=0 loops=1)"
"                    ->  Bitmap Index Scan on "usdcLogDetails_1_index_Incarnation"  (cost=0.00..60.52 rows=3226 width=0) (actual time=180.000..180.000 rows=501246 loops=1)"
"                          Index Cond: ("Incarnation" = 2)"
"                    ->  Bitmap Index Scan on "usdcLogDetails_1_index_SampleTime"  (cost=0.00..68.58 rows=3226 width=0) (actual time=154.000..154.000 rows=645155 loops=1)"
"                          Index Cond: (("SampleTime" >= '2010-02-16 15:05:30.393-08'::timestamp with time zone) AND ("SampleTime" <= '2010-02-16 16:49:40.384-08'::timestamp with time zone))"
"        ->  Bitmap Heap Scan on "usdcLogStatistics" uls  (cost=5.73..296.76 rows=191 width=20) (actual time=0.339..1.057 rows=1915 loops=501246)"
"              Recheck Cond: ("StatisticID" = 140)"
"              ->  Bitmap Index Scan on "usdcLogStatistics_index_StatisticID"  (cost=0.00..5.69 rows=191 width=0) (actual time=0.300..0.300 rows=1915 loops=501246)"
"                    Index Cond: ("StatisticID" = 140)"
 

이 글에 대한 댓글이 총 2건 있습니다.

짧은 소견으로는 이렇습니다.

 

uld 의 sample_time 의 결과가 60만개가 넘습니다

그래서 느려지는거 같고요

StatisticID"=140 이것의 결과가 2000 개가 안 되는군요..

 

그럼 차라리

조인할때 저 조건도 함께 on 에 넣으면

 

조인된 결과가 엄청나게 적게 나올거 같습니다.

그 결과내에서 sample_time 조건 넣어봐야...  더 이상 느려질 것도 없어 보입니다.

만일 그래도 플랜이 같이 나온다면

아주 적게 나온 결과를 서브쿼리로 두면 될거 같아 보입니다

 

tyro님이 2010-02-20 15:57에 작성한 댓글입니다.
이 댓글은 2010-02-20 16:03에 마지막으로 수정되었습니다. Edit

조언 주셔서 감사합니다.

제가 서브쿼리를 써서 한것도 2번의 쿼리는 속도가 느렸습니다.  그래서 이글을 올렸는데, 님조언대로 아침에 출근하면 조인 on 에 쿼리를 넣어 봐야 겠네요.

감사합니다.

 

황수현(withmuse)님이 2010-02-23 13:49에 작성한 댓글입니다.
[Top]
No.
제목
작성자
작성일
조회
8628외부 파일을 Copy문을 이용해서 PostgreSQL 서버에 입력 가능하게 하려면?? [1]
설동임
2010-02-25
8350
8627ecpg 가 이상해서요.
손광락
2010-02-25
7430
8626date 형식은 global 한것인가요? [2]
성제호
2010-02-24
8741
8624쿼리속도 비교 [2]
황수현
2010-02-19
9203
8623Vacuum에서 full 사용 관련하여 문의 드립니다. [4]
박병훈
2010-02-18
10073
8622칼럼 형 변환. [4]
슝슝이
2010-02-17
8239
8621Vacuum 관련 문의 드립니다. [2]
박병훈
2010-02-17
8291
Valid XHTML 1.0!
All about the DATABASE... Copyleft 1999-2024 DSN, All rights reserved.
작업시간: 0.018초, 이곳 서비스는
	PostgreSQL v16.2로 자료를 관리합니다