X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id CD9CB5333A for ; Sun, 3 Apr 2005 13:38:50 +0100 (BST) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 44842-07 for ; Sun, 3 Apr 2005 12:38:41 +0000 (GMT) Received: from mx5.net.nih.gov (mailfwd.nih.gov [165.112.130.36]) by svr1.postgresql.org (Postfix) with ESMTP id C4C495294C for ; Sun, 3 Apr 2005 13:38:40 +0100 (BST) Received: from mx5.net.nih.gov (localhost [127.0.0.1]) by mx5.net.nih.gov (8.12.10/8.11.6) with ESMTP id j33Ccgaq017941 for ; Sun, 3 Apr 2005 08:38:43 -0400 Received: from WATSON (vpn-165-112-10-53.net.nih.gov [165.112.10.53]) by mx5.net.nih.gov (8.12.10/8.11.6) with SMTP id j33CcfKe017928; Sun, 3 Apr 2005 08:38:42 -0400 Message-ID: <000801c5384a$115fbef0$5179f345@WATSON> From: "Sean Davis" To: "_moray" , References: Subject: Re: a very big table Date: Sun, 3 Apr 2005 08:38:41 -0400 MIME-Version: 1.0 Content-Type: text/plain; format=flowed; charset="iso-8859-1"; reply-type=response Content-Transfer-Encoding: 7bit X-Priority: 3 X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook Express 6.00.2900.2180 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180 X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.096 tagged_above=0 required=5 tests=AWL X-Spam-Level: X-Archive-Number: 200504/24 X-Sequence-Number: 20917 ----- Original Message ----- From: "_moray" To: Sent: Tuesday, March 29, 2005 12:25 PM Subject: [SQL] a very big table > > hullo all, > > I have a problem with a table containing a lot of data. > > referred tables "inserzionista" and "pubblicazioni" (referenced 2 times) > have resp. 1909 tuples and 8300 tuples, while this one 54942. > > now the problem is that it is slow, also a simple "select * from > pubblicita". (it takes 5-6 seconds on my P4@1,6Ghz laptop...) > > I tried using some indexes, but the main problem is that I am using a php > script to access the data that builds the query according to user input. Generally, you need to have an index for any column that will appear in a 'where' clause or be referenced as a foreign key. The referencing columns should be declared "unique" or "primary key" and will also then be indexed. So, any column that is on the left or right of an '=' sign in a join or a 'where' clause should probably be indexed. There are exceptions, but that is the general rule. Also, after you make your indices, you need to remember to vacuum analyze. > As you can see it is a quite heavy query...but also with simple queries: > > =========== > cioe2=# explain SELECT * from pubblicita; > QUERY PLAN > ------------------------------------------------------------------- > Seq Scan on pubblicita (cost=0.00..2863.42 rows=54942 width=325) > (1 row) > > cioe2=# explain SELECT * from pubblicita where soggetto ilike 'a%'; > QUERY PLAN > ------------------------------------------------------------------- > Seq Scan on pubblicita (cost=0.00..3000.78 rows=54942 width=325) > Filter: (soggetto ~~* 'a%'::text) > (2 rows) > =========== > > suggestions on how to make things smoother? > (the table is below) > > thnx > > Ciro. > > =========== > create table pubblicita ( > codice_pubblicita bigserial, > codice_inserzionista int NOT NULL, > codice_pagina varchar(2), > codice_materiale varchar(2), > codice_pubblicazione bigint NOT NULL, > > data_registrazione timestamp, > > ripete_da bigint, > soggetto text, > inserto text, > > prezzo numeric, > ns_fattura int, > ns_fattura_data date, > vs_fattura int, > vs_fattura_data date, > > colore bool, > data_prenotazione date, > data_arrivo date, > data_consegna date, > note_prenotazione text, > note_consegna text, > > note text, > > annullata bool DEFAULT 'f', > > PRIMARY KEY (codice_pubblicita), > FOREIGN KEY (codice_pubblicazione) > REFERENCES pubblicazioni > ON UPDATE CASCADE, > FOREIGN KEY (ripete_da) > REFERENCES pubblicazioni (codice_pubblicazione) > ON UPDATE CASCADE, > FOREIGN KEY (codice_inserzionista) > REFERENCES inserzionisti > ON UPDATE CASCADE, > FOREIGN KEY (codice_pagina) > REFERENCES pagine > ON UPDATE CASCADE, > FOREIGN KEY (codice_materiale) > REFERENCES materiali > ON UPDATE CASCADE > ); > =========== > > ---------------------------(end of broadcast)--------------------------- > TIP 9: the planner will ignore your desire to choose an index scan if your > joining column's datatypes do not match >