Received: from makus.postgresql.org (makus.postgresql.org [98.129.198.125]) by mail.postgresql.org (Postfix) with ESMTP id 65CC69ED582 for ; Wed, 7 Dec 2011 13:35:16 -0400 (AST) Received: from etc.kandalaya.org ([83.143.86.98] helo=images.kandalaya.org) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1RYLOl-0000nI-Bg for pgsql-sql@postgresql.org; Wed, 07 Dec 2011 17:35:16 +0000 Received: from mail.linux-delhi.org ([120.56.167.51]) by images.kandalaya.org (8.14.3/8.14.3/Debian-9) with ESMTP id pB7HYoUr010631 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Wed, 7 Dec 2011 23:04:53 +0530 Received: from mail.linux-delhi.org (raju@localhost [127.0.0.1]) by mail.linux-delhi.org (8.14.4/8.14.4/Debian-2) with ESMTP id pB7HYk7l020750 for ; Wed, 7 Dec 2011 23:04:47 +0530 From: "Raj Mathur (=?utf-8?b?4KSw4KS+4KSc?= =?utf-8?b?IOCkruCkvuCkpeClgeCksA==?=)" Reply-To: raju@linux-delhi.org Organization: Kandalaya To: pgsql-sql@postgresql.org Subject: Re: Correlating Asterisk CDRs Date: Wed, 7 Dec 2011 23:04:45 +0530 User-Agent: KMail/1.13.7 (Linux/3.0.0-1-686-pae; KDE/4.6.5; i686; ; ) References: <201112072004.29661.raju@linux-delhi.org> In-Reply-To: MIME-Version: 1.0 Content-Type: Text/Plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <201112072304.45631.raju@linux-delhi.org> X-Spam-Status: No, score=-7.4 required=5.0 tests=AWL,BAYES_00, DNS_FROM_RFC_BOGUSMX, LOCAL_FROM_RAJU, RCVD_IN_PBL, RCVD_IN_SORBS_WEB, RDNS_NONE autolearn=ham version=3.2.5 X-Spam-Checker-Version: SpamAssassin 3.2.5 (2008-06-10) on images.kandalaya.org X-Virus-Scanned: clamav-milter 0.95.2 at images.kandalaya.org X-Virus-Status: Clean X-Pg-Spam-Score: -1.1 (-) X-Archive-Number: 201112/18 X-Sequence-Number: 36079 On Wednesday 07 Dec 2011, Brent Dombrowski wrote: > 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: > > 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=20 PLAN > >=20 > > ------------------------------------------------------------------- > > ------------------------------------------------------------------- > > ------- Limit (cost=3D46782.15..46782.40 rows=3D100 width=3D109) (actu= al > > time=3D4077.866..4078.054 rows=3D100 loops=3D1) > >=20 > > -> Sort (cost=3D46782.15..46785.33 rows=3D1272 width=3D109) (actual > > time=3D4077.863..4077.926 > >=20 > > rows=3D100 loops=3D1) > >=20 > > 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) > >=20 > > Merge Cond: (((cdr.clid)::text =3D (cdr2.clid)::text) > > AND ((cdr.dst)::text =3D > >=20 > > "substring"((cdr2.dst)::text, 4))) > >=20 > > Join Filter: (cdr2.calldate >=3D cdr.calldate) > > -> Index Scan using ick1 on cdr=20 > > (cost=3D0.00..34667.86 rows=3D208798 > >=20 > > width=3D43) (actual time=3D0.022..434.246 rows=3D208798 loops=3D1) > >=20 > > -> Index Scan using i2k1 on cdr2=20 > > (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 > 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. Seen. At the moment (and in the foreseeable future) the query is going=20 to be run with a condition of the form "cdr.calldate >=3D=20 date_trunc('day', now())", so the set of matches from cdr2 will remain=20 relatively constant. However, you're right, the scope of cdr2 calldates also ought to be time=20 limited. Have added an "and cdr2.calldate < cdr.calldate + interval '1=20 day'" to the query. Thanks. Regards, =2D- Raj =2D-=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