2013-02-22 2 views
1

이 쿼리MySQL은) (

SELECT 
    itemID, 
    locationParentID, 
    locationID, 
    categoryParentID, 
    categoryID, 
    itemTitle, 
    itemDetails, 
    itemAttributes, 
    itemPictures, 
    itemFeatured, 
    itemSpotlight, 
    itemAdded 
FROM items 
WHERE items.siteID IN('".$cfg['site']['siteShares']."') 
    AND EXISTS(SELECT 1 FROM sites_locations sl WHERE items.locationID = sl.locationID AND siteID = '".$cfg['site']['siteID']."' LIMIT 1) 
    AND EXISTS(SELECT 1 FROM sites_categories sc WHERE items.categoryID = sc.categoryID AND siteID = '".$cfg['site']['siteID']."' LIMIT 1) 
    AND itemStatus = '1' 
    AND itemAdded > '".$cfg['timestamp']."' 

작품 정말 느린 존재하지만 그것은 육초

여기에 무엇을 내가 그것을 빨리 될 것 같은데요로 내가 JOIN을 사용하여 할 수

의 개까지 소요 나는 JOIN으로 시도했지만 sites_location 테이블에있는 위치를 가진 항목 대신 모든 결과를 반환합니다.

SELECT 
    itemID, 
    locationParentID, 
    categoryParentID, 
    categoryID, 
    itemTitle, 
    itemDetails, 
    itemAttributes, 
    itemPictures, 
    itemFeatured, 
    itemSpotlight, 
    itemAdded 
FROM items LEFT JOIN sites_locations 
    ON items.locationID = sites_locations.locationID 
    AND sites_locations.siteID = items.siteID 
WHERE items.siteID IN('1,2') AND itemStatus = '1' 
    AND itemAdded > '1356048000' ORDER BY itemID DESC LIMIT 15 
+2

이유가 전혀 존재 사용 하는가를 얻을 수없는 이유의 차이입니다

? 일반 크로스 조인 (심지어 natrual)처럼 여기에 것들을 정렬 것 같습니다 – Najzero

+0

당신의 인덱스는 무엇입니까? – h2ooooooo

+0

items table primary = itemID; sites_locations 테이블 기본 자동 AND 위치 ID는 관계입니다. –

답변

0

확실하지 당신은 테이블에 두 번째 질의에 sites_categories에 가입하고있는 경우.

+0

sites_locations와 함께 작동 시키면 sites_categories에서 동일한 작업을 수행하도록 확장합니다. –

0

INNER JOIN 키워드는 두 테이블에 적어도 하나의 일치가있을 때 행을 반환합니다.

LEFT JOIN 키워드는 오른쪽 테이블 (table_name2)에 일치하는 항목이 없더라도 왼쪽 테이블 (table_name1)의 모든 행을 반환합니다. 당신이 값

Check this link