我需要从项目列表中选择 50 个满足搜索条件的项目。我还需要他们当前的销售数量。项目列表有超过 100 000 条记录
这就是我现在要做的:
SELECT
items.id,
CONCAT(prod_codes.code,items.item) AS item,
items.price,
sold.count
FROM items
LEFT JOIN prod_codes ON items.prod_code_id=prod_codes.id
LEFT JOIN (
SELECT COUNT(*) AS count,CONCAT(prod_code,part_num) AS part
FROM `sold_items`
WHERE 1
GROUP BY CONCAT(prod_code,part_num)
ORDER BY count DESC
) AS sold ON sold.part=CONCAT(prod_codes.code,items.item)
WHERE active_status=1
AND CONCAT(items.prod_code,items.item) collate utf8_general_ci LIKE '%".$search_part."%'
AND items.description collate utf8_general_ci LIKE '%".$search_desc."%'
ORDER BY items.item
LIMIT 50
但是这个查询需要 20 多秒才能执行。我确信有更好的方法来做到这一点。