0

我有一个MySQL非常简单但运行速度非常慢的查询。我有两个表corporate_main_membercorporate_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)时,查询运行正常......

4

3 回答 3

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 
于 2012-06-26T10:13:42.823 回答
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 上创建索引。

于 2012-06-26T10:14:19.533 回答
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
于 2012-06-26T11:53:49.293 回答