删除从InnoDB表5K行与30M行
问题描述:
表的最佳方式:删除从InnoDB表5K行与30M行
- foreign_id_1
- foreign_id_2
- 整数
- DATE1
- DATE2
- 初级(foreign_id_1,foreign_id_2)
查询:delete from table where (foreign_id_1 = ? or foreign_id_2 = ?) and date2 < ?
无日期查询大约需要40秒。这是太高:(随着时间更加长..
的选项有:
create
另一个表和insert
select
,然后rename
使用限制和查询运行多次- 拆分查询运行
foreign_id_1
然后foreign_id_2
- 使用select然后单行删除
有没有更快的方法?
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)
答
现在数据量为40M(+ 33%)并且在迅速增长。所以我开始寻找其他的一些非SQL解决方案。
谢谢。
答
在你的WHERE
使得MySQL的不情愿(如果不是完全拒绝)使用您user_id
和/或person_id
字段的索引(如果有任何一个OR
- 显示CREATE TABLE
将表明,如果有是)。
如果你可以添加索引(或修改现有的,因为我想复合索引的),我可能会添加两个:
ALTER TABLE compatibility
ADD INDEX user_id_updated_at (user_id, updated_at),
ADD INDEX persona_id_updated_at (person_id, updated_at);
相应地,假设行DELETE
没得被原子地删除(即在同一时刻发生)。
DELETE FROM compatibility WHERE user_id = 193 AND updated_at < '2010-12-02 22:55:33';
DELETE FROM compatibility WHERE person_id = 193 AND updated_at < '2010-12-02 22:55:33';
请在查询中发布`EXPLAIN`的结果。我的钱没有足够的索引。 – Blrfl 2010-12-02 18:46:56
添加查询说明 – zhekanax 2010-12-02 19:05:22