我有以下查询逐一执行
delete FROM secure_dl_ipmap WHERE timestamp< (NOW() - INTERVAL {$ days} DAY)
SELECT null FROM secure_dl_ddown d WHERE d.id = id_file
删除FROM secure_dl_ddown WHERE id NOT IN(SELECT i.id_file FROM secure_dl_ipmap i)
secure_dl_ddown 表
没有索引。 id,actuallink,whoreferred是主要的
大约100k行;
secure_dl_ipmap 表
没有索引。 id& refer设置为Primary
大约100k行;
您是否有任何想法如何更改查询以及是否需要索引表格?
答案 0 :(得分:3)
在BTREE
上添加secure_dl_ipmap.timestamp
索引,在HASH
上添加secure_dl_ddown.id
索引:
ALTER TABLE secure_dl_ipmap ADD INDEX idIdx USING BTREE (timestamp);
ALTER TABLE secure_dl_ddown ADD INDEX idIdx USING HASH (id);
背景是BTREE
索引最适合用于范围查询(例如你的"低于")。 HASH
指数快速上涨"相等"选择器。
编辑
为了加快DELETE
次操作,您可以按照here DELETE QUICK FROM ...
使用If you are going to delete many rows from a table, it might be faster
to use DELETE QUICK followed by OPTIMIZE TABLE.
This rebuilds the index rather than performing many index block
merge operations.
:
DELETE secure_dl_ddown
FROM secure_dl_ddown
LEFT JOIN secure_dl_ipmap ON secure_dl_ddown.id=secure_dl_ipmap.id_file
WHERE secure_dl_ipmap.id_file IS NULL
<强> EDIT2 强>
在同一页面上找到提示后试试这个:
{{1}}
(删除t1中存在的t2中没有匹配的行,使用LEFT JOIN)