我需要将来自2700万行表的信息与700万行表进行组合并进行一些过滤。
CREATE TABLE event_participation (
place_id int(4),
person_id varchar(12),
event_id varchar(10),
event_description varchar(230),
.... and more fields about that specific participation
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE INDEX IDX_1 ON event_participation (place_id);
CREATE INDEX IDX_2 ON event_participation (person_id);
CREATE INDEX IDX_3 ON event_participation (event_id);
CREATE TABLE person (
person_id varchar(12),
last_name varchar(25),
first_name varchar(20),
middle_name varchar(20),
person_attr1 varchar(20),
...
person_attr50 varchar(20),
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE INDEX IDX_10 ON person (person_id);
CREATE INDEX IDX_11 ON person (person_attr1);
CREATE INDEX IDX_12 ON person (person_attr2);
...
我对查询中使用的所有属性都有索引。
event_participation表中有2700万行,表格中有7百万行。
我需要运行这样的查询:
SELECT person.last_name, person.first_name
FROM event_participation
LEFT JOIN person ON event_participation.person_id = person.person_id
WHERE event_id IN ("event 1", "event 2", "event 3", "event 4",
"event 5", "event 6", "event 7") AND person.person_attr1 = 'A' AND
person.person_attr2 = 'B' AND place_id = 90
GROUP BY event_participation.person_id
HAVING count(event_id) >= 3
解释是:
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: event_participation
type: ref
possible_keys: person_id,event_id,place_id
key: place_id
key_len: 5
ref: const
rows: 6437170
Extra: Using where; Using temporary; Using filesort
*************************** 2. row ***************************
id: 1
select_type: SIMPLE
table: person
type: ref
possible_keys: person_id,person_attr1,person_attr2
key: person_id
key_len: 39
ref: event_participation.person_id
rows: 1
Extra: Using where
我正在寻找参加至少3项活动的活跃人士,并满足其他一些标准。通常我会修复与事件相关的条件,并运行多个查询,仅改变人员属性。
即使对于非常小的子集,这个查询也很慢,所以我寻找替代方法。 我创建了一个缓存表:
CREATE TABLE temp_name (
person_id varchar(12),
PRIMARY KEY (person_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
运行如下查询:
INSERT INTO temp_name ( temp_name )
SELECT DISTINCT event_participation.person_id
FROM event_participation
WHERE event_id IN ("event 1", "event 2", "event 3", "event 4",
"event 5", "event 6", "event 7") AND place_id = 90
GROUP BY event_participation.person_id
HAVING count(event_id) >= 3
然后运行过滤查询,如:
SELECT person.last_name, person.first_name
FROM temp_name LEFT JOIN person ON temp_name.person_id = person.person_id
WHERE person.person_attr1 = 'A' AND person.person_attr2 = 'B'
虽然我可以实现最终查询的当前性能,但是临时表的创建和管理正在扼杀我。任何建议都将受到高度赞赏。
答案 0 :(得分:0)
你能尝试一下吗?
SELECT person.last_name, person.first_name
FROM person pers
INNER JOIN
(SELECT person_id, count(*) as count
FROM event_participation
WHERE event_id IN ("event 1", "event 2", "event 3", "event 4",
"event 5", "event 6", "event 7") AND person.person_attr1 = 'A' AND
person.person_attr1 = 'B' AND place_id = 90
Group by person_id
) as event_count on event_count.person_id = pers.person_id AND event_count.count>2
更新:我忘了向内部查询广告组。
答案 1 :(得分:0)
每个InnoDB表都应该有显式 PRIMARY KEY
。我怀疑person_id
应该是person
的PK。
ALTER TABLE person
DROP INDEX(person),
ADD PRIMARY KEY (person);
这里有什么?...
WHERE person.person_attr1 = 'A'
AND person.person_attr1 = 'B'
是'不可能'。也就是说,你应该没有行。请编辑问题。同时,假设你的其中一个意味着attr2,这个综合指数可能会有很大的帮助:
INDEX(person_attr1, person_attr1, place_id, event_id)
LEFT
有什么理由吗?如果没有,请将其删除,以便可以调查更多优化。