Creating indexes for PostgreSQL database

Questions and answers regarding general SQL and backend databases

Creating indexes for PostgreSQL database

Postby Robert Huber » Mon Jan 21, 2008 6:19 pm

Hi All

I read in the manuals and web about applying indexes to tables for the PostgreSQL db V8.2.5. But I still can't seem to find an overview for that.

Is there a tool which helps in creating indexes for tables? Or do I really have to create each index manually?

PostgreSQL doesn't create indexes for primary keys as Sybase does, is that assumption correct?

Best regards, Robert
Robert Huber
7r gmbh, Switzerland
SAN Developer
www.seven-r.ch
User avatar
Robert Huber
 
Posts: 1239
Joined: Tue Aug 23, 2005 6:52 pm
Location: Schaffhausen, Switzerland

Postby swingman » Mon Jan 21, 2008 11:28 pm

Hi Robert,

I use pgAdminIII to create indexes. I suppose if I had to create a lot of indexes, I would check out the code pgAdmin generates and manually write a script.
Christian Batchelor
Certified Servoy Developer
Batchelor Associates Ltd, London, UK
http://www.batchelorassociates.co.uk

http://www.postgresql.org - The world's most advanced open source database.
User avatar
swingman
 
Posts: 1474
Joined: Wed Oct 01, 2003 10:20 am
Location: London

Re: Creating indexes for PostgreSQL database

Postby ROCLASI » Tue Jan 22, 2008 8:08 am

Hi Robert,
Robert Huber wrote:I read in the manuals and web about applying indexes to tables for the PostgreSQL db V8.2.5. But I still can't seem to find an overview for that.

What kind of overview are you looking for?
This page in the manual has all the information you need for creating an index.

Robert Huber wrote:Is there a tool which helps in creating indexes for tables? Or do I really have to create each index manually?

Like Christian already mentioned you can use PgAdminIII but you can also use any other GUI/CLI tool that is out there.
I like to use DbWrench myself.
Of course you could also use the RawSQL plugin in Servoy.

Robert Huber wrote:PostgreSQL doesn't create indexes for primary keys as Sybase does, is that assumption correct?

Good question. I had to look for it but with a Primary Key constraint you also have an index on it in PostgreSQL. So your assumption is incorrect :)
(TIP: when you use psql you can see all indices of your database with the command \di, use \d indexname to see the details )

Hope this helps.
Robert Ivens
SAN Developer / Servoy Valued Professional / Servoy Certified Developer

ROCLASI Software Solutions / JBS Group, Partner
Mastodon: @roclasi
--
ServoyForge - Building Open Source Software.
PostgreSQL - The world's most advanced open source database.
User avatar
ROCLASI
Servoy Expert
 
Posts: 5438
Joined: Thu Oct 02, 2003 9:49 am
Location: Netherlands/Belgium

Re: Creating indexes for PostgreSQL database

Postby Robert Huber » Tue Jan 22, 2008 10:50 am

Hi Robert

Thanks for the hint to the manual. Saying overview, I meant the various possibilities for creating indexes, not the specific syntax. But it seems there is no help in the form of an analyzing and creation tool in pgAdminIII. Is there one in the DBWrench app?

Creating a primary key constraint like

ALTER TABLE stations ADD PRIMARY KEY (id);

after creating the table with a stmt like

CREATE TABLE stations (altitude int4 , company_id int4 , ..., valid_sts_ht int4 NOT NULL);

does not create a primary key index.
But may be I didn't understand you correctly and you meant something different.

Thanks for your help and best regards, Robert

PS: Did you receive my example where I can't suppress the horizontal slider?
Robert Huber
7r gmbh, Switzerland
SAN Developer
www.seven-r.ch
User avatar
Robert Huber
 
Posts: 1239
Joined: Tue Aug 23, 2005 6:52 pm
Location: Schaffhausen, Switzerland

Re: Creating indexes for PostgreSQL database

Postby ROCLASI » Tue Jan 22, 2008 11:11 am

Robert Huber wrote:Hi Robert

Thanks for the hint to the manual. Saying overview, I meant the various possibilities for creating indexes, not the specific syntax. But it seems there is no help in the form of an analyzing and creation tool in pgAdminIII. Is there one in the DBWrench app?

There is no wizard or something for analyzing if a column needs an index or not like Sybase Central has. Although I believe something like that is planned.
As for creating/managing indices you can use DbWrench and other tools.

Robert Huber wrote:Creating a primary key constraint like

ALTER TABLE stations ADD PRIMARY KEY (id);

after creating the table with a stmt like

CREATE TABLE stations (altitude int4 , company_id int4 , ..., valid_sts_ht int4 NOT NULL);

does not create a primary key index.
But may be I didn't understand you correctly and you meant something different.


It implicitly creates an index for the primary key. I just tested it with psql and PgAdminIII.
The only thing is that PgAdminIII doesn't show the index as such. Psql however does show the index.
Actually psql explicitly tells you it does:
Code: Select all
# ALTER TABLE stations ADD PRIMARY KEY (id);
NOTICE:  ALTER TABLE / ADD PRIMARY KEY will create implicit index "stations_pkey" for table "stations"
ALTER TABLE


(also EXPLAIN SELECT * FROM stations WHERE id=?; will show it uses the index :) )

Robert Huber wrote:PS: Did you receive my example where I can't suppress the horizontal slider?

Nope, did you email it to me or did you use the private mailbox on this forum?


Hope this helps.
Robert Ivens
SAN Developer / Servoy Valued Professional / Servoy Certified Developer

ROCLASI Software Solutions / JBS Group, Partner
Mastodon: @roclasi
--
ServoyForge - Building Open Source Software.
PostgreSQL - The world's most advanced open source database.
User avatar
ROCLASI
Servoy Expert
 
Posts: 5438
Joined: Thu Oct 02, 2003 9:49 am
Location: Netherlands/Belgium

Re: Creating indexes for PostgreSQL database

Postby Robert Huber » Tue Jan 22, 2008 5:53 pm

Hi Robert

With psql, I also see the primary key indexes! Hmmm, have you got any idea why this is NOT shown in pgAdminIII? I don't see any "logic" for that to hide.

Thanks for the tip anyway, one seems to use psql for certain tasks.

ROCLASI wrote:
Robert Huber wrote:PostgreSQL doesn't create indexes for primary keys as Sybase does, is that assumption correct?

Good question. I had to look for it but with a Primary Key constraint you also have an index on it in PostgreSQL. So your assumption is incorrect :)
(TIP: when you use psql you can see all indices of your database with the command \di, use \d indexname to see the details )


I have sent the example to the eMail you defined in your Servoy forum profile. Should I send it to another address?

ROCLASI wrote:
Robert Huber wrote:PS: Did you receive my example where I can't suppress the horizontal slider?

Nope, did you email it to me or did you use the private mailbox on this forum?


Best regards, Robert
Robert Huber
7r gmbh, Switzerland
SAN Developer
www.seven-r.ch
User avatar
Robert Huber
 
Posts: 1239
Joined: Tue Aug 23, 2005 6:52 pm
Location: Schaffhausen, Switzerland

Postby patrick » Tue Jan 22, 2008 7:01 pm

I guess the reason to not show the index is because it is not of any relevance. You cannot delete an index on a primary key, it is always there if you define a primary key, so why should you even bother...
Patrick Ruhsert
Servoy DACH
patrick
 
Posts: 3703
Joined: Wed Jun 11, 2003 10:33 am
Location: Munich, Germany

Postby Robert Huber » Fri Jan 25, 2008 11:33 am

Salut Patrick

I agree with you that it is not needed to show pk indexes, my problem was only that I didn't "see" them being created (automatically) - so, as soon one knows that it's ok. I still don't see any advantage if one doesn't see them in pgAdminIII, after all we are developers so we should know what's in our app .-)

Best regards, Robert

patrick wrote:I guess the reason to not show the index is because it is not of any relevance. You cannot delete an index on a primary key, it is always there if you define a primary key, so why should you even bother...
Robert Huber
7r gmbh, Switzerland
SAN Developer
www.seven-r.ch
User avatar
Robert Huber
 
Posts: 1239
Joined: Tue Aug 23, 2005 6:52 pm
Location: Schaffhausen, Switzerland

Re: Creating indexes for PostgreSQL database

Postby Robert Huber » Fri Jan 25, 2008 11:38 am

Hi Robert

Sent you the example again to the eMail address of your profile.

Thanks, Robert

ROCLASI wrote:Hi Robert

Robert Huber wrote:PS: Did you receive my example where I can't suppress the horizontal slider?

Nope, did you email it to me or did you use the private mailbox on this forum?


Hope this helps.
Robert Huber
7r gmbh, Switzerland
SAN Developer
www.seven-r.ch
User avatar
Robert Huber
 
Posts: 1239
Joined: Tue Aug 23, 2005 6:52 pm
Location: Schaffhausen, Switzerland

Re:

Postby huber » Tue Jul 17, 2012 4:19 pm

Hi Patrick

After quite a while, I can give you a good reason why to bother ;-) After searching for performance bottlenecks in various different areas, I did beside lot's of other trials a REINDEX INDEX of primary keys, with quite stunning results for some tables :-)
That's in our project, this may vary, but now I am quite sure one can and should not forget about primary key index, and for that matter foreign key indexes as well.
Don't know other developers experiences, but would be interesting to hear about them.

Regards, Robert

patrick wrote:I guess the reason to not show the index is because it is not of any relevance. You cannot delete an index on a primary key, it is always there if you define a primary key, so why should you even bother...
Robert Huber
7r AG, Switzerland
SAN Developer
http://www.seven-r.ch
User avatar
huber
 
Posts: 518
Joined: Mon May 14, 2012 11:31 pm

Re: Creating indexes for PostgreSQL database

Postby omar » Tue Jul 17, 2012 11:16 pm

Hi,

I like to use NaviCat Premium as my database design tool. It supports all the major databases, data modeling and lot's more. There's a Windows, a Mac and a Linux version. (I don't have shares :wink: )

The PK for example, on the example_data/customers table is shown like this:

navicat.png
navicat.png (81.82 KiB) Viewed 6458 times


Kind regards,
Intrasoft, Founder
Omar van Galen
omar@intrasoft.nl
+31-(0)6-21234586
Servoy Developer
omar
 
Posts: 377
Joined: Sat Feb 12, 2011 4:51 pm
Location: Intrasoft, The Netherlands


Return to SQL Databases

Who is online

Users browsing this forum: No registered users and 2 guests