Received: from makus.postgresql.org (makus.postgresql.org [98.129.198.125]) by mail.postgresql.org (Postfix) with ESMTP id 2462F9ED581 for ; Wed, 7 Dec 2011 13:13:28 -0400 (AST) Received: from mail-ey0-f174.google.com ([209.85.215.174]) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1RYL3f-0000Sz-79 for pgsql-sql@postgresql.org; Wed, 07 Dec 2011 17:13:27 +0000 Received: by eaak14 with SMTP id k14so694233eaa.19 for ; Wed, 07 Dec 2011 09:13:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=subject:mime-version:content-type:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to:x-mailer; bh=xdbiQJ08MfezdvPI3k+TB7meXInArPXPY03gsjA012s=; b=kpBj9U4ImJM/BCfvfd+KtMy+3fvoQHfhfkaU2mmVfyKp+kPkD6Ggbs+OmTJ7xdJeXI 4MQKydvqUpIlZ4tCJ9V1nkNCeN1IkLSlzK6IymdbsLSivAvhLOQoZALxWiR0m8N4lgD/ bU1IugnEn8IquKYCby+iSfOg24P9BmJTa1i84= Received: by 10.231.81.147 with SMTP id x19mr4695887ibk.12.1323277993717; Wed, 07 Dec 2011 09:13:13 -0800 (PST) Received: from [10.9.73.100] (173-164-88-9-Oregon.hfc.comcastbusiness.net. [173.164.88.9]) by mx.google.com with ESMTPS id mb4sm4793554igc.1.2011.12.07.09.13.08 (version=TLSv1/SSLv3 cipher=OTHER); Wed, 07 Dec 2011 09:13:11 -0800 (PST) Subject: Re: Correlating Asterisk CDRs Mime-Version: 1.0 (Apple Message framework v1084) Content-Type: text/plain; charset=utf-8 From: Brent Dombrowski In-Reply-To: <201112072004.29661.raju@linux-delhi.org> Date: Wed, 7 Dec 2011 09:13:04 -0800 Cc: "pgsql-sql" Content-Transfer-Encoding: quoted-printable Message-Id: References: <201112072004.29661.raju@linux-delhi.org> To: raju@linux-delhi.org X-Mailer: Apple Mail (2.1084) X-Pg-Spam-Score: -2.6 (--) X-Archive-Number: 201112/17 X-Sequence-Number: 36078 On Dec 7, 2011, at 6:34 AM, Raj Mathur (=E0=A4=B0=E0=A4=BE=E0=A4=9C = =E0=A4=AE=E0=A4=BE=E0=A4=A5=E0=A5=81=E0=A4=B0) wrote: > Hi, >=20 > I'm trying to correlate Call Data Records (CDRs) from two Asterisk > servers, one of which uses the other for telephony. The data is in > the tables cdr and cdr2. With some indexes, the query and explain > result are: >=20 > explain analyse select cdr.calldate, cdr2.calldate, > (cdr2.calldate-cdr.calldate) as rtdur, cdr.clid, cdr.dst, cdr2.src, > cdr2.dst, cdr2.dstchannel, cdr2.lastapp, cdr2.duration, > cdr2.disposition from cdr, cdr2 where cdr2.calldate >=3D cdr.calldate > and cdr.clid=3Dcdr2.clid and cdr.dst=3Dsubstring(cdr2.dst from 4) = order by > cdr.calldate, cdr2.calldate, cdr.clid limit 100; >=20 > QUERY PLAN = =20 > = --------------------------------------------------------------------------= ------------------------------------------------------------------- > Limit (cost=3D46782.15..46782.40 rows=3D100 width=3D109) (actual = time=3D4077.866..4078.054=20 > rows=3D100 loops=3D1) > -> Sort (cost=3D46782.15..46785.33 rows=3D1272 width=3D109) = (actual time=3D4077.863..4077.926=20 > rows=3D100 loops=3D1) > Sort Key: cdr.calldate, cdr2.calldate, cdr.clid > Sort Method: top-N heapsort Memory: 42kB > -> Merge Join (cost=3D2.95..46733.54 rows=3D1272 width=3D109) = (actual=20 > time=3D0.070..3799.546 rows=3D168307 loops=3D1) > Merge Cond: (((cdr.clid)::text =3D (cdr2.clid)::text) = AND ((cdr.dst)::text =3D=20 > "substring"((cdr2.dst)::text, 4))) > Join Filter: (cdr2.calldate >=3D cdr.calldate) > -> Index Scan using ick1 on cdr (cost=3D0.00..34667.86 = rows=3D208798=20 > width=3D43) (actual time=3D0.022..434.246 rows=3D208798 loops=3D1) > -> Index Scan using i2k1 on cdr2 (cost=3D0.00..9960.89 = rows=3D65449 width=3D88)=20 > (actual time=3D0.011..391.599 rows=3D240981 loops=3D1) > Total runtime: 4078.184 ms > (10 rows) >=20 > Is there any way to make this query faster? I already have an index > i2k1 on substring(cdr2.dst from 4), which is being used. >=20 > Application > ----------- >=20 > I'm looking for all caller records in cdr2 that have the same callerid > (clid) and destination (dst) and were started on cdr2 after they were > started on cdr. cdr2.dst is the same as cdr.dst but with a > 3-character prefix. >=20 > Regards, >=20 > -- Raj > --=20 > Raj Mathur || raju@kandalaya.org || GPG: > http://otheronepercent.blogspot.com || http://kandalaya.org || CC68 > It is the mind that moves || http://schizoid.in || D17F >=20 > --=20 > Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) > To make changes to your subscription: > http://www.postgresql.org/mailpref/pgsql-sql You have a non-equi join in there (cdr2.calldate >=3D cdr.calldate). I = would try to get rid of that. It's increasing the number of rows in the = result set and will only get worse as your data set grows. Brent.