세 개의 비슷한 테이블 (mdl_user, mdl_course 및 mdl_course_enrol)을 가진 두 개의 데이터베이스가 있습니다.mysql 쿼리를 실행하는 데 걸리는 시간 단축
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';
. 시간을 줄일 수있는 방법이 있습니까? 다음
는출력 후 선택 설명입니다 : -이 빨리 될 것이라고 약속 할 수
쿼리의 'SELECT ...'부분을 가지고 SELECT EXPLAIN ...'그것에'실행합니다. 출력을 게시하십시오. 언뜻 보면 1) [상관 관계 하위 쿼리] (http://en.wikipedia.org/wiki/Correlated_subquery)가 있는데, JOIN으로 다시 작성해야합니다. 2) BINARY를 사용하는 WHERE 절 - [특수한 조합없이 색인을 생성 할 수 없습니다] (http://stackoverflow.com/questions/4020812/mysql-binary-comparison-doesnt-use-index). – DCoder
두 데이터베이스가 서로 다른 조합 형식이므로 BINARY를 사용해야합니다. JOINs에 대해 더 많이 읽고 시간이 절약되는지 확인하겠습니다. – user1439090