Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Data Bases > Pgsql Performance > Re: EXPLAIN det...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 3 of 4 Topic 3977 of 4424
Post > Topic >>

Re: EXPLAIN detail

by lists@[EMAIL PROTECTED] (PFC) Apr 9, 2008 at 08:41 PM

> Well, this table has a primary key index on first column called
acctid=20=
=20
> which is an integer; instead the calldate column is a TIMESTAMPTZ and
in=
=20=20
> fact I'm using to do (calldate)::date in the ON clause because since
the=
=20=20
> time part of that column is always different and in the nesting I
have=20=
=20
> to identificate the date is the same...
>
> the other two columns (src and lastdata) are both VARCHAR(80) and
the=20=
=20
> query is this one:

	Tip for getting answers from this list :
	You should just post the output of "\d yourtable" from psql, it would be=
=20=20
quicker than writing a paragraph... Be lazy, lol.

	So, basically if I understand you are doing a self-join on your
table,=20=
=20
you want all rows from the same day, and you're doing something with
the=20=
=20
dates, and...

	Tip for getting answers from this list :
	Explain (in english) what your query actually does, someone might come
up=
=20=20
with a better idea on HOW to do it.

	Snip :

> EXPLAIN ANALYZE
> SELECT
>       (a.calldate)::date,
>       a.src,
>       a.dst,
>       MIN(e.calldate) - a.calldate
> FROM
>     cdr a
>     INNER JOIN cdr e
>     ON ((e.calldate)::date =3D (a.calldate)::date AND e.src =3D a.src
>         AND e.lastdata =3D '/dati/ita/logoutok' AND e.calldate
>=3D=20=20
> a.calldate)
> WHERE
>      (a.calldate)::date =3D '2008-04-09'
>      AND a.src =3D '410'
>      AND substr(a.dst, 1, 4) =3D '*100'
>      AND a.lastdata =3D '/dati/ita/loginok'
> GROUP BY
>       a.calldate, a.src, a.dst

	OK, I assume you have an index on calldate, which is a TIMESTAMPTZ ?
	(in that case, why is it called calldate, and not calltimestamp ?...)

	Bad news, the index is useless for this condition :
	(a.calldate)::date =3D '2008-04-09'
	There, you are asking postgres to scan the entire table, convert
the=20=20
column to date, and test. Bad.

	In order to use the index, you could rewrite it as something like :
	a.calldate >=3D '2008-04-09' AND a.calldate < ('2008-04-09'::DATE +
'1=20=
=20
DAY'::INTERVAL)
	This is a RANGE query (just like BETWEEN) which is index-friendly.

	Personnaly, I wouldn't do it that way : since you use the date (and
not=20=
=20
the time, I presume you only use the time for display purposes) I
would=20=
=20
just store the timestamptz in "calltimestamp" and the date in "calldate",=
=20=20
with a trigger to ensure the date is set to calltimestamp::date every
time=
=20=20
a row is inserted/updated.
	This is better than a function index since you use that column a lot
in=20=
=20
your query, it will be slightly faster, and it will save a lot of=20=20
timestamptz->date casts hence it will save CPU cycles

	Try this last option (separate date column), and repost EXPLAIN
ANALYZE=20=
=20
of your query so it can be optimized further.

	Also, PLEASE don't use substr(), use a.dst LIKE '*100%', look in
the=20=20
manual. LIKE 'foo%' is indexable if you create the proper index.








--=20
Sent via pgsql-performance mailing list (pgsql-performance@[EMAIL PROTECTED]
)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-performance
 




 4 Posts in Topic:
Re: EXPLAIN detail
npuleio@[EMAIL PROTECTED]  2008-04-09 07:44:23 
Re: EXPLAIN detail
craig@[EMAIL PROTECTED]   2008-04-09 23:28:51 
Re: EXPLAIN detail
lists@[EMAIL PROTECTED]   2008-04-09 20:41:29 
Re: EXPLAIN detail
richard.broersma@[EMAIL P  2008-04-09 15:17:38 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Mon Dec 1 7:55:08 CST 2008.