For example, for products and sales, a product may have many sales and a sales may response for many products.
And sometimes I need to get all sales of a particular product when I need all products of a sales in other times.
So I make two design the mapping relation in postgresql(I omit sales table in both two cases since they are nothing different):
1. Use a productXsales table:
--------+---------+-------------------------------------------------------
Indexes:
--------+---------+-----------
Indexes:
2. User ARRAY and GIN INDEX in product table:
--------+-----------+------------------------------------------------------
Indexes:
I make same test data for the two alternatives:
So the rows count should be:
test=# SELECT count(1) from product_;
-------
(1 row)
test=# SELECT count(1) from p_x_s;
-------
(1 row)
test=# SELECT count(1) from product;
-------
(1 row)
Then start performance test.
Query and Results:
test=# SELECT id, name from product_, p_x_s where id = p_id and s_id = 100;
-----+------------
(3 rows)
test=# SELECT id, name from product where 100 = any(sals);
-----+------------
(3 rows)
test=# SELECT id, name from product where '{100}' <@ sals;