我有 2 个组,我试图找到它们的交集(需要 2 列匹配),我发现加入 2 个临时表的性能比仅用一个临时表加入原始表的性能要慢 50 倍。这对我来说毫无意义,所以也许有人可以启发我?
这是我编写 2 个临时表版本的方式:
CREATE TEMPORARY TABLE attendees (
event_id SMALLINT(5) UNSIGNED,
person_id INT(10) UNSIGNED NOT NULL,
KEY(event_id),
KEY(person_id)
);
INSERT INTO attendees (event_id, person_id)
SELECT event_id, person_id
FROM attendance WHERE year=2013
GROUP BY event_id, person_id;
CREATE TEMPORARY TABLE invitees (
event_id SMALLINT(5) UNSIGNED,
person_id INT(10) UNSIGNED NOT NULL,
KEY(event_id),
KEY(person_id)
);
INSERT INTO invitees (event_id, person_id)
SELECT event_id, person_id
FROM invitations WHERE year=2013
GROUP BY event_id, person_id;
SELECT i.event_id, COUNT(DISTINCT i.person_id) …Run Code Online (Sandbox Code Playgroud)