我有一个MySQL
查询非常简单,但运行速度很慢。我有两个表corporate_main_member
和corporate_main_corporate_membership
,我正在运行以下查询:
SELECT m.gender FROM corporate_main_member m, corporate_main_corporate_membership cm where m.id = cm.FK_member_ID and cm.FK_corporation_id = 6
如果我不包含第二个条件(cm.FK_corporation_id = 6)
,则查询运行正常....
答案 0 :(得分:1)
在查询中使用JOIN
关键字并在FK_corporation_id上创建索引。
SELECT m.gender
FROM corporate_main_member m
INNER JOIN corporate_main_corporate_membership cm
ON m.id = cm.fk_member_id
WHERE cm.fk_corporation_id = 6
答案 1 :(得分:1)
这是上述查询的更有效形式。
SELECT
m.gender
FROM corporate_main_member m,
corporate_main_corporate_membership cm on(m.id = cm.FK_member_ID)
where cm.FK_corporation_id = 6
虽然您可以在corporate_main_corporate_membership上创建索引。
答案 2 :(得分:0)
SELECT
m.gender
FROM corporate_main_member AS m
LEFT JOIN corporate_main_corporate_membership AS cm
ON m.id = cm.FK_member_ID
AND cm.FK_corporation_id = 6