【发布时间】:2011-05-19 07:46:06
【问题描述】:
表:
- foreign_id_1
- foreign_id_2
- 整数
- 日期1
- 日期2
- primary(foreign_id_1, foreign_id_2)
查询:delete from table where (foreign_id_1 = ? or foreign_id_2 = ?) and date2 < ?
无日期查询大约需要 40 秒。这太高了:(日期要长得多..
选项有:
create另一个表和insertselect,然后rename使用限制并多次运行查询- 拆分查询以运行
foreign_id_1然后foreign_id_2 - 使用选择然后按单行删除
有没有更快的方法?
mysql> explain select * from compatibility where user_id = 193 or person_id = 193 \G
id: 1
select_type: SIMPLE
table: compatibility
type: index_merge
possible_keys: PRIMARY,compatibility_person_id_user_id
key: PRIMARY,compatibility_person_id_user_id
key_len: 4,4
ref: NULL
rows: 2
Extra: Using union(PRIMARY,compatibility_person_id_user_id); Using where
1 row in set (0.00 sec)
mysql> explain select * from compatibility where (user_id = 193 or person_id = 193) and updated_at < '2010-12-02 22:55:33' \G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: compatibility
type: index_merge
possible_keys: PRIMARY,compatibility_person_id_user_id
key: PRIMARY,compatibility_person_id_user_id
key_len: 4,4
ref: NULL
rows: 2
Extra: Using union(PRIMARY,compatibility_person_id_user_id); Using where
1 row in set (0.00 sec)
【问题讨论】:
-
请在您的查询中发布
EXPLAIN的结果。我的钱是因为索引不足。 -
添加查询说明
-
选项 3 和 4 的组合是一个不错的方法,前提是您的 SELECTS 足够快。 PK删除不会出错。 :)
标签: mysql performance innodb sql-delete large-data