Question

I have a table with entity-attribute-value structure. As an example, as entities I can have different countries. I can have the following attributes: "located in", "has border with", "capital".

Then I want to find all those countries which are "located in Asia" and "has border with Russia". The straightforward way to do that is to join the table with itself using entities are the column for joining and then to use where.

However, if I have 20 rows where Russia in in the entity-column, than in the joint table I will have 20*20=400 rows with Russia as the entity. And it is so for every country. So, the joint table going to be huge.

Will it be not more efficient to use the original table to extract all countries which are located in Asia, then to extract all countries which have border with Russia and then to use those elements which are in both sets of countries?

Was it helpful?

Solution

You shouldn't end up having a huge number of records so this should work

SELECT  a.entity,
    a.located_in,
    a.border
FROM    my_table a
WHERE   a.border in (SELECT b.entity FROM my_table b WHERE b.entity = 'RUSSIA' )
AND     a.located_in = 'ASIA'

OTHER TIPS

You are confusing join with Cartesian product. There could never be more rows in the join then there are in the actual data, the only thing being altered is which elements/rows are taken.

So if you have 20 Russian rows, the table resulting from the join could never have more than 20 Russian entries.

The operation you suggest using is exactly what a join does. Just make sure you have the appropriate indices and let MySQL do the rest.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top