<commit_before><?php
/**
 * @file src/Worker/RemoveContact.php
 * @brief Removes orphaned data from deleted contacts
 */
namespace Friendica\Worker;

use Friendica\Database\DBA;
use Friendica\Core\Protocol;
use Friendica\Model\Item;

require_once 'include/dba.php';

class RemoveContact {
	public static function execute($id) {

		// Only delete if the contact is to be deleted
		$condition = ['network' => Protocol::PHANTOM, 'id' => $id];
		$r = DBA::exists('contact', $condition);
		if (!DBA::isResult($r)) {
			return;
		}

		// Now we delete the contact and all depending tables
		$condition = ['contact-id' => $id];
		do {
			$items = Item::select(['id'], $condition, ['limit' => 100]);
			while ($item = Item::fetch($items)) {
				DBA::delete('item', ['id' => $item['id']]);
			}
			DBA::close($items);
		} while (Item::exists($condition));<commit_msg>Improve query speed<commit_after><?php
/**
 * @file src/Worker/RemoveContact.php
 * @brief Removes orphaned data from deleted contacts
 */
namespace Friendica\Worker;

use Friendica\Database\DBA;
use Friendica\Core\Protocol;
use Friendica\Model\Item;

require_once 'include/dba.php';

class RemoveContact {
	public static function execute($id) {

		// Only delete if the contact is to be deleted
		$condition = ['network' => Protocol::PHANTOM, 'id' => $id];
		$contact = DBA::selectFirst('contact', ['uid'], $condition);
		if (!DBA::isResult($contact)) {
			return;
		}

		// Now we delete the contact and all depending tables
		$condition = ['uid' => $contact['uid'], 'contact-id' => $id];
		do {
			$items = Item::select(['id'], $condition, ['limit' => 100]);
			while ($item = Item::fetch($items)) {
				DBA::delete('item', ['id' => $item['id']]);
			}
			DBA::close($items);
		} while (Item::exists($condition));
{'fraction_non_alphanumeric': 0.18658399098083428, 'fraction_numerical': 0.0033821871476888386, 'mean_word_length': 4.395136778115502, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4911442', 'n_tokens_mistral': 626, 'n_tokens_neox': 570, 'n_words': 150}