dbTalk Databases Forums  

implicit index creation on foreign key addition

comp.databases.postgresql comp.databases.postgresql


Discuss implicit index creation on foreign key addition in the comp.databases.postgresql forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Coniglio Sgabbiato
 
Posts: n/a

Default implicit index creation on foreign key addition - 12-17-2009 , 11:27 AM






Hi,
Is there an option in PG to do it?

Reply With Quote
  #2  
Old   
Andreas Kretschmer
 
Posts: n/a

Default Re: implicit index creation on foreign key addition - 12-18-2009 , 12:45 AM






Coniglio Sgabbiato <nobody (AT) nowhere (DOT) net> wrote:
Quote:
Hi,
Is there an option in PG to do it?
No. Only for primary keys.

Andreas
--
Andreas Kretschmer
Linux - weil ich es mir wert bin!
GnuPG-ID 0x3FFF606C http://wwwkeys.de.pgp.net

Reply With Quote
  #3  
Old   
Coniglio Sgabbiato
 
Posts: n/a

Default Re: implicit index creation on foreign key addition - 12-18-2009 , 08:44 AM



Il 18/12/2009 7.45, Andreas Kretschmer ha scritto:
Quote:
Coniglio Sgabbiato<nobody (AT) nowhere (DOT) net> wrote:
Hi,
Is there an option in PG to do it?

No. Only for primary keys.

Andreas
What a shame! Is it possible to request such a feature for PG next
release? Im my opinion it should be active by default (it is expected
that foreign keys will be used in joins besides referential integrity).

Reply With Quote
  #4  
Old   
Thomas Kellerer
 
Posts: n/a

Default Re: implicit index creation on foreign key addition - 12-18-2009 , 08:46 AM



Coniglio Sgabbiato, 18.12.2009 15:44:
Quote:
No. Only for primary keys.

Andreas

What a shame! Is it possible to request such a feature for PG next
release? Im my opinion it should be active by default (it is expected
that foreign keys will be used in joins besides referential integrity).
Do you know any DBMS where FK columns are automatically indexed?

Thomas

Reply With Quote
  #5  
Old   
Coniglio Sgabbiato
 
Posts: n/a

Default Re: implicit index creation on foreign key addition - 12-18-2009 , 09:55 AM



[CUT]

Quote:
Do you know any DBMS where FK columns are automatically indexed?

Thomas

Here:

http://dev.mysql.com/doc/refman/5.1/...nstraints.html

it _seems_ to say that Mysql _could_ do that:

"InnoDB requires indexes on foreign keys and referenced keys so that
foreign key checks can be fast and not require a table scan. In the
referencing table, there must be an index where the foreign key columns
are listed as the first columns in the same order. Such an index is
created on the referencing table automatically if it does not exist.
(This is in contrast to some older versions, in which indexes had to be
created explicitly or the creation of foreign key constraints would
fail.) index_name, if given, is used as described previously."

I tested it but it seems it does not work. I am not so accustomed to
Mysql, so I could did a mistake:

mysql> create table test (id int primary key) engine=innodb;
Query OK, 0 rows affected (0.04 sec)

mysql> create table test2 (id int primary key, fk int references test)
engine=innodb;
Query OK, 0 rows affected (0.05 sec)

mysql> show keys from test2;
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
Quote:
Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation
Cardinality | Sub_part | Packed | Null | Index_type | Comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
test2 | 0 | PRIMARY | 1 | id | A
0 | NULL | NULL | | BTREE | |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
1 row in set (0.00 sec)

Reply With Quote
  #6  
Old   
Andreas Kretschmer
 
Posts: n/a

Default Re: implicit index creation on foreign key addition - 12-18-2009 , 09:57 AM



Coniglio Sgabbiato <nobody (AT) nowhere (DOT) net> wrote:
Quote:
Il 18/12/2009 7.45, Andreas Kretschmer ha scritto:
Coniglio Sgabbiato<nobody (AT) nowhere (DOT) net> wrote:
Hi,
Is there an option in PG to do it?

No. Only for primary keys.

Andreas

What a shame! Is it possible to request such a feature for PG next
release? Im my opinion it should be active by default (it is expected
that foreign keys will be used in joins besides referential integrity).
In short: -1 from me.

No, there are a LOT of use cases where nobody needs such an index,
especially with small refereced tables.


Andreas
--
Really, I'm not out to destroy Microsoft. That will just be a completely
unintentional side effect. (Linus Torvalds)
"If I was god, I would recompile penguin with --enable-fly." (unknown)
Kaufbach, Saxony, Germany, Europe. N 51.05082°, E 13.56889°

Reply With Quote
  #7  
Old   
Marco Mariani
 
Posts: n/a

Default Re: implicit index creation on foreign key addition - 12-18-2009 , 10:05 AM



Coniglio Sgabbiato wrote:


[ talking about foreign keys in mysql ]


Quote:
http://dev.mysql.com/doc/refman/5.1/...nstraints.html

it _seems_ to say that Mysql _could_ do that:
[...]

Quote:
I tested it but it seems it does not work.
Maybe you could wait for a full moon and retry..

Reply With Quote
  #8  
Old   
Coniglio Sgabbiato
 
Posts: n/a

Default Re: implicit index creation on foreign key addition - 12-18-2009 , 10:18 AM



[CUT]
Quote:
In short: -1 from me.

No, there are a LOT of use cases where nobody needs such an index,
especially with small refereced tables.
I meant creation of index in referencing table, not referenced, what do
you think about?

Reply With Quote
  #9  
Old   
Andreas Kretschmer
 
Posts: n/a

Default Re: implicit index creation on foreign key addition - 12-18-2009 , 10:29 AM



Coniglio Sgabbiato <nobody (AT) nowhere (DOT) net> wrote:
Quote:
[CUT]
In short: -1 from me.

No, there are a LOT of use cases where nobody needs such an index,
especially with small refereced tables.

I meant creation of index in referencing table, not referenced, what do
you think about?
Ahhh...

test=# create table "Coniglio Sgabbiato" (id serial primary key);
NOTICE: CREATE TABLE will create implicit sequence "Coniglio Sgabbiato_id_seq" for serial column "Coniglio Sgabbiato.id"
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "Coniglio Sgabbiato_pkey" for table "Coniglio Sgabbiato"
CREATE TABLE

Read the NOTICE - lines careful ...

But:

test=*# create table foobar(i int references "Coniglio Sgabbiato" on delete cascade);
CREATE TABLE




Andreas
--
Really, I'm not out to destroy Microsoft. That will just be a completely
unintentional side effect. (Linus Torvalds)
"If I was god, I would recompile penguin with --enable-fly." (unknown)
Kaufbach, Saxony, Germany, Europe. N 51.05082°, E 13.56889°

Reply With Quote
  #10  
Old   
Marco Mariani
 
Posts: n/a

Default Re: implicit index creation on foreign key addition - 12-18-2009 , 10:39 AM



Coniglio Sgabbiato wrote:

Quote:
In short: -1 from me.

No, there are a LOT of use cases where nobody needs such an index,
especially with small refereced tables.

I meant creation of index in referencing table, not referenced, what do
you think about?

Ok, so I took the time to write a test case.

Can you explain me the effect of the index bigone_dayname_idx, apart
from taking space and slowing insertions?

BTW, most of my foreign keys are never used in joins, are there purely
for referential integrity, and reference small tables.


Quote:
-- create a set of rows with low cardinality

CREATE TABLE week ( dayname VARCHAR PRIMARY KEY, stuff FLOAT );
INSERT INTO week VALUES
('monday', 3.1415926535897931),
('tuesday', 2.7182818284590451),
('wednesday', 1.4142135623730951),
('thurstday', 0.3010299956639812),
('friday', 0.8660254037844386),
('saturday', 0.99627207622074998),
('sunday', 0);

-- and a table that will contain a lot of data, referencing the first one

CREATE TABLE bigone ( pk SERIAL, dayname VARCHAR REFERENCES week );

-- 700k rows

INSERT INTO bigone (dayname)
SELECT dayname FROM (
SELECT * FROM week CROSS JOIN (SELECT * FROM generate_series(1,100000)) numbers
) days
ORDER BY random();

INSERT INTO bigone (dayname) VALUES ((SELECT dayname FROM week ORDER BY RANDOM() LIMIT 1));


now, join them


Quote:
test=# EXPLAIN SELECT pk, stuff FROM bigone natural JOIN week;
QUERY PLAN
---------------------------------------------------------------------
Hash Join (cost=36.10..14089.86 rows=440832 width=12)
Hash Cond: ((bigone.dayname)::text = (week.dayname)::text)
-> Seq Scan on bigone (cost=0.00..7992.32 rows=440832 width=36)
-> Hash (cost=21.60..21.60 rows=1160 width=40)
-> Seq Scan on week (cost=0.00..21.60 rows=1160 width=40)
(5 rows)

create the index you want (and I don't)
Quote:
test=# CREATE INDEX bigone_dayname_idx ON bigone (dayname);
CREATE INDEX
and retry.

Quote:
coges=# EXPLAIN SELECT pk, stuff FROM bigone natural JOIN week;
QUERY PLAN
----------------------------------------------------------------------
Hash Join (cost=36.10..20245.12 rows=700001 width=12)
Hash Cond: ((bigone.dayname)::text = (week.dayname)::text)
-> Seq Scan on bigone (cost=0.00..10584.01 rows=700001 width=12)
-> Hash (cost=21.60..21.60 rows=1160 width=40)
-> Seq Scan on week (cost=0.00..21.60 rows=1160 width=40)

Reply With Quote
Reply




Thread Tools
Display Modes

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



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.