0

3 つの同様のテーブル (mdl_user、mdl_course、mdl_course_enrol) を持つ 2 つのデータベースがあります。

mdl_user と mdl_course のユーザーとコースの id 値は、両方のデータベースで異なります。両方の値は、3 番目のテーブル、つまり mdl_course_enrol によって参照されます。データベース 1 のテーブルは空で、データベース 2 (nlbdb1_9) のテーブルはいっぱいです。mdl_course_enrol に正しい値が入力されるようにクエリを実行しようとしています。以下は私の質問です: -

Insert into mdl_course_enrol 
(userid, courseid, registrationdate, expirydate, status, startdate, enddate, roleassignmentid)
Select u1.id, c1.id, t2.registrationdate, t2.expirydate, t2.status, t2.startdate, t2.enddate, t2.roleassignmentid
from 
nlbdb1_9.mdl_course_enrol as t2, mdl_user as u1, mdl_course as c1
where 
t2.userid=(Select u1.id from nlbdb1_9.mdl_user as u2 where t2.userid=u2.id and BINARY u2.email=BINARY u1.email)
and
t2.courseid=(Select c1.id from nlbdb1_9.mdl_course as c2 where t2.courseid=c2.id and BINARY c2.fullname = BINARY c1.fullname);

以下はテーブル構造です:-

CREATE TABLE `mdl_course_enrol` (
  `id` bigint(10) unsigned NOT NULL AUTO_INCREMENT,
  `userid` bigint(10) unsigned NOT NULL,
  `courseid` bigint(10) unsigned NOT NULL,
  `registrationdate` bigint(10) unsigned NOT NULL,
  `expirydate` bigint(10) unsigned NOT NULL,
  `status` varchar(32) NOT NULL DEFAULT 'Not Attempted',
  `startdate` bigint(10) NOT NULL,
  `enddate` bigint(10) NOT NULL,
  `roleassignmentid` bigint(10) unsigned NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=14962 DEFAULT CHARSET=utf8;


CREATE TABLE `mdl_course` (
  `id` bigint(10) NOT NULL AUTO_INCREMENT,
  `category` bigint(10) NOT NULL DEFAULT '0',
  `sortorder` bigint(10) NOT NULL DEFAULT '0',
  `fullname` varchar(254) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
  `shortname` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
  `idnumber` varchar(100) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
  `summary` longtext CHARACTER SET utf8 COLLATE utf8_unicode_ci,
  `summaryformat` tinyint(2) NOT NULL DEFAULT '0',
  `format` varchar(10) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL DEFAULT 'topics',
  `showgrades` tinyint(2) NOT NULL DEFAULT '1',
  `sectioncache` longtext CHARACTER SET utf8 COLLATE utf8_unicode_ci,
  `modinfo` longtext CHARACTER SET utf8 COLLATE utf8_unicode_ci,
  `newsitems` mediumint(5) NOT NULL DEFAULT '1',
  `startdate` bigint(10) NOT NULL DEFAULT '0',
  `numsections` mediumint(5) NOT NULL DEFAULT '1',
  `marker` bigint(10) NOT NULL DEFAULT '0',
  `maxbytes` bigint(10) NOT NULL DEFAULT '0',
  `legacyfiles` smallint(4) NOT NULL DEFAULT '0',
  `showreports` smallint(4) NOT NULL DEFAULT '0',
  `visible` tinyint(1) NOT NULL DEFAULT '1',
  `visibleold` tinyint(1) NOT NULL DEFAULT '1',
  `hiddensections` tinyint(2) NOT NULL DEFAULT '0',
  `groupmode` smallint(4) NOT NULL DEFAULT '0',
  `groupmodeforce` smallint(4) NOT NULL DEFAULT '0',
  `defaultgroupingid` bigint(10) NOT NULL DEFAULT '0',
  `lang` varchar(30) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
  `theme` varchar(50) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
  `timecreated` bigint(10) NOT NULL DEFAULT '0',
  `timemodified` bigint(10) NOT NULL DEFAULT '0',
  `requested` tinyint(1) NOT NULL DEFAULT '0',
  `enablecompletion` tinyint(1) NOT NULL DEFAULT '0',
  `completionstartonenrol` tinyint(1) NOT NULL DEFAULT '0',
  `completionnotify` tinyint(1) NOT NULL DEFAULT '0',
  `coursedisplay` tinyint(2) NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`),
  KEY `mdl_cour_cat_ix` (`category`),
  KEY `mdl_cour_idn_ix` (`idnumber`),
  KEY `mdl_cour_sho_ix` (`shortname`),
  KEY `mdl_cour_sor_ix` (`sortorder`)
) ENGINE=InnoDB AUTO_INCREMENT=45 DEFAULT CHARSET=utf8 COMMENT='Central course table';

CREATE TABLE `mdl_user` (
  `id` bigint(10) unsigned NOT NULL AUTO_INCREMENT,
  `auth` varchar(20) NOT NULL DEFAULT 'manual',
  `confirmed` tinyint(1) NOT NULL DEFAULT '0',
  `policyagreed` tinyint(1) NOT NULL DEFAULT '0',
  `deleted` tinyint(1) NOT NULL DEFAULT '0',
  `mnethostid` bigint(10) unsigned NOT NULL DEFAULT '0',
  `username` varchar(100) NOT NULL DEFAULT '',
  `password` varchar(32) NOT NULL DEFAULT '',
  `pwdlastchange` bigint(10) unsigned NOT NULL DEFAULT '0',
  `idnumber` varchar(255) NOT NULL DEFAULT '',
  `firstname` varchar(100) NOT NULL DEFAULT '',
  `lastname` varchar(100) NOT NULL DEFAULT '',
  `email` varchar(100) NOT NULL DEFAULT '',
  `emailstop` tinyint(1) unsigned NOT NULL DEFAULT '0',
  `nric` varchar(15) NOT NULL DEFAULT '',
  `skype` varchar(50) NOT NULL DEFAULT '',
  `yahoo` varchar(50) NOT NULL DEFAULT '',
  `aim` varchar(50) NOT NULL DEFAULT '',
  `msn` varchar(50) NOT NULL DEFAULT '',
  `phone1` varchar(20) NOT NULL DEFAULT '',
  `phone2` varchar(20) NOT NULL DEFAULT '',
  `institution` varchar(40) NOT NULL DEFAULT '',
  `department` varchar(30) NOT NULL DEFAULT '',
  `address` varchar(70) NOT NULL DEFAULT '',
  `city` varchar(20) NOT NULL DEFAULT '',
  `country` varchar(2) NOT NULL DEFAULT '',
  `lang` varchar(30) NOT NULL DEFAULT 'en_utf8',
  `theme` varchar(50) NOT NULL DEFAULT '',
  `timezone` varchar(100) NOT NULL DEFAULT '99',
  `firstaccess` bigint(10) unsigned NOT NULL DEFAULT '0',
  `lastaccess` bigint(10) unsigned NOT NULL DEFAULT '0',
  `lastlogin` bigint(10) unsigned NOT NULL DEFAULT '0',
  `currentlogin` bigint(10) unsigned NOT NULL DEFAULT '0',
  `lastip` varchar(15) NOT NULL DEFAULT '',
  `secret` varchar(15) NOT NULL DEFAULT '',
  `picture` tinyint(1) NOT NULL DEFAULT '0',
  `url` varchar(255) NOT NULL DEFAULT '',
  `description` text,
  `mailformat` tinyint(1) unsigned NOT NULL DEFAULT '1',
  `maildigest` tinyint(1) unsigned NOT NULL DEFAULT '0',
  `maildisplay` tinyint(2) unsigned NOT NULL DEFAULT '2',
  `htmleditor` tinyint(1) unsigned NOT NULL DEFAULT '1',
  `ajax` tinyint(1) unsigned NOT NULL DEFAULT '1',
  `autosubscribe` tinyint(1) unsigned NOT NULL DEFAULT '1',
  `trackforums` tinyint(1) unsigned NOT NULL DEFAULT '0',
  `timemodified` bigint(10) unsigned NOT NULL DEFAULT '0',
  `trustbitmask` bigint(10) unsigned NOT NULL DEFAULT '0',
  `imagealt` varchar(255) DEFAULT NULL,
  `screenreader` tinyint(1) NOT NULL DEFAULT '0',
  `nlbid` bigint(10) unsigned DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `mdl_user_mneuse_uix` (`mnethostid`,`username`),
  KEY `mdl_user_del_ix` (`deleted`),
  KEY `mdl_user_con_ix` (`confirmed`),
  KEY `mdl_user_fir_ix` (`firstname`),
  KEY `mdl_user_las_ix` (`lastname`),
  KEY `mdl_user_cit_ix` (`city`),
  KEY `mdl_user_cou_ix` (`country`),
  KEY `mdl_user_las2_ix` (`lastaccess`),
  KEY `mdl_user_ema_ix` (`email`),
  KEY `mdl_user_aut_ix` (`auth`),
  KEY `mdl_user_idn_ix` (`idnumber`)
) ENGINE=MyISAM AUTO_INCREMENT=5902 DEFAULT CHARSET=utf8 COMMENT='One record for each person';

挿入クエリを実行しようとしていますが、時間がかかりすぎます。時間を短縮する方法はありますか?

以下は、Explain Select 後の出力です。

ここに画像の説明を入力

4

1 に答える 1

0

これが速くなるとは約束できません。ただし、問題は相関サブクエリにあると推測しています。これは、select-only 部分を実行して妥当な時間内に実行されるかどうかを確認することで確認できます。

このバージョンでは、相関サブクエリを結合として書き直しています。

Insert into mdl_course_enrol(userid, courseid, registrationdate, expirydate, status,
                             startdate, enddate, roleassignmentid)
    Select u.id, c.id, t2.registrationdate, t2.expirydate, t2.status, t2.startdate,
           t2.enddate, t2.roleassignmentid
    from nlbdb1_9.mdl_course_enrol t2 join
         nlbdb1_9.mdl_user u2
         on t2.userid = u2.userid join
         mdl_user u
         on (u.email collate utf8_unicode_ci = u2.email collate utf8_unicode_ci)  join
         nlbdb1_9.mdl_course c2
         on t2.courseid=c2.id join
         mdl_course c
         on (c2.fullname collate utf8_unicode_ci = c.fullname collate utf8_unicode_ci )
于 2012-09-21T16:05:30.917 に答える