我在Galera集群(3台服务器)中有一个MariaDB-tabel subject_message_new
,用于注册用户已阅读邮件。我们有31000
个用户和数百万条消息,因此该表非常大(现在高达150万条记录)。如果更新了消息,则必须删除所有带有$id_message
的读取内容。
用户还可以将邮件复制到其他主题,以便邮件($id_message
)引用原始邮件(id_message_org
),同时还必须删除该连接的原始邮件中的the-read-entry
。
这是我的MySQL表:
Kolom Type Commentaar
id bigint(20) Auto nummering
id_message int(11)
id_subject int(11) NULL
nr_user int(11)
datetime datetime
Indexen
PRIMARY id
INDEX nr_user
INDEX id_message
INDEX id_subject
Foreign keys
id_message subject_message(id_message) CASCADE CASCADE
nr_user users(nr_user) CASCADE CASCADE
id_subject subject(id_subject) CASCADE CASCADE
我有一个PHP函数,在消息发生更改后调用
function message_new($id_message,$id_subject){
global $conn;
mysqli_query($conn,"DELETE FROM subject_message_new WHERE (id_message='$id_message' AND id_subject='$id_subject') OR id_message IN(SELECT id_message FROM subject_message WHERE id_message_org='$id_message')");
}
如果我将子查询拆分为2 x DELETE
- 查询需要2 x 6-12
秒......
此功能(查询)在制作中花费的时间长达6-12秒。 有人可以告诉我如何调整这个吗?还是让它更好用?
答案 0 :(得分:0)
问题在于删除方法中的子查询。将单个语句拆分为两个查询显然会提高性能。
DELETE FROM subject_message_new WHERE id_message ='$ id_message'AND_subject ='$ id_subject';
从subject_message_new删除smn INNER JOIN subject_message sm on smn.id_message = sm.id_message_org;
注意:我们应该尝试在执行选择,更新或删除操作
时更喜欢JOIN而不是子查询
然后在subject_message_new表的id_message和id_subject列中添加索引。并在subject_message中为列id_message_org添加索引。这肯定可以解决问题。
答案 1 :(得分:0)
也许使用多表UPDATE/DELETE
语法重写查询会有所帮助,
它会是这样的:
DELETE subject_message_new.* FROM subject_message_new
LEFT JOIN subject_message
ON (subject_message_new.id_message = subject_message.id_message)
WHERE (subject_message_new.id_message='$id_message'
AND subject_message_new.id_subject='$id_subject')
OR id_message.id_message_org='$id_message';