我們有龐大的數據庫MySQL查詢優化,我們有將近兩個湖泊記錄,而我們嘗試使用下面的查詢時間過長帶來的結果爲大型數據庫
更改爲MyQuery
SELECT
count(DISTINCT e.guid) AS total
FROM
elgg_entities e
JOIN elgg_users_entity u
ON e.guid = u.guid
JOIN (SELECT
subm1.*,
s1.string
FROM
elgg_metadata subm1
JOIN elgg_metastrings s1
ON subm1.value_id = s1.id) AS m1
ON e.guid = m1.entity_guid
WHERE
m1.name_id = '332'
AND m1.string LIKE '%96059%'
AND (( e.access_id = -2
AND e.owner_guid IN (SELECT guid_one
FROM elgg_entity_relationships
WHERE relationship = 'friend'
AND guid_two =130)
)
OR
( e.access_id IN (2, 1, 3, 4, 6, 7)
OR (e.owner_guid = 130)
OR ( e.access_id = 0
AND e.owner_guid = 130)
)
AND e.enabled = 'yes'
)
搜索
編輯,我們在循環中有更多的派生查詢,所以我需要關於@DRAPP的更多優化
答案
SELECT count(DISTINCT e.guid) AS total
FROM elgg_entities e
JOIN elgg_users_entity u ON e.guid = u.guid
JOIN (
SELECT subm1 . * , s1.string
FROM elgg_metadata subm1
JOIN elgg_metastrings s1 ON subm1.value_id = s1.id
) AS m1 ON e.guid = m1.entity_guid
JOIN (
SELECT subm2 . * , s2.string
FROM elgg_metadata subm2
JOIN elgg_metastrings s2 ON subm2.value_id = s2.id
) AS m2 ON e.guid = m2.entity_guid
WHERE (
(
subm1.name_id = '332'
AND s1.string LIKE '%10001%'
)
AND (
subm2.name_id = '328'
AND s2.string LIKE '%New York%'
)
)
AND (
(
e.access_id = -2
AND e.owner_guid
IN (
SELECT guid_one
FROM elgg_entity_relationships
WHERE relationship = 'friend'
AND guid_two =2336
)
)
OR (
e.access_id
IN (2, 1)
OR (
e.owner_guid =2336
)
OR (
e.access_id =0
AND e.owner_guid =2336
)
)
AND e.enabled = 'yes'
)
AND (
(
subm1.access_id = -2
AND subm1.owner_guid
IN (
SELECT guid_one
FROM elgg_entity_relationships
WHERE relationship = 'friend'
AND guid_two =2336
)
)
OR (
subm1.access_id
IN (2, 1)
OR (
subm1.owner_guid =2336
)
OR (
subm1.access_id =0
AND subm1.owner_guid =2336
)
)
AND subm1.enabled = 'yes'
)
AND (
(
subm2.access_id = -2
AND subm2.owner_guid
IN (
SELECT guid_one
FROM elgg_entity_relationships
WHERE relationship = 'friend'
AND guid_two =2336
)
)
OR (
subm2.access_id
IN (2, 1)
OR (
subm2.owner_guid =2336
)
OR (
subm2.access_id =0
AND subm2.owner_guid =2336
)
)
AND subm2.enabled = 'yes'
)
感謝
盡你所能,你能描述你在試圖得到什麼,什麼是「近兩個湖記錄」? – DRapp 2012-04-17 18:45:49