我远离 SQL 大师,我正在尝试执行:
SELECT `apps`.* FROM `apps`
INNER JOIN `similars`
ON (`apps`.id = `similars`.similar_app_id OR `apps`.id = `similars`.app_id)
WHERE (`similars`.app_id = 542
OR `similars`.similar_app_id = 542)
AND apps.id <> 542
ORDER BY field(`similars`.app_id, 542) desc LIMIT 6
order by 比没有 order by 慢 20 倍。
explain extended
SELECT DISTINCT `apps`.*
FROM `apps`
INNER JOIN `similars`
ON (`apps`.id = `similars`.similar_app_id
OR `apps`.id = `similars`.app_id)
WHERE (`similars`.app_id = 542
OR `similars`.similar_app_id = 542) AND apps.id <> 542
ORDER BY `similars`.app_id - 542 desc
给我 :
+----+-------------+----------+-------------+-- -------------------------------------------------- ------------+------------------------------------------------+- --------+------+--------+----------+--------------- -------------------------------------------------- ---------------------------------------------+ | 编号 | 选择类型 | 表| 类型 | 可能的键 | 关键 | key_len | 参考 | 行 | 过滤 | 额外 | +----+-------------+----------+-------------+-- -------------------------------------------------- ------------+------------------------------------------------+- --------+------+--------+----------+--------------- -------------------------------------------------- ---------------------------------------------+ | 1 | 简单 | 相似| 索引合并 | index_app_id_and_similar_app_id,index_app_id,index_similar_app_id | index_app_id,index_similar_app_id | 5,5 | 空 | 241 | 100.00 | 使用 union(index_app_id,index_similar_app_id); 使用哪里;使用临时的;使用文件排序 | | 1 | 简单 | 应用程序 | 范围 | 初级 | 初级 | 4 | 空 | 21493 | 100.00 | 使用哪里;使用连接缓冲区 | +----+-------------+----------+-------------+-- -------------------------------------------------- ------------+------------------------------------------------+- --------+------+--------+----------+--------------- -------------------------------------------------- ---------------------------------------------+
我已经尝试了 app_id、similar_app_id 上的所有索引组合以及它们的组合。
任何提示或技巧?
谢谢