Merge pull request #10936 from annando/speed-unused-contacts

Speed up query for unused contacts
This commit is contained in:
Hypolite Petovan 2021-10-26 16:12:24 -04:00 committed by GitHub
commit c0db96e84f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -34,15 +34,20 @@ class RemoveUnusedAvatars
{ {
public static function execute() public static function execute()
{ {
$condition = ["`uid` = ? AND NOT `self` AND NOT `nurl` IN (SELECT `nurl` FROM `contact` WHERE `uid` != ?) $sql = "FROM `contact` INNER JOIN `photo` ON `contact`.`id` = `contact-id`
AND `id` IN (SELECT `contact-id` FROM `photo`) AND NOT `id` IN (SELECT `author-id` FROM `post-user`) WHERE `contact`.`uid` = ? AND NOT `self` AND (`photo` != ? OR `thumb` != ? OR `micro` != ?)
AND NOT `id` IN (SELECT `owner-id` FROM `post-user`) AND NOT `id` IN (SELECT `causer-id` FROM `post-user`) AND NOT `nurl` IN (SELECT `nurl` FROM `contact` WHERE `uid` != ?)
AND NOT `id` IN (SELECT `cid` FROM `post-tag`) AND NOT `id` IN (SELECT `contact-id` FROM `post-user`)", 0, 0]; AND NOT `contact`.`id` IN (SELECT `author-id` FROM `post-user`)
AND NOT `contact`.`id` IN (SELECT `owner-id` FROM `post-user`)
AND NOT `contact`.`id` IN (SELECT `causer-id` FROM `post-user`)
AND NOT `contact`.`id` IN (SELECT `cid` FROM `post-tag`)
AND NOT `contact`.`id` IN (SELECT `contact-id` FROM `post-user`);";
$total = DBA::count('contact', $condition); $ret = DBA::fetchFirst("SELECT COUNT(*) AS `total` " . $sql, 0, '', '', '', 0);
$total = $ret['total'] ?? 0;
Logger::notice('Starting removal', ['total' => $total]); Logger::notice('Starting removal', ['total' => $total]);
$count = 0; $count = 0;
$contacts = DBA::select('contact', ['id'], $condition); $contacts = DBA::p("SELECT `contact`.`id` " . $sql, 0, '', '', '', 0);
while ($contact = DBA::fetch($contacts)) { while ($contact = DBA::fetch($contacts)) {
Contact::update(['photo' => '', 'thumb' => '', 'micro' => ''], ['id' => $contact['id']]); Contact::update(['photo' => '', 'thumb' => '', 'micro' => ''], ['id' => $contact['id']]);
Photo::delete(['contact-id' => $contact['id'], 'photo-type' => [Photo::CONTACT_AVATAR, Photo::CONTACT_BANNER]]); Photo::delete(['contact-id' => $contact['id'], 'photo-type' => [Photo::CONTACT_AVATAR, Photo::CONTACT_BANNER]]);