tags:

views:

70

answers:

1

Actually i was trying to get data from elgg database based on multiple joins. It generated very big query with lots of JOIN statements and query never respond back.

SELECT distinct e.* from test_entities e 
JOIN test_metadata m1 on e.guid = m1.entity_guid 
JOIN test_metastrings ms1 on ms1.id = m1.name_id 
JOIN test_metastrings mv1 on mv1.id = m1.value_id 
JOIN test_objects_entity obj on e.guid = obj.guid 
JOIN test_metadata m2 on e.guid = m2.entity_guid 
JOIN test_metastrings ms2 on ms2.id = m2.name_id 
JOIN test_metastrings mv2 on mv2.id = m2.value_id 
JOIN test_metadata m3 on e.guid = m3.entity_guid 
JOIN test_metastrings ms3 on ms3.id = m3.name_id 
JOIN test_metastrings mv3 on mv3.id = m3.value_id 
JOIN test_metadata m4 on e.guid = m4.entity_guid 
JOIN test_metastrings ms4 on ms4.id = m4.name_id 
JOIN test_metastrings mv4 on mv4.id = m4.value_id 
JOIN test_metadata m5 on e.guid = m5.entity_guid 
JOIN test_metastrings ms5 on ms5.id = m5.name_id 
JOIN test_metastrings mv5 on mv5.id = m5.value_id 
JOIN test_metadata m6 on e.guid = m6.entity_guid 
JOIN test_metastrings ms6 on ms6.id = m6.name_id 
JOIN test_metastrings mv6 on mv6.id = m6.value_id 

where ms1.string='expire_date' and mv1.string <= 1272565800 and ms2.string='homecity' and 
mv2.string LIKE "%dasf%" and ms3.string='schoolname' and mv3.string LIKE "%asdf%" 

and ms4.string='award_amount' and mv4.string <= 123 and ms5.string='no_of_awards' and mv5.string <= 7 and ms6.string='avg_rating' and mv6.string <= 2 and e.type = 'object' 
and e.subtype = 5 and e.site_guid = 1 and (obj.title like '%asdf%') OR (obj.description like '%asdf%') 

and ( (e.access_id = -2 AND e.owner_guid IN ( SELECT guid_one FROM test_entity_relationships WHERE relationship='friend' AND guid_two=5 )) OR (e.access_id IN (2,1) OR (e.owner_guid = 5) OR ( e.access_id = 0 AND e.owner_guid = 5 ) ) and e.enabled='yes') 

and ( (m1.access_id = -2 AND m1.owner_guid IN ( SELECT guid_one FROM test_entity_relationships WHERE relationship='friend' AND guid_two=5 )) OR (m1.access_id IN (2,1) OR (m1.owner_guid = 5) OR ( m1.access_id = 0 AND m1.owner_guid = 5 ) ) and m1.enabled='yes') 

and ( (m2.access_id = -2 AND m2.owner_guid IN ( SELECT guid_one FROM test_entity_relationships WHERE relationship='friend' AND guid_two=5 )) OR (m2.access_id IN (2,1) OR (m2.owner_guid = 5) OR ( m2.access_id = 0 AND m2.owner_guid = 5 ) ) and m2.enabled='yes') 

and ( (m3.access_id = -2 AND m3.owner_guid IN ( SELECT guid_one FROM test_entity_relationships WHERE relationship='friend' AND guid_two=5 )) OR (m3.access_id IN (2,1) OR (m3.owner_guid = 5) OR ( m3.access_id = 0 AND m3.owner_guid = 5 ) ) and m3.enabled='yes') 

and ( (m4.access_id = -2 AND m4.owner_guid IN ( SELECT guid_one FROM test_entity_relationships WHERE relationship='friend' AND guid_two=5 )) OR (m4.access_id IN (2,1) OR (m4.owner_guid = 5) OR ( m4.access_id = 0 AND m4.owner_guid = 5 ) ) and m4.enabled='yes') 

and ( (m5.access_id = -2 AND m5.owner_guid IN ( SELECT guid_one FROM test_entity_relationships WHERE relationship='friend' AND guid_two=5 )) OR (m5.access_id IN (2,1) OR (m5.owner_guid = 5) OR ( m5.access_id = 0 AND m5.owner_guid = 5 ) ) and m5.enabled='yes') 

and ( (m6.access_id = -2 AND m6.owner_guid IN ( SELECT guid_one FROM test_entity_relationships WHERE relationship='friend' AND guid_two=5 )) OR (m6.access_id IN (2,1) OR (m6.owner_guid = 5) OR ( m6.access_id = 0 AND m6.owner_guid = 5 ) ) and m6.enabled='yes') 

order by obj.title limit 0, 10

this is the query that i am running.

+3  A: 

Asking questions like that typically points to problems in your schema design. It looks like you may be using the Entity Attribute Value pattern.

RedFilter
+1 "if your query is too complicated, you're doing it wrong"
Lo'oris
http://docs.elgg.org/wiki/DatabaseSchema this the schema for the Elgg.
Chetan sharma