1

这个查询

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,因为我猜它会更快

这是我尝试过的,但它返回所有结果,而不仅仅是在s 表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
4

2 回答 2

0

不确定您是否在第二个查询中对表sites_categories进行了联接。

于 2013-02-22T10:33:52.123 回答
0

INNER JOIN两个表中至少有一个匹配项时,关键字返回行。

关键字返回左表 (table_name1) 中的LEFT JOIN所有行,即使右表 (table_name2) 中没有匹配项。

这就是您无法获得价值的区别

检查此链接

于 2013-02-22T13:47:02.350 回答