Received: from localhost (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id B7DD8650160 for ; Wed, 12 Nov 2008 15:26:31 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 91205-03 for ; Wed, 12 Nov 2008 15:26:25 -0400 (AST) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from wf-out-1314.google.com (wf-out-1314.google.com [209.85.200.175]) by mail.postgresql.org (Postfix) with ESMTP id 836B464FE69 for ; Wed, 12 Nov 2008 15:26:25 -0400 (AST) Received: by wf-out-1314.google.com with SMTP id 25so543546wfc.28 for ; Wed, 12 Nov 2008 11:26:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:message-id:date:from:to :subject:cc:in-reply-to:mime-version:content-type:references; bh=PXkRVp43YQjqJl9CeaYv5dLJJy6Ok3JTiC1xC2AFj/8=; b=qS4xSaiQW3y8Gga92gvPpOxYK1zOKGnCw3Gtx9nDk2KurI+tEmsFLicc0g4MhFaiHM Raub6WAlQt0T/fdfdX7kNGtYh0kMT64p/UYdbUS+bGzJSnpz55qUufBagothKnKbcK2Q Mu0gnTZ56w/puYXx4c97aGyssOLfTHD6nQN0U= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:to:subject:cc:in-reply-to:mime-version :content-type:references; b=vHLBw3bR/PtXvPcgS9wbwXfuEwBGvI0nuELMVr37pwRHBLwDJCEUNta66eQ6OlYAeM kdZDjbQhFmG3m7PXYOFvbcq7xxbFEqPmMSdgdTGAJnV6lvRFG0zKd6MaTfoAbeymI1oa pASb/MFmqcd1KLQi3QBAOnvodT1JuVFCefovs= Received: by 10.114.192.3 with SMTP id p3mr6309855waf.166.1226517983882; Wed, 12 Nov 2008 11:26:23 -0800 (PST) Received: by 10.114.111.20 with HTTP; Wed, 12 Nov 2008 11:26:23 -0800 (PST) Message-ID: <1d709ecc0811121126k4558db13m88c959188c3a9c0f@mail.gmail.com> Date: Wed, 12 Nov 2008 11:26:23 -0800 From: "Vladimir Sitnikov" To: Andrus Subject: Re: Increasing select max(datecol) from bilkaib where datecol<=date'2008-11-01' and (cr='00' or db='00') speed Cc: pgsql-performance@postgresql.org In-Reply-To: MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_Part_4520_18455259.1226517983868" References: <1d709ecc0811120928n3c183df5kb7b7d50dee6f73ed@mail.gmail.com> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.001 tagged_above=0 required=5 tests=HTML_MESSAGE=0.001 X-Spam-Level: X-Archive-Number: 200811/104 X-Sequence-Number: 31835 ------=_Part_4520_18455259.1226517983868 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline > > This query finds initial balance date befeore given date. If you are not interested in other balances except initial ones (the ones that have '00') the best way is to create partial indices that I have suggested. That will keep size of indices small, while providing good performance (constant response time) > bilkaib table contains several year transactions so it is large. > That is not a problem for the particular case. However, when you evaluate query performance, it really makes sense giving number of rows in each table (is 100K rows a "large" table? what about 10M rows?) and other properties of the data stored in the table (like number of rows that have cr='00') > Alternatively if you create an index on (cr, bilkaib) and one on (db, > bilkaib) then you will be able to use other values in the query too. > That means if you create one index on biklaib (cr, datecol) and another index on (db, datecol) you will be able to improve queries like select greatest( (select max(date) from bilkaib where datecol<=date'2008-11-01' and cr=XXX), (select max(date) from bilkaib where datecol<=date'2008-11-01' and db=YYY)). with arbitrary XXX and YYY. I am not sure if you really want this. > I'm sorry I do'nt understand this. > What does the (cr, bilkaib) syntax mean? I believe that should be read as (cr, datecol). > Should I create two functions indexes and re-write query as Vladimir > suggests or is there better appoach ? I am afraid PostgreSQL is not smart enough to rewrite query with "or" into two separate index scans. There is no way to improve the query significantly without rewriting it. Note: for this case indices on (datecol), (cr) and (db) are not very helpful. Regards, Vladimir Sitnikov ------=_Part_4520_18455259.1226517983868 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline


This query finds initial balance date befeore given date.
If you are not interested in other balances except initial ones (the ones that have '00') the best way is to create partial indices that I have suggested.
That will keep size of indices small, while providing good performance (constant response time)
 
bilkaib table contains several year transactions so it is large.
That is not a problem for the particular case. However, when you evaluate query performance, it really makes sense giving number of rows in each table (is 100K rows a "large" table? what about 10M rows?)  and other properties of the data stored in the table (like number of rows that have cr='00')
 
Alternatively if you create an index on (cr, bilkaib) and one on (db, bilkaib) then you will be able to use other values in the query too.
That means if you create one index on biklaib (cr, datecol) and another index on (db, datecol) you will be able to improve queries like
select greatest(
   (select max(date) from bilkaib where datecol<=date'2008-11-01' and cr=XXX),
   (select max(date) from bilkaib where datecol<=date'2008-11-01' and db=YYY)).
with arbitrary XXX and YYY. I am not sure if you really want this.


I'm sorry I do'nt understand this.
What does the (cr, bilkaib) syntax mean?
I believe that should be read as (cr, datecol).

 
Should I create two functions indexes and re-write query as Vladimir suggests or is there better appoach ?
I am afraid PostgreSQL is not smart enough to rewrite query with "or" into two separate index scans. There is no way to improve the query significantly without rewriting it.

Note:  for this case indices on (datecol), (cr) and (db) are not very helpful.
 
Regards,
Vladimir Sitnikov

------=_Part_4520_18455259.1226517983868--