1
0
mirror of https://github.com/phpbb/phpbb.git synced 2025-03-22 00:21:02 +01:00

Merge pull request #4701 from DavidIQ/ticket/14992

[ticket/14992] Improve user_notifications table performance
This commit is contained in:
Marc Alexander 2017-05-25 19:36:26 +02:00
commit deb9240546
No known key found for this signature in database
GPG Key ID: 50E0D2423696F995
8 changed files with 336 additions and 14 deletions

View File

@ -49,6 +49,6 @@ script:
- travis/check-image-icc-profiles.sh $DB $TRAVIS_PHP_VERSION $NOTESTS
- travis/check-executable-files.sh $DB $TRAVIS_PHP_VERSION $NOTESTS ./
- sh -c "if [ '$SLOWTESTS' != '1' -a '$DB' = 'mysqli' ]; then phpBB/vendor/bin/phpunit tests/lint_test.php; fi"
- sh -c "if [ '$NOTESTS' != '1' -a '$SLOWTESTS' != '1' ]; then phpBB/vendor/bin/phpunit --configuration travis/phpunit-$DB-travis.xml; fi"
- sh -c "if [ '$NOTESTS' != '1' -a '$SLOWTESTS' != '1' ]; then phpBB/vendor/bin/phpunit --configuration travis/phpunit-$DB-travis.xml --verbose --stop-on-error; fi"
- sh -c "if [ '$SLOWTESTS' = '1' ]; then phpBB/vendor/bin/phpunit --configuration travis/phpunit-$DB-travis.xml --group slow; fi"
- sh -c "set -x;if [ '$NOTESTS' = '1' -a '$TRAVIS_PULL_REQUEST' != 'false' ]; then git-tools/commit-msg-hook-range.sh origin/$TRAVIS_BRANCH..FETCH_HEAD; fi"

View File

@ -0,0 +1,46 @@
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/
namespace phpbb\db\migration\data\v32x;
class user_notifications_table_index_p1 extends \phpbb\db\migration\migration
{
static public function depends_on()
{
return array(
'\phpbb\db\migration\data\v32x\cookie_notice_p2',
);
}
public function update_schema()
{
return array(
'add_index' => array(
$this->table_prefix . 'user_notifications' => array(
'user_id' => array('user_id'),
),
),
);
}
public function revert_schema()
{
return array(
'drop_keys' => array(
$this->table_prefix . 'user_notifications' => array(
'user_id',
),
),
);
}
}

View File

@ -0,0 +1,46 @@
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/
namespace phpbb\db\migration\data\v32x;
class user_notifications_table_index_p2 extends \phpbb\db\migration\migration
{
static public function depends_on()
{
return array(
'\phpbb\db\migration\data\v32x\user_notifications_table_index_p1',
);
}
public function update_schema()
{
return array(
'add_index' => array(
$this->table_prefix . 'user_notifications' => array(
'uid_itm_id' => array('user_id', 'item_id'),
),
),
);
}
public function revert_schema()
{
return array(
'drop_keys' => array(
$this->table_prefix . 'user_notifications' => array(
'uid_itm_id',
),
),
);
}
}

View File

@ -0,0 +1,46 @@
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/
namespace phpbb\db\migration\data\v32x;
class user_notifications_table_index_p3 extends \phpbb\db\migration\migration
{
static public function depends_on()
{
return array(
'\phpbb\db\migration\data\v32x\user_notifications_table_index_p2',
);
}
public function update_schema()
{
return array(
'add_index' => array(
$this->table_prefix . 'user_notifications' => array(
'usr_itm_tpe' => array('user_id', 'item_type', 'item_id'),
),
),
);
}
public function revert_schema()
{
return array(
'drop_keys' => array(
$this->table_prefix . 'user_notifications' => array(
'usr_itm_tpe',
),
),
);
}
}

View File

@ -0,0 +1,48 @@
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/
namespace phpbb\db\migration\data\v32x;
class user_notifications_table_reduce_column_sizes extends \phpbb\db\migration\migration
{
static public function depends_on()
{
return array(
'\phpbb\db\migration\data\v32x\user_notifications_table_remove_duplicates',
);
}
public function update_schema()
{
return array(
'change_columns' => array(
$this->table_prefix . 'user_notifications' => array(
'item_type' => array('VCHAR:165', ''),
'method' => array('VCHAR:165', ''),
),
),
);
}
public function revert_schema()
{
return array(
'change_columns' => array(
$this->table_prefix . 'user_notifications' => array(
'item_type' => array('VCHAR:255', ''),
'method' => array('VCHAR:255', ''),
),
),
);
}
}

View File

@ -0,0 +1,55 @@
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/
namespace phpbb\db\migration\data\v32x;
class user_notifications_table_remove_duplicates extends \phpbb\db\migration\migration
{
static public function depends_on()
{
return array(
'\phpbb\db\migration\data\v32x\user_notifications_table_index_p3',
);
}
public function update_data()
{
return array(
array('custom', array(array($this, 'remove_duplicates'))),
);
}
public function remove_duplicates()
{
$insert_buffer = new \phpbb\db\sql_insert_buffer($this->db, $this->table_prefix . 'user_notifications');
$sql = "SELECT item_type, item_id, user_id, method, MAX(notify) AS notify
FROM {$this->table_prefix}user_notifications
GROUP BY item_type, item_id, user_id, method
HAVING COUNT(item_type) > 1";
$result = $this->sql_query($sql);
while ($row = $this->db->sql_fetchrow($result))
{
// Delete the duplicate entries
$this->sql_query("DELETE FROM {$this->table_prefix}user_notifications
WHERE user_id = {$row['user_id']}
AND item_type = '{$row['item_type']}'
AND method = '{$row['method']}'");
// And re-insert as a single one
$insert_buffer->insert($row);
}
$this->db->sql_freeresult($result);
}
}

View File

@ -0,0 +1,46 @@
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/
namespace phpbb\db\migration\data\v32x;
class user_notifications_table_unique_index extends \phpbb\db\migration\migration
{
static public function depends_on()
{
return array(
'\phpbb\db\migration\data\v32x\user_notifications_table_reduce_column_sizes',
);
}
public function update_schema()
{
return array(
'add_unique_index' => array(
$this->table_prefix . 'user_notifications' => array(
'itm_usr_mthd' => array('item_type', 'item_id', 'user_id', 'method'),
),
),
);
}
public function revert_schema()
{
return array(
'drop_keys' => array(
$this->table_prefix . 'user_notifications' => array(
'itm_usr_mthd',
),
),
);
}
}

View File

@ -946,7 +946,20 @@ class tools implements tools_interface
{
case 'oracle':
case 'sqlite3':
$row[$col] = substr($row[$col], strlen($table_name) + 1);
$index_name = $this->check_index_name_length($table_name, $table_name . '_' . $index_name, false);
$table_prefix = substr(CONFIG_TABLE, 0, -6); // strlen(config)
if (strpos($index_name , $table_name) === false)
{
if (strpos($index_name, $table_prefix) !== false)
{
$row[$col] = substr($row[$col], strlen($table_prefix) + 1);
}
else
{
$row[$col] = substr($row[$col], strlen($table_name) + 1);
}
}
break;
}
@ -1359,12 +1372,14 @@ class tools implements tools_interface
{
case 'mysql_40':
case 'mysql_41':
$index_name = $this->check_index_name_length($table_name, $index_name, false);
$statements[] = 'DROP INDEX ' . $index_name . ' ON ' . $table_name;
break;
case 'oracle':
case 'sqlite3':
$statements[] = 'DROP INDEX ' . $table_name . '_' . $index_name;
$index_name = $this->check_index_name_length($table_name, $table_name . '_' . $index_name, false);
$statements[] = 'DROP INDEX ' . $index_name;
break;
}
@ -1487,17 +1502,17 @@ class tools implements tools_interface
{
$statements = array();
$this->check_index_name_length($table_name, $index_name);
switch ($this->sql_layer)
{
case 'oracle':
case 'sqlite3':
$statements[] = 'CREATE UNIQUE INDEX ' . $table_name . '_' . $index_name . ' ON ' . $table_name . '(' . implode(', ', $column) . ')';
$index_name = $this->check_index_name_length($table_name, $table_name . '_' . $index_name);
$statements[] = 'CREATE UNIQUE INDEX ' . $index_name . ' ON ' . $table_name . '(' . implode(', ', $column) . ')';
break;
case 'mysql_40':
case 'mysql_41':
$index_name = $this->check_index_name_length($table_name, $index_name);
$statements[] = 'ALTER TABLE ' . $table_name . ' ADD UNIQUE INDEX ' . $index_name . '(' . implode(', ', $column) . ')';
break;
}
@ -1512,8 +1527,6 @@ class tools implements tools_interface
{
$statements = array();
$this->check_index_name_length($table_name, $index_name);
// remove index length unless MySQL4
if ('mysql_40' != $this->sql_layer)
{
@ -1524,7 +1537,8 @@ class tools implements tools_interface
{
case 'oracle':
case 'sqlite3':
$statements[] = 'CREATE INDEX ' . $table_name . '_' . $index_name . ' ON ' . $table_name . '(' . implode(', ', $column) . ')';
$index_name = $this->check_index_name_length($table_name, $table_name . '_' . $index_name);
$statements[] = 'CREATE INDEX ' . $index_name . ' ON ' . $table_name . '(' . implode(', ', $column) . ')';
break;
case 'mysql_40':
@ -1539,6 +1553,7 @@ class tools implements tools_interface
}
// no break
case 'mysql_41':
$index_name = $this->check_index_name_length($table_name, $index_name);
$statements[] = 'ALTER TABLE ' . $table_name . ' ADD INDEX ' . $index_name . ' (' . implode(', ', $column) . ')';
break;
}
@ -1551,15 +1566,35 @@ class tools implements tools_interface
*
* @param string $table_name
* @param string $index_name
* @param bool $throw_error
* @return string The index name, shortened if too long
*/
protected function check_index_name_length($table_name, $index_name)
protected function check_index_name_length($table_name, $index_name, $throw_error = true)
{
$table_prefix = substr(CONFIG_TABLE, 0, -6); // strlen(config)
if (strlen($table_name . $index_name) - strlen($table_prefix) > 24)
if (strlen($index_name) > 30)
{
$max_length = strlen($table_prefix) + 24;
trigger_error("Index name '{$table_name}_$index_name' on table '$table_name' is too long. The maximum is $max_length characters.", E_USER_ERROR);
// Try removing the table prefix if it's at the beginning
$table_prefix = substr(CONFIG_TABLE, 0, -6); // strlen(config)
if (strpos($index_name, $table_prefix) === 0)
{
$index_name = substr($index_name, strlen($table_prefix) + 1);
return $this->check_index_name_length($table_name, $index_name);
}
// Try removing the table name then
if (strpos($index_name, $table_name) === 0)
{
$index_name = substr($index_name, strlen($table_name) + 1);
return $this->check_index_name_length($table_name, $index_name);
}
if ($throw_error)
{
trigger_error("Index name '$index_name' on table '$table_name' is too long. The maximum is 30 characters.", E_USER_ERROR);
}
}
return $index_name;
}
/**