Skip to content

Commit

Permalink
Fixed issue: Participant import with duplicate check very slow when t…
Browse files Browse the repository at this point in the history
…here more than 100k participants
  • Loading branch information
c-schmitz committed Sep 20, 2020
1 parent cab8d3b commit 00d3a31
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 3 deletions.
2 changes: 1 addition & 1 deletion application/config/version.php
Expand Up @@ -12,7 +12,7 @@
*/

$config['versionnumber'] = '4.3.16';
$config['dbversionnumber'] = 427;
$config['dbversionnumber'] = 428;
$config['buildnumber'] = '';
$config['updatable'] = true;
$config['templateapiversion'] = 3;
Expand Down
58 changes: 58 additions & 0 deletions application/helpers/update/updatedb_helper.php
Expand Up @@ -2931,6 +2931,36 @@ function db_upgrade_all($iOldDBVersion, $bSilent = false)
$oDB->createCommand()->update('{{settings_global}}',array('stg_value'=>420),"stg_name='DBVersion'");
$oTransaction->commit();
}

/*
* DBVersion 361 & 362 were intentionally left out to sync with Cloud Hosting
*/

/*
* Correct permission for survey menu Survey Participants (tokens, not surveysettings).
*/
if ($iOldDBVersion < 363) {
$oTransaction = $oDB->beginTransaction();
$aTableNames = dbGetTablesLike("tokens%");
$oDB = Yii::app()->getDb();
foreach ($aTableNames as $sTableName) {
try {
setTransactionBookmark();
switch (Yii::app()->db->driverName){
case 'mysql':
case 'mysqli':
$oDB->createCommand()->createIndex('idx_email', $sTableName, 'email(30)', false);
break;
case 'pgsql':
$oDB->createCommand()->createIndex('idx_email', $sTableName, 'email', false);
break;
// MSSQL does not support indexes on text fields so no dice
}
} catch (Exception $e) { rollBackToTransactionBookmark(); }
}
$oDB->createCommand()->update('{{settings_global}}', ['stg_value'=>363], "stg_name='DBVersion'");
$oTransaction->commit();
}

if($iOldDBVersion < 421) {
$oTransaction = $oDB->beginTransaction();
Expand Down Expand Up @@ -3109,6 +3139,34 @@ function db_upgrade_all($iOldDBVersion, $bSilent = false)
$oDB->createCommand()->update('{{settings_global}}', array('stg_value' => 427), "stg_name='DBVersion'");
$oTransaction->commit();
}

/*
* Correct permission for survey menu Survey Participants (tokens, not surveysettings).
*/
if ($iOldDBVersion < 428) {
$oTransaction = $oDB->beginTransaction();
$aTableNames = dbGetTablesLike("tokens%");
$oDB = Yii::app()->getDb();
foreach ($aTableNames as $sTableName) {
try {
setTransactionBookmark();
switch (Yii::app()->db->driverName){
case 'mysql':
case 'mysqli':
$oDB->createCommand()->createIndex('idx_email', $sTableName, 'email(30)', false);
break;
case 'pgsql':
$oDB->createCommand()->createIndex('idx_email', $sTableName, 'email', false);
break;
// MSSQL does not support indexes on text fields so no dice
}
} catch (Exception $e) { rollBackToTransactionBookmark(); }
}
$oDB->createCommand()->update('{{settings_global}}', ['stg_value'=>428], "stg_name='DBVersion'");
$oTransaction->commit();
}


} catch (Exception $e) {
Yii::app()->setConfig('Updating', false);
$oTransaction->rollback();
Expand Down
16 changes: 14 additions & 2 deletions application/models/Token.php
Expand Up @@ -199,10 +199,22 @@ public static function createTable($surveyId, array $extraFields = array())
$db->createCommand()->createTable($sTableName, $fields, $options);

/**
* @todo Check if this random component in the index name is needed.
* As far as I (sam) know index names need only be unique per table.
* The random component in the index name is needed because MSSQL is being the dorky kid and
* complaining about duplicates when renaming the table and trying to use the same index again
* on a new token table (for example on reactivation)
*/
$db->createCommand()->createIndex("idx_token_token_{$surveyId}_".rand(1, 50000), $sTableName, 'token');

// MSSQL does not support indexes on text fields so not needed here
switch (Yii::app()->db->driverName){
case 'mysql':
case 'mysqli':
$db->createCommand()->createIndex('idx_email', $sTableName, 'email(30)', false);
break;
case 'pgsql':
$db->createCommand()->createIndex('idx_email', $sTableName, 'email', false);
break;
}

// Refresh schema cache just in case the table existed in the past, and return if table exist
return $db->schema->getTable($sTableName, true);
Expand Down

1 comment on commit 00d3a31

@Shnoulle
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❤️ 👍

00d3a31#diff-fb32bdf0b19fecbe2d24aa8be88536e4R2957

I have really slow issue with MSSQL during survey, thanks for the tip !

Please sign in to comment.