这个查询MySQL的EXISTS()很慢
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']."'
的作品,但它需要高达6秒
因为我猜它会更快,我可以用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
为什么要使用在所有的存在?看起来像一个普通的交叉连接(即使是天生的)会在这里排序 – Najzero 2013-02-22 10:28:10
你的索引是什么? – h2ooooooo 2013-02-22 10:28:10
items table primary = itemID; sites_locations表中的主要auto和locationID是关系; – 2013-02-22 10:32:53