[DATABASE] Add explicit indices for all foreign keys

This adds a requirement for all definitions that have foreign keys to also
require indices for all source (local) attributes mentioned in foreign keys.

MariaDB/MySQL creates indices for source attributes automatically, so this
serves as a way to get rid of those automatic indices and create clean explicit
ones instead.

In PostgreSQL, most of the time, indices on the source are necessary to
decrease performance penalty of foreign keys (like in MariaDB), but they aren't
created automatically, so this serves to remove that difference between
PostgreSQL and MariaDB.
This commit is contained in:
Alexei Sorokin 2020-07-31 16:36:40 +03:00
parent 0bfa747382
commit 92e8c40c55
22 changed files with 142 additions and 58 deletions

View File

@ -49,6 +49,9 @@ class Confirm_address extends Managed_DataObject
'foreign keys' => array(
'confirm_address_user_id_fkey' => array('user', array('user_id' => 'id')),
),
'indexes' => array(
'confirm_address_user_id_idx' => array('user_id'),
),
);
}

View File

@ -52,6 +52,9 @@ class File_redirection extends Managed_DataObject
'foreign keys' => array(
'file_redirection_file_id_fkey' => array('file', array('file_id' => 'id')),
),
'indexes' => array(
'file_redirection_file_id_idx' => array('file_id'),
),
);
}

View File

@ -63,6 +63,10 @@ class Foreign_link extends Managed_DataObject
'foreign_link_foreign_id_service_fkey' => array('foreign_user', array('foreign_id' => 'id', 'service' => 'service')),
'foreign_link_service_fkey' => array('foreign_service', array('service' => 'id')),
),
'indexes' => array(
'foreign_link_foreign_id_service_idx' => array('foreign_id', 'service'),
'foreign_link_service_idx' => array('service'),
),
);
}

View File

@ -51,6 +51,8 @@ class Foreign_subscription extends Managed_DataObject
'foreign_subscription_subscribed_service_fkey' => array('foreign_user', array('subscribed' => 'id', 'service' => 'service')),
),
'indexes' => array(
'foreign_subscription_subscriber_service_idx' => array('subscriber', 'service'),
'foreign_subscription_subscribed_service_idx' => array('subscribed', 'service'),
'foreign_subscription_service_subscribed_idx' => array('service', 'subscribed'),
),
);

View File

@ -54,6 +54,9 @@ class Foreign_user extends Managed_DataObject
'unique keys' => array(
'foreign_user_uri_key' => array('uri'),
),
'indexes' => array(
'foreign_user_service_idx' => array('service'),
),
);
}

View File

@ -54,6 +54,10 @@ class Group_block extends Managed_DataObject
'group_block_blocked_fkey' => array('profile', array('blocked' => 'id')),
'group_block_blocker_fkey' => array('user', array('blocker' => 'id')),
),
'indexes' => array(
'group_block_blocked_idx' => array('blocked'),
'group_block_blocker_idx' => array('blocker'),
),
);
}

View File

@ -200,6 +200,10 @@ class Oauth_application extends Managed_DataObject
'oauth_application_owner_fkey' => array('profile', array('owner' => 'id')), // Are remote users allowed to create oauth application records?
'oauth_application_consumer_key_fkey' => array('consumer', array('consumer_key' => 'consumer_key')),
),
'indexes' => array(
'oauth_application_owner_idx' => array('owner'),
'oauth_application_consumer_key_idx' => array('consumer_key'),
),
);
}
}

View File

@ -52,6 +52,9 @@ class Oauth_application_user extends Managed_DataObject
'oauth_application_user_profile_id_fkey' => array('profile', array('profile_id' => 'id')),
'oauth_application_user_application_id_fkey' => array('oauth_application', array('application_id' => 'id')),
),
'indexes' => array(
'oauth_application_user_application_id_idx' => array('application_id'),
),
);
}

View File

@ -68,6 +68,9 @@ class Oauth_token_association extends Managed_DataObject
'oauth_token_association_profile_id_fkey' => array('profile', array('profile_id' => 'id')),
'oauth_token_association_application_id_fkey' => array('oauth_application', array('application_id' => 'id')),
),
'indexes' => array(
'oauth_token_association_application_id_idx' => array('application_id'),
),
);
}
}

View File

@ -49,6 +49,9 @@ class Profile_block extends Managed_DataObject
'profile_block_blocked_fkey' => array('profile', array('blocked' => 'id')),
),
'primary key' => array('blocker', 'blocked'),
'indexes' => array(
'profile_block_blocked_idx' => array('blocked'),
),
);
}

View File

@ -47,6 +47,9 @@ class Related_group extends Managed_DataObject
'related_group_group_id_fkey' => array('user_group', array('group_id' => 'id')),
'related_group_related_group_id_fkey' => array('user_group', array('related_group_id' => 'id')),
),
'indexes' => array(
'related_group_related_group_id_idx' => array('related_group_id'),
),
);
}
}

View File

@ -45,6 +45,9 @@ class Remember_me extends Managed_DataObject
'foreign keys' => array(
'remember_me_user_id_fkey' => array('user', array('user_id' => 'id')),
),
'indexes' => array(
'remember_me_user_id_idx' => array('user_id'),
),
);
}
}

View File

@ -102,6 +102,7 @@ class User extends Managed_DataObject
'user_carrier_fkey' => array('sms_carrier', array('carrier' => 'id')),
),
'indexes' => array(
'user_carrier_idx' => array('carrier'),
'user_created_idx' => array('created'),
'user_smsemail_idx' => array('smsemail'),
),

View File

@ -259,12 +259,9 @@ class MysqlSchema extends Schema
<<<END
SELECT INDEX_NAME AS `key_name`, INDEX_TYPE AS `key_type`, COLUMN_NAME AS `col`
FROM INFORMATION_SCHEMA.STATISTICS
WHERE TABLE_SCHEMA = '{$schema}' AND TABLE_NAME = '{$table}'
WHERE TABLE_SCHEMA = '{$schema}'
AND TABLE_NAME = '{$table}'
AND NON_UNIQUE IS TRUE
AND INDEX_NAME NOT IN (
SELECT CONSTRAINT_NAME FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
WHERE REFERENCED_TABLE_NAME IS NOT NULL
)
ORDER BY SEQ_IN_INDEX;
END
);

View File

@ -1083,8 +1083,44 @@ class Schema
}
// A few quick checks :D
if (!isset($def['fields'])) {
throw new Exception("Invalid table definition for $tableName: no fields.");
if (!array_key_exists('fields', $def)) {
throw new ServerException(
"Invalid table definition for {$tableName}: no fields."
);
}
// Invalidate foreign key definitions that lack a specified index
if (array_key_exists('foreign keys', $def)) {
foreach ($def['foreign keys'] as $fkey_name => $fkey_def) {
$fkey_cols = array_keys($fkey_def[1]);
// A list of all keys/indices
$keys = array_values(array_merge(
($def['unique keys'] ?? []),
($def['indexes'] ?? [])
));
if (array_key_exists('primary key', $def)) {
$keys[] = $def['primary key'];
}
$indexed = false;
foreach ($keys as $key_cols) {
// Only the beginning of a key counts
$cols = array_slice($key_cols, 0, count($fkey_cols));
if ($cols == $fkey_cols) {
$indexed = true;
break;
}
}
if (!$indexed) {
throw new ServerException(
"Invalid table definition for {$tableName}: "
. "foreign key {$fkey_name} is not indexed."
);
}
}
}
return $def;

View File

@ -60,6 +60,10 @@ class Activitypub_pending_follow_requests extends Managed_DataObject
'activitypub_pending_follow_requests_local_profile_id_fkey' => ['profile', ['local_profile_id' => 'id']],
'activitypub_pending_follow_requests_remote_profile_id_fkey' => ['profile', ['remote_profile_id' => 'id']],
],
'indexes' => [
'activitypub_pending_follow_requests_local_profile_id_idx' => ['local_profile_id'],
'activitypub_pending_follow_requests_remote_profile_id_idx' => ['remote_profile_id'],
],
];
}

View File

@ -95,6 +95,7 @@ class Happening extends Managed_DataObject
'happening_uri_fkey' => array('notice', array('uri' => 'uri'))
),
'indexes' => array(
'happening_profile_id_idx' => array('profile_id'),
'happening_created_idx' => array('created'),
'happening_start_time_end_time_idx' => array('start_time', 'end_time'),
),

View File

@ -85,7 +85,10 @@ class RSVP extends Managed_DataObject
'rsvp_event_uri_fkey' => array('happening', array('event_uri' => 'uri')),
'rsvp_profile_id_fkey' => array('profile', array('profile_id' => 'id'))
),
'indexes' => array('rsvp_created_idx' => array('created')),
'indexes' => array(
'rsvp_event_uri_idx' => array('event_uri'),
'rsvp_created_idx' => array('created'),
),
);
}

View File

@ -55,6 +55,7 @@ class GNUsocialProfileExtensionResponse extends Managed_DataObject
'gnusocialprofileextensionresponse_extension_id_fkey' => ['gnusocialprofileextensionfield', ['extension_id' => 'id']],
],
'indexes' => [
'gnusocialprofileextensionresponse_profile_id_idx' => ['profile_id'],
'gnusocialprofileextensionresponse_extension_id_idx' => ['extension_id'],
],
];

View File

@ -1,35 +1,30 @@
<?php
// This file is part of GNU social - https://www.gnu.org/software/social
//
// GNU social is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// GNU social is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
/**
* Who received a group message
*
* PHP version 5
*
* @category Data
* @package StatusNet
* @package GNUsocial
* @author Evan Prodromou <evan@status.net>
* @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
* @link http://status.net/
*
* StatusNet - the distributed open-source microblogging tool
* Copyright (C) 2011, StatusNet, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* @copyright 2011 StatusNet, Inc.
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
*/
if (!defined('STATUSNET')) {
exit(1);
}
defined('GNUSOCIAL') || die();
require_once INSTALLDIR . '/classes/Memcached_DataObject.php';
@ -37,10 +32,9 @@ require_once INSTALLDIR . '/classes/Memcached_DataObject.php';
* Data class for group direct messages for users
*
* @category GroupPrivateMessage
* @package StatusNet
* @package GNUsocial
* @author Evan Prodromou <evan@status.net>
* @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
* @link http://status.net/
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
*
* @see DB_DataObject
*/
@ -66,10 +60,13 @@ class Group_message_profile extends Managed_DataObject
'group_message_profile_to_profile_fkey' => array('profile', array('to_profile' => 'id')),
'group_message_profile_group_message_id_fkey' => array('group_message', array('group_message_id' => 'id')),
),
'indexes' => array(
'group_message_profile_group_message_id_idx' => array('group_message_id'),
),
);
}
function send($gm, $profile)
public function send($gm, $profile)
{
$gmp = new Group_message_profile();
@ -87,13 +84,13 @@ class Group_message_profile extends Managed_DataObject
return $gmp;
}
function notify()
public function notify()
{
// XXX: add more here
$this->notifyByMail();
}
function notifyByMail()
public function notifyByMail()
{
$to = User::getKV('id', $this->to_profile);
@ -117,21 +114,23 @@ class Group_message_profile extends Managed_DataObject
// TRANS: %1$s is the sending user's long name, %2$s is the sending user's nickname,
// TRANS: %3$s is the message content, %4$s a URL to the message,
// TRANS: %5$s is the StatusNet sitename.
$body = sprintf(_m("%1\$s (%2\$s) sent a private message to group %3\$s:\n\n".
"------------------------------------------------------\n".
"%4\$s\n".
"------------------------------------------------------\n\n".
"You can reply to their message here:\n\n".
"%5\$s\n\n".
"Do not reply to this email; it will not get to them.\n\n".
"With kind regards,\n".
"%6\$s"),
$body = sprintf(
_m("%1\$s (%2\$s) sent a private message to group %3\$s:\n\n"
. "------------------------------------------------------\n"
. "%4\$s\n"
. "------------------------------------------------------\n\n"
. "You can reply to their message here:\n\n"
. "%5\$s\n\n"
. "Do not reply to this email; it will not get to them.\n\n"
. "With kind regards,\n"
. "%6\$s"),
$from_profile->getBestName(),
$from_profile->nickname,
$group->nickname,
$gm->content,
common_local_url('newmessage', array('to' => $from_profile->id)),
common_config('site', 'name')) . "\n";
common_local_url('newmessage', ['to' => $from_profile->id]),
common_config('site', 'name')
) . "\n";
$headers = _mail_prepare_headers('message', $to->nickname, $from_profile->nickname);

View File

@ -37,6 +37,9 @@ class Mention_url_profile extends Managed_DataObject
'foreign keys' => array(
'mention_url_profile_profile_id_fkey' => array('profile', array('profile_id' => 'id')),
),
'indexes' => array(
'mention_url_profile_profile_id_idx' => array('profile_id'),
),
);
}

View File

@ -90,6 +90,7 @@ class ModLog extends Managed_DataObject
],
'indexes' => [
'modlog_profile_id_created_idx' => ['profile_id', 'created'],
'modlog_moderator_id_idx' => ['moderator_id'],
],
];
}