Friday, March 4, 2011

Why I love PostgreSQL

What if you have a "users" table, and all the properties for the users are in the "property" table. Would a select like this be efficient in PosgreSQL?

select distinct u.*, p3.value
from users u
join property p1 on u.id = p1.user_id
join property p2 on u.id = p2.user_id
join property p3 on u.id = p3.user_id
where p1.name = 'admin' and p1.value='false'
and p2.name = 'email' and p2.value like '%mah%'
and p3.name = 'name'
order by p3.value;


Surprisingly this is quite OK, since PostgreSQL won't be stupid enough to do the full Cartesian product.



I love you PosgreSQL.

0 comentarii: