2012-09-21 97 views
0

我有兩個數據庫有三個相似的表(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'; 

我試圖運行插入查詢,但它走的時間太長。有什麼辦法可以縮短時間嗎?

以下爲輸出解釋選擇後: -

enter image description here

+0

取查詢的'SELECT ...'一部分,跑'它EXPLAIN SELECT ...'。發佈輸出。乍一看,你有1)[相關子查詢](http://en.wikipedia.org/wiki/Correlated_subquery),它應該被重寫爲JOIN; 2)使用BINARY的WHERE子句 - [如果沒有特殊的排序規則,就不能編制索引](http://stackoverflow.com/questions/4020812/mysql-binary-comparison-doesnt-use-index)。 – DCoder

+0

我必須使用BINARY,因爲兩個數據庫都採用不同的分頁格式。我會更多地閱讀JOIN,看看它是否會縮短一些時間。 – user1439090

回答

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) 
+0

我只是試着用少量編輯來運行這個查詢,但沒有大量的性能改進。它在過去的半個小時內仍在持續運行查詢。 – user1439090

+1

您最好的選擇可能是分別爲每個查找表創建臨時表。這些表格中會包含舊的和新的ID。然後構建索引並使用這些表來查找新表中的值。 –

+0

這要快得多。謝謝。剛剛完成插入所有數據。 – user1439090