Query takes much to long, is there a work around?

From: "Toon van Doorn" <tvdoorn(at)reto(dot)nl>
To: <pgsql-php(at)postgresql(dot)org>
Subject: Query takes much to long, is there a work around?
Date: 2005-10-04 11:55:16
Message-ID: 20051004115514.A9ECED941F@svr1.postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-php

Hello all,

I made a mailing system where i log al the hits.

The query is dynamic, now there is this mailing with 16 links to log.
Here is the query to get out the data.
#########################
SELECT *,
(SELECT count(linkcode) FROM linklog WHERE linklog.mailcode='24120256' AND
linkcode='1012' AND linklog.adrescode=adreslog.alogid) as Homepage,
(SELECT count(linkcode) FROM linklog WHERE linklog.mailcode='24120256' AND
linkcode='1234' AND linklog.adrescode=adreslog.alogid) as Mickey,
(SELECT count(linkcode) FROM linklog WHERE linklog.mailcode='24120256' AND
linkcode='2345' AND linklog.adrescode=adreslog.alogid) as Winnie,
(SELECT count(linkcode) FROM linklog WHERE linklog.mailcode='24120256' AND
linkcode='3010' AND linklog.adrescode=adreslog.alogid) as Kd,
(SELECT count(linkcode) FROM linklog WHERE linklog.mailcode='24120256' AND
linkcode='3020' AND linklog.adrescode=adreslog.alogid) as Lleft,
(SELECT count(linkcode) FROM linklog WHERE linklog.mailcode='24120256' AND
linkcode='3030' AND linklog.adrescode=adreslog.alogid) as Ergo,
(SELECT count(linkcode) FROM linklog WHERE linklog.mailcode='24120256' AND
linkcode='3040' AND linklog.adrescode=adreslog.alogid) as Optic,
(SELECT count(linkcode) FROM linklog WHERE linklog.mailcode='24120256' AND
linkcode='3050' AND linklog.adrescode=adreslog.alogid) as Mic,
(SELECT count(linkcode) FROM linklog WHERE linklog.mailcode='24120256' AND
linkcode='3060' AND linklog.adrescode=adreslog.alogid) as Toets,
(SELECT count(linkcode) FROM linklog WHERE linklog.mailcode='24120256' AND
linkcode='3070' AND linklog.adrescode=adreslog.alogid) as Kabel,
(SELECT count(linkcode) FROM linklog WHERE linklog.mailcode='24120256' AND
linkcode='3456' AND linklog.adrescode=adreslog.alogid) as Sponge,
(SELECT count(linkcode) FROM linklog WHERE linklog.mailcode='24120256' AND
linkcode='4567' AND linklog.adrescode=adreslog.alogid) as Cookie,
(SELECT count(linkcode) FROM linklog WHERE linklog.mailcode='24120256' AND
linkcode='5678' AND linklog.adrescode=adreslog.alogid) as Dora,
(SELECT count(linkcode) FROM linklog WHERE linklog.mailcode='24120256' AND
linkcode='6789' AND linklog.adrescode=adreslog.alogid) as Cool,
(SELECT count(linkcode) FROM linklog WHERE linklog.mailcode='24120256' AND
linkcode='7891' AND linklog.adrescode=adreslog.alogid) as Col,
(SELECT count(linkcode) FROM linklog WHERE linklog.mailcode='24120256' AND
linkcode='8912' AND linklog.adrescode=adreslog.alogid) as Headset
FROM adreslog WHERE mailcode='24120256'

3599 rows fetched (71,81 sec)
#######################

This way i get the information of the adres and how often they clicked on
wich link.

The explain on this query gives
51 rows fetched (0,19 sec)

Is there a faster way of selecting the hits on a link per adres?

Every hit is a record in the linklog table

CREATE TABLE "public"."linklog" (
"llogid" BIGSERIAL,
"mailcode" VARCHAR(15),
"linkcode" VARCHAR(15),
"adrescode" BIGINT,
"date" TIMESTAMP(6) WITHOUT TIME ZONE
) WITH OIDS;

This is the table with adresses
CREATE TABLE "public"."adreslog" (
"alogid" SERIAL,
"mailcode" VARCHAR(15),
"naam1" VARCHAR(255),
"naam2" VARCHAR(255),
"naam3" VARCHAR(255),
"adres" VARCHAR(70),
"firstread" TIMESTAMP(6) WITHOUT TIME ZONE,
"lastread" TIMESTAMP(6) WITHOUT TIME ZONE,
"totalread" INTEGER DEFAULT 0
) WITH OIDS;

Kind regards,
Toon van Doorn

Responses

Browse pgsql-php by date

  From Date Subject
Next Message Frank Bax 2005-10-04 12:18:47 Re: Query takes much to long, is there a work around?
Previous Message Daniel Walters 2005-10-04 02:37:53 Re: Trouble with conections between php and postgre