PostgreSQL Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesPostgreSQL Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old July 2nd, 2009, 11:40 AM
BRH BRH is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2009
Posts: 13 BRH User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 29 m 31 sec
Reputation Power: 0
Question PG cannot use index (combination primary key)

Hi everybody,

I new on this forum and cannot find anything related with this issue.

My problem is following:
I have a table:
CREATE TABLE rights (
userid integer not null REFERENCES user(userid),
fishid integer not null REFERENCES fish(speciesid),
boatid integer not null REFERENCES boat(boatid),
CONSTRAINT rights_pri PRIMARY KEY(userid,fishid,boatid)
);

There are some tens of rows in table.

I like to use table as:
SELECT 1
FROM rights
WHERE userid=107 AND
fishid=64 AND
boatid=2

EXPLAIN shows me, that PG cannot use index:
Seq Scan on rights (cost=0.00..3.47 rows=1 width=0)

Filter: ((userid = 107) AND (fishid = 64) AND (boatid = 2))

Any ideas why?

--
BRH

Reply With Quote
  #2  
Old July 2nd, 2009, 01:38 PM
pabloj's Avatar
pabloj pabloj is offline
Modding: Oracle MsSQL Firebird
Dev Shed God 7th Plane (8000 - 8499 posts)
 
Join Date: Jun 2001
Location: Outside US
Posts: 8,367 pabloj User rank is Colonel (50000 - 60000 Reputation Level)pabloj User rank is Colonel (50000 - 60000 Reputation Level)pabloj User rank is Colonel (50000 - 60000 Reputation Level)pabloj User rank is Colonel (50000 - 60000 Reputation Level)pabloj User rank is Colonel (50000 - 60000 Reputation Level)pabloj User rank is Colonel (50000 - 60000 Reputation Level)pabloj User rank is Colonel (50000 - 60000 Reputation Level)pabloj User rank is Colonel (50000 - 60000 Reputation Level)pabloj User rank is Colonel (50000 - 60000 Reputation Level)pabloj User rank is Colonel (50000 - 60000 Reputation Level)pabloj User rank is Colonel (50000 - 60000 Reputation Level)pabloj User rank is Colonel (50000 - 60000 Reputation Level) 
Time spent in forums: 3 Months 4 Days 14 h 12 m 34 sec
Reputation Power: 532
First of all try disabling seqscan (see here) and check if the index is picked up, if this is the case then tune your postgresql.conf

Reply With Quote
  #3  
Old July 2nd, 2009, 03:02 PM
shammat shammat is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Oct 2003
Location: Munich, Bavaria
Posts: 1,532 shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 2 Weeks 3 Days 16 h 34 m 21 sec
Reputation Power: 209
Quote:
Originally Posted by BRH
There are some tens of rows in table.
For ten rows PG will always use a sequential scan. An index scan will not pay of for that small number

Edit: removed part of my message. I didn't read the first posting closely enough

Reply With Quote
  #4  
Old July 3rd, 2009, 01:55 PM
BRH BRH is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2009
Posts: 13 BRH User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 29 m 31 sec
Reputation Power: 0
I disabled seqscan with
set enable_seqscan=false;

and it seems to make query to use index:
Index Scan using rigths_pri on rights (cost=0.00..8.27 rows=1 width=0)
Index Cond: ((userid = 107) AND (fishid = 64) AND (boatid = 2))

It is not maybe wise to change to postgresql.conf, because there is 12 other databases on same server.

What it the reason for this seqscan?

Reply With Quote
  #5  
Old July 3rd, 2009, 02:14 PM
pabloj's Avatar
pabloj pabloj is offline
Modding: Oracle MsSQL Firebird
Dev Shed God 7th Plane (8000 - 8499 posts)
 
Join Date: Jun 2001
Location: Outside US
Posts: 8,367 pabloj User rank is Colonel (50000 - 60000 Reputation Level)pabloj User rank is Colonel (50000 - 60000 Reputation Level)pabloj User rank is Colonel (50000 - 60000 Reputation Level)pabloj User rank is Colonel (50000 - 60000 Reputation Level)pabloj User rank is Colonel (50000 - 60000 Reputation Level)pabloj User rank is Colonel (50000 - 60000 Reputation Level)pabloj User rank is Colonel (50000 - 60000 Reputation Level)pabloj User rank is Colonel (50000 - 60000 Reputation Level)pabloj User rank is Colonel (50000 - 60000 Reputation Level)pabloj User rank is Colonel (50000 - 60000 Reputation Level)pabloj User rank is Colonel (50000 - 60000 Reputation Level)pabloj User rank is Colonel (50000 - 60000 Reputation Level) 
Time spent in forums: 3 Months 4 Days 14 h 12 m 34 sec
Reputation Power: 532
Check shammat's post

Reply With Quote
  #6  
Old July 3rd, 2009, 02:19 PM
BRH BRH is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2009
Posts: 13 BRH User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 29 m 31 sec
Reputation Power: 0
Yeps I read it. In fact, there is 181 rows in table.

This option, when false, decrease (bigger) query speed from 250 sek to 45 sek.

Anything else I can do?

Reply With Quote
  #7  
Old July 3rd, 2009, 03:51 PM
shammat shammat is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Oct 2003
Location: Munich, Bavaria
Posts: 1,532 shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 2 Weeks 3 Days 16 h 34 m 21 sec
Reputation Power: 209
Quote:
Originally Posted by BRH
Anything else I can do?
Did you analyze your table?

Reply With Quote
  #8  
Old July 6th, 2009, 01:19 AM
BRH BRH is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2009
Posts: 13 BRH User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 29 m 31 sec
Reputation Power: 0
I'm not sure, but I think I have.
How it should be done?

Reply With Quote
  #9  
Old July 6th, 2009, 02:28 AM
shammat shammat is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Oct 2003
Location: Munich, Bavaria
Posts: 1,532 shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 2 Weeks 3 Days 16 h 34 m 21 sec
Reputation Power: 209
Quote:
Originally Posted by BRH
I'm not sure, but I think I have.
How it should be done?
Using ANALYZE
http://www.postgresql.org/docs/current/static/sql-analyze.html

or using VACCUUM ANALYZE
http://www.postgresql.org/docs/current/static/sql-vacuum.html

Reply With Quote
  #10  
Old July 6th, 2009, 03:22 AM
BRH BRH is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2009
Posts: 13 BRH User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 29 m 31 sec
Reputation Power: 0
Bingo!

Thank's shammat!
It seems that there is something, I haven't done by analyze. After running analyze to whole db, running time drops from 260 sek to 10 sek!

With verbose on analyse or vacuum, there are no extra messages than without verbose - why?

Reply With Quote
  #11  
Old July 6th, 2009, 04:28 AM
shammat shammat is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Oct 2003
Location: Munich, Bavaria
Posts: 1,532 shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 2 Weeks 3 Days 16 h 34 m 21 sec
Reputation Power: 209
Quote:
Originally Posted by BRH
It seems that there is something, I haven't done by analyze. After running analyze to whole db, running time drops from 260 sek to 10 sek!
Sounds like you have disabled autovacuum, otherwise autovacuum should have taken care of that.

Which Postgres version are you running?

Reply With Quote
  #12  
Old July 6th, 2009, 04:32 AM
BRH BRH is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2009
Posts: 13 BRH User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 29 m 31 sec
Reputation Power: 0
Yeps Autovacuum has been disabled, but vacuumdb is been running every day.

I'm running on 8.2.6

Reply With Quote
  #13  
Old July 6th, 2009, 04:43 AM
shammat shammat is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Oct 2003
Location: Munich, Bavaria
Posts: 1,532 shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 2 Weeks 3 Days 16 h 34 m 21 sec
Reputation Power: 209
Quote:
Originally Posted by BRH
Yeps Autovacuum has been disabled, but vacuumdb is been running every day.
Then you should check the vacuumdb option maybe, they are not "agressive" enough.

Quote:
I'm running on 8.2.6
autovacuum has been improved since then

You should really think about an upgrade to 8.4

Reply With Quote
  #14  
Old July 6th, 2009, 04:48 AM
BRH BRH is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2009
Posts: 13 BRH User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 29 m 31 sec
Reputation Power: 0
You are right. We already have placed a project to upgrade.

Why verbose are not talking?

Reply With Quote
  #15  
Old July 6th, 2009, 05:03 AM
shammat shammat is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Oct 2003
Location: Munich, Bavaria
Posts: 1,532 shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 2 Weeks 3 Days 16 h 34 m 21 sec
Reputation Power: 209
Quote:
Originally Posted by BRH
Why verbose are not talking?
No idea, maybe a bug in 8.2 or something else. You might want to take that question to the PG mailing list.

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesPostgreSQL Help > PG cannot use index (combination primary key)


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump




 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 




© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
Stay green...Green IT