X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.2]) by svr1.postgresql.org (Postfix) with ESMTP id A84A4D1B83D for ; Thu, 5 Feb 2004 06:42:07 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (neptune.hub.org [200.46.204.2]) (amavisd-new, port 10024) with ESMTP id 35892-06 for ; Thu, 5 Feb 2004 02:42:08 -0400 (AST) Received: from millenium.mst.co.jp (unknown [210.230.185.241]) by svr1.postgresql.org (Postfix) with ESMTP id 82DE2D1D412 for ; Thu, 5 Feb 2004 02:42:00 -0400 (AST) Received: from mst1x5r347kymb (lc12114 [192.168.1.114]) by millenium.mst.co.jp (8.11.6p2/3.7W) with SMTP id i156g2Y04192; Thu, 5 Feb 2004 15:42:02 +0900 Message-ID: <007401c3ebb3$2a534740$7201a8c0@mst1x5r347kymb> From: "Iain" To: "Richard Sydney-Smith" , References: <001001c3eb8a$255d6dc0$5801a8c0@athlon2000> Subject: Re: Slow sub-selects, max and count(*) Date: Thu, 5 Feb 2004 15:42:03 +0900 MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_NextPart_000_0070_01C3EBFE.9A2F0860" X-Priority: 3 X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook Express 6.00.2800.1158 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165 X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200402/51 X-Sequence-Number: 16742 This is a multi-part message in MIME format. ------=_NextPart_000_0070_01C3EBFE.9A2F0860 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Hi, I don't think thatyou gave enough information to get any direct help, for example, what are these "sub-selects"? I often see performance problems arising from procedural code that puts selects that don't use indexes inside loops. That's a plain programming issue, and understanding your system. Another situation I encountered recently was using "in" sub selects. Recoding them as "exists" or "not exists" as the case may be turned a query that ran for 18hours with no sign of ending (before I killed it) into a query that ran in a couple of minutes. I always try to avoid procedural code in such batch updates. If it can be encoded into one insert then it is usually better, It just depends on what you are doing. ----- Original Message ----- From: Richard Sydney-Smith To: pgsql-sql@postgresql.org Sent: Thursday, February 05, 2004 10:48 AM Subject: [SQL] Slow sub-selects, max and count(*) I have a procedure ( included below with table definition) to import end of day quotes into a table fsechist. The issue is with the speed ( or lackof it) that the procedure proceeds. Apparently it is the subselects that are the worst issue and I have tried to replace these. Also max() and count() refuse to use indexes. I have replaced max() with (select ... as mx from ... order by ... desc limit 1) and this is much faster but appear to have to avoid count() as I can not find a way to point this at an index. Please, I am sure that there is a better way to do this. And 5 to 7 minutes to insert a day is really too slow. Each days import table contains about 3200 records. Total table size for fsechist is about 2.5 million records. If you could point me in the right direction here it would be much appreciated. thanks Richard Sydney-Smith -------------------------------------------------------------------------- ------------------------------ CREATE OR REPLACE FUNCTION public.import_sharedata(varchar) RETURNS bool AS ' declare filnam alias for $1; alldone bool := true; /* cnt integer := 0; */ /* count of number inserted not used */ begin /* Imports a text file containing end of day price quotes filnam should be in double backslash format e.g. c:\\data\\sharequotes\\quotes.dat Ensures that it only brings in quotes for companies we have listed in our database and ensures only the latest copy of the quote is kept for each day Import table has 7 columns in CSV tab delimited ascii format. EzyChart format has prices in cents, METASTOCK in dollars MetaStock +100 has the volume divided by 100. Depending on your import data source you may need to process the temp_shareprices so that the values are in Dollars , not cents and the volume is per unit not per 100 units. Present calculation is for EzyChart format. Both tables have to have a 4 digit year yyyymmdd Warning : If you have the wrong format this procedure will import garbage into your database !!! */ perform drop_table(\'temp_shareprices\'); create table temp_shareprices( ticker char(10) null, dte char(8), v1 numeric(19,4) not null default 0, v2 numeric(19,4) not null default 0, v3 numeric(19,4) not null default 0, v4 numeric(19,4) not null default 0, v5 numeric(19,4) not null default 0 ); execute \'copy temp_shareprices from \'||quote_literal(filnam)||\' delimiter \'||quote_literal(\',\'); -- extend yr to 4 digits, alter quotes to dollars update temp_shareprices set dte = \'20\'||dte,v1=v1/100,v2=v2/100,v3=v3/100,v4=v4/100; -- add in any new company tickers insert into fsecurity (sec_cde) select ticker from temp_shareprices where length(trim(ticker))=3 and ticker not in ( select sec_cde from fsecurity); -- ==================== -- remove rows from temp_shareprices where we do not have a corresponding security -- using sub-select is slow -- delete from temp_shareprices where ticker not in (select sec_cde from fsecurity); -- can just delete ones where length<>3 as insert above ensures all length 3 are inserted. All ASX codes are 3char long delete from temp_shareprices where length(trim(ticker))<>3; -- Attempt to remove without limiting to length and not using sub select does not work --update temp_shareprices set ticker = s.sec_cde from temp_shareprices t left outer join fsecurity s on t.ticker = s.sec_cde; --delete from temp_shareprices where ticker is null; -- ==================== -- put indexes on temp table CREATE INDEX temphist_tick ON temp_shareprices USING btree (ticker); CREATE INDEX temphist_dte ON temp_shareprices USING btree (dte); -- ==================== -- Scan the input table and delete as appropriate from fsechist -- Using sub select to remove any pre-existing records in fsechist very slow -- delete from fsechist where hist_q in (select hist_q from fsechist, temp_shareprices where hist_tick = ticker and dte = hist_date); -- Tried to replace subselect update fsechist set hist_tick = \'@@\' from fsechist h, temp_shareprices t where t.ticker = h.hist_tick and h.hist_date = t.dte; delete from fsechist where hist_tick = \'@@\'; -- ==================== -- insert newvalues insert into fsechist(hist_tick,hist_date,hist_v1,hist_v2,hist_v3,hist_v4,hist_vol) select ticker,dte,v1,v2,v3,v4,v5 from temp_shareprices; -- count the number inserted -- select count(*) into cnt from temp_shareprices; -- perform drop_table(\'temp_shareprices\'); return alldone; end;' LANGUAGE 'plpgsql' VOLATILE; Note : drop_table is a simple procedure that drops a procedure by name iff it exists. ============================================== Definition of Fsechist CREATE TABLE public.fsechist ( hist_q int4 NOT NULL DEFAULT nextval('hist_q'::text), hist_v1 numeric(19,4), hist_v2 numeric(19,4), hist_v3 numeric(19,4), hist_v4 numeric(19,4), hist_vol numeric(19,4), hist_deltar numeric(19,4), hist_deltag numeric(19,4), hist_date char(8), hist_tick char(10), hist_announce char(1), CONSTRAINT fsechist_pkey PRIMARY KEY (hist_q) ) WITH OIDS; CREATE INDEX fsechist_date ON public.fsechist USING btree (hist_date); CREATE UNIQUE INDEX fsechist_q ON public.fsechist USING btree (hist_q); CREATE INDEX fsechist_tick ON public.fsechist USING btree (hist_tick); ------=_NextPart_000_0070_01C3EBFE.9A2F0860 Content-Type: text/html; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable
Hi,
 
I don't think thatyou gave enough= =20 information to get any direct help, for example, what are these=20 "sub-selects"?
 
I often see performance problems = arising=20 from procedural code that puts selects that don't use indexes inside l= oops.=20 That's a plain programming issue, and understanding your system.
 
Another situation I encountered r= ecently=20 was using "in" sub selects. Recoding them as "exists" or "not exists" as th= e=20 case may be turned a query that ran for 18hours with no sign of ending (bef= ore I=20 killed it) into a query that ran in a couple of minutes.
 
I always try to avoid procedural = code in=20 such batch updates. If it can be encoded into one insert then it is usually= =20 better, It just depends on what you are doing.
 
 
----- Original Message -----
From:=20 Richard Sydney-Smith
To: pgsql-sql@postgresql.org
Sent: Thursday, February 05= , 2004=20 10:48 AM
Subject: [SQL] Slow sub-sel= ects,=20 max and count(*)

I have a procedure ( included below= with=20 table definition) to import end of day quotes into a table=20 fsechist.
 
The issue is with the speed ( or lackof = it) that=20 the procedure proceeds. Apparently it is the subselects that are the wors= t=20 issue and I have tried to replace these. Also max() and count() refuse to= use=20 indexes. I have replaced max() with (select ... as mx from ... order by .= ..=20 desc limit 1) and this is much faster but appear to have to avoid count()= as I=20 can not find a way to point this at an index.
 
Please, I am sure that there is a better= way to=20 do this. And 5 to 7 minutes to insert a day is really too slow. Each days= =20 import table contains about 3200 records. Total table size for fsechist i= s=20 about 2.5 million records.
 
 
If you could point me in the right direc= tion here=20 it would be much appreciated.
 
thanks
 
Richard Sydney-Smith
 
 
----------------------------------------------------------------= ----------------------------------------
 
CREATE OR REPLACE FUNCTION=20 public.import_sharedata(varchar)
  RETURNS bool AS
'
 
declare
   filnam alias for= =20 $1;
   alldone bool :=3D true;
   /* cnt intege= r :=3D 0;=20 */  /* count of number inserted not used */
 
begin
/*
   Imports a te= xt file=20 containing end of day price quotes
   filnam should be in do= uble=20 backslash format e.g. c:\\data\\sharequotes\\quotes.dat
 =20
   Ensures that it only brings in quotes for companies we h= ave=20 listed in our database
   and ensures only the latest copy o= f the=20 quote is kept for each day
 
   Import table has 7 columns = in CSV=20 tab delimited ascii format.
      EzyChart fo= rmat=20 has prices in cents, 
      METASTOCK i= n=20 dollars
      MetaStock +100 has the volume= =20 divided by 100.
  
   Depending on your import= data=20 source you may need to process the temp_shareprices so that the=20
   values are in Dollars , not cents and the volume is per = unit=20 not per 100 units.
 
   Present calculation is for = EzyChart=20 format.
 
   Both tables  have to h= ave a 4=20 digit year yyyymmdd
 
   Warning : If you have the = wrong=20 format this procedure will import garbage into your database !!!
 
*/
 
  perform=20 drop_table(\'temp_shareprices\');
 
  create table=20 temp_shareprices(
    ticker char(10)=20 null,
    dte char(8),
    v1=20 numeric(19,4) not null default 0,
    v2 numeric(19,4) = not=20 null default 0,
    v3 numeric(19,4) not null default= =20 0,
    v4 numeric(19,4) not null default=20 0,
    v5 numeric(19,4) not null default=20 0
    );
  execute \'copy temp_shareprices from= =20 \'||quote_literal(filnam)||\' delimiter \'||quote_literal(\',\');
<= /DIV>
 

-- extend yr to 4 digits, alter quotes to dollars
 
  update temp_shareprices set dte =3D=20 \'20\'||dte,v1=3Dv1/100,v2=3Dv2/100,v3=3Dv3/100,v4=3Dv4/100;
 
-- add in any new company tickers
insert into fsecurity (sec_cde)= =20 select ticker from temp_shareprices where length(trim(ticker))=3D3 and ti= cker=20 not in ( select sec_cde from fsecurity);
 
-- =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
-- remove rows from temp_shareprices where we do not have a correspo= nding=20 security
-- using sub-select is slow
-- delete from temp_shareprices where= =20 ticker not in (select sec_cde from fsecurity);
-- can just delete ones where length<>3 as insert above ensure= s all=20 length 3 are inserted. All ASX codes are 3char long
delete from=20 temp_shareprices where length(trim(ticker))<>3;
 
-- Attempt to remove without limiting to length and not using sub se= lect=20 does not work
--update temp_shareprices set ticker =3D s.sec_cde from= =20 temp_shareprices t left outer join fsecurity s on t.ticker =3D=20 s.sec_cde;
--delete from temp_shareprices where ticker is null;
-- =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D = ;

-- put indexes on temp table
CREATE INDEX temphist_tick ON=20 temp_shareprices  USING btree  (ticker);
CREATE INDEX=20 temphist_dte  ON temp_shareprices  USING btree  (dte);
 
 
-- =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D = ;
-- Scan the input table=20 and delete as appropriate from fsechist
 
-- Using sub select to remove any pre-existing records in fsechist v= ery=20 slow
-- delete from fsechist where hist_q in (select hist_q from fsech= ist,=20 temp_shareprices where hist_tick =3D ticker and dte =3D hist_date);
 
-- Tried to replace subselect
update fsechist set hist_tick =3D = \'@@\'=20 from fsechist h, temp_shareprices t where t.ticker =3D h.hist_tick and=20 h.hist_date =3D t.dte;
delete from fsechist where hist_tick =3D \'@@\'= ;
-- =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D = ;
 
-- insert newvalues
insert into=20 fsechist(hist_tick,hist_date,hist_v1,hist_v2,hist_v3,hist_v4,hist_vol)=20 select   ticker,dte,v1,v2,v3,v4,v5 from temp_shareprices;
 
-- count the number inserted
-- select   count(*) into = cnt=20 from temp_shareprices;
 
-- perform  drop_table(\'temp_shareprices\');
 
return alldone;
 
end;'
  LANGUAGE 'plpgsql' VOLATILE;
 
 
Note : drop_table is a simple procedure = that=20 drops a procedure by name iff it exists.
 
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
Definition of Fsechist
 
CREATE TABLE public.fsechist
(
&nb= sp;=20 hist_q int4 NOT NULL DEFAULT nextval('hist_q'::text),
  hist_v1= =20 numeric(19,4),
  hist_v2 numeric(19,4),
  hist_v3=20 numeric(19,4),
  hist_v4 numeric(19,4),
  hist_vol=20 numeric(19,4),
  hist_deltar numeric(19,4),
  hist_deltag= =20 numeric(19,4),
  hist_date char(8),
  hist_tick=20 char(10),
  hist_announce char(1),
  CONSTRAINT fsechist_= pkey=20 PRIMARY KEY (hist_q)
) WITH OIDS;
 
CREATE INDEX fsechist_date  ON=20 public.fsechist  USING btree  (hist_date);
 
CREATE UNIQUE INDEX fsechist_q  ON= =20 public.fsechist  USING btree  (hist_q);
CREATE INDEX fsechist_tick  ON=20 public.fsechist  USING btree  (hist_tick);

------=_NextPart_000_0070_01C3EBFE.9A2F0860--