我有两个数据库,有三个相似的表(mdl_user,mdl_course和mdl_course_enrol)。
mdl_user和mdl_course中的用户和课程的id值在两个数据库中都不同。这两个值都由第三个表引用,即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';
我正在尝试运行插入查询,但它花了太长时间。有没有办法减少时间?
以下是解释选择后的输出: -
答案 0 :(得分:0)
我不能保证这会更快。但是,我猜这个问题出现在相关的子查询中。您可以通过运行仅选择部分来验证这一点,以查看它是否在合理的时间内运行。
此版本将相关子查询重写为连接:
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 )