[英]SQL Query taking a long time
I am trying to run this query 我正在尝试运行此查询
SELECT DISTINCT
t.class_name,
t.class_code,
@UTCTimeNow,
@Username
FROM
tblimport_n t
LEFT JOIN classmaster_n cm ON t.class_code = cm.classcode
WHERE
cm.classcode IS NULL
But this is taking so long(between 7-8 mins). 但这要花很长时间(7-8分钟之间)。 This is the current state
这是当前状态
tblImport contains 27k records tblImport包含27k条记录
tblImport has fulltext index on class_code tblImport在class_code上具有全文本索引
classmaster_n contains 27k records classmaster_n包含27k条记录
classmaster_n has index on classcode classmaster_n在类代码上具有索引
Is it taking considerable time? 是否需要花费大量时间? I doubt!
我怀疑!
In future both the tables can have 0.1 million records! 将来,两个表都可以有10万条记录! I am afraid of that result!
我怕那样的结果!
EDIT : 编辑:
What I am trying to get is, all the records from tblImport
, those are not in classmaster_n
. 我试图得到的是,从所有记录
tblImport
,这些都不是在classmaster_n
。
Actually I want to insert all the classes that does not already exists in the database. 实际上,我想插入数据库中尚不存在的所有类。 I will be using this query to insert records.
我将使用此查询来插入记录。
EDIT 2 编辑2
Changed FULLTEXT
index type in tblImport
_n table to normal index 将
tblImport
_n表中的FULLTEXT
索引类型更改为普通索引
Explain for my query shows this 解释我的查询显示此
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t ALL (NULL) (NULL) (NULL) (NULL) 27071
1 SIMPLE cm index (NULL) i_Classmaster_Classcode 202 (NULL) 27251 Using where; Using index; Not exists
tblImport_n tblImport_n
CREATE TABLE `tblimport_n` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`SCHOOL_CODE` varchar(50) DEFAULT NULL,
`SCHOOL_NAME` varchar(300) DEFAULT NULL,
`CLASS_CODE` varchar(50) DEFAULT NULL,
`CLASS_NAME` varchar(300) DEFAULT NULL,
`TEACHER_EMPLOYEE_CODE` varchar(50) DEFAULT NULL,
`TEACHER_TITLE` varchar(300) DEFAULT NULL,
`TEACHER_FIRSTNAME` varchar(300) DEFAULT NULL,
`TEACHER_MIDDLENAME` varchar(300) DEFAULT NULL,
`TEACHER_LASTNAME` varchar(300) DEFAULT NULL,
`TEACHER_EMAIL_ADDRESS` varchar(300) DEFAULT NULL,
`STAFF_CODE` varchar(300) DEFAULT NULL,
`STUDENT_CODE` varchar(50) DEFAULT NULL,
`STUDENT_FIRSTNAME` varchar(300) DEFAULT NULL,
`STUDENT_MIDDLENAME` varchar(300) DEFAULT NULL,
`STUDENT_LASTNAME` varchar(300) DEFAULT NULL,
`STUDENT_GRADE` varchar(300) DEFAULT NULL,
`STUDENT_GENDER` varchar(300) DEFAULT NULL,
`STUDENT_BIRTH_DATE` varchar(300) DEFAULT NULL,
`STUDENT_HOMEROOM` varchar(300) DEFAULT NULL,
`STUDENT_IEP_STATUS` varchar(300) DEFAULT NULL,
`STUDENT_LEP_STATUS` varchar(300) DEFAULT NULL,
`STUDENT_LEP_Year` varchar(300) DEFAULT NULL,
`STUDENT_RACE` varchar(300) DEFAULT NULL,
`STUDENT_LANGUAGE` varchar(300) DEFAULT NULL,
`STUDENT_NETWORK` varchar(300) DEFAULT NULL,
`STUDENT_ACCOMMODATIONS` varchar(300) DEFAULT NULL,
`DISTRICTID` int(10) DEFAULT NULL,
`TMD5hash` varchar(1500) DEFAULT NULL,
`SMD5hash` varchar(1500) DEFAULT NULL,
PRIMARY KEY (`ID`),
KEY `i1` (`CLASS_CODE`),
KEY `i2` (`SCHOOL_CODE`)
) ENGINE=InnoDB AUTO_INCREMENT=32768 DEFAULT CHARSET=utf8
classmaster_n classmaster_n
CREATE TABLE `classmaster_n` (
`ClassId` int(11) NOT NULL AUTO_INCREMENT,
`ClassName` varchar(200) NOT NULL,
`ClassCode` varchar(200) NOT NULL,
`CreatedDate` datetime DEFAULT NULL,
`CreatedUser` varchar(100) DEFAULT NULL,
`UpdatedDate` datetime DEFAULT NULL,
`UpdatedUser` varchar(100) DEFAULT NULL,
PRIMARY KEY (`ClassId`),
KEY `i_Classmaster_Classcode` (`ClassCode`)
) ENGINE=InnoDB AUTO_INCREMENT=35094 DEFAULT CHARSET=latin1
EDIT 3 编辑3
I think I made it!!! 我想我做到了!!!
I just changed tblImport_n.Class_Code
to non null column and the query took just 27secs!! 我只是将
tblImport_n.Class_Code
更改为非null列,而查询只花了27秒!
I am still testing all the cases... 我还在测试所有情况...
Try with NOT EXISTS clause: 尝试使用NOT EXISTS子句:
SELECT DISTINCT
t.class_name,
t.class_code,
@UTCTimeNow,
@Username
FROM tblimport_n t
WHERE NOT EXISTS
(SELECT 'X' FROM classmaster_n cm WHERE t.class_code = cm.classcode)
Because your LEFT JOIN with WHERE condition on classcode is null is the same thing 因为您在类代码上具有WHERE条件的LEFT JOIN为null是同一回事
try this 尝试这个
SELECT
t.class_name,
t.class_code,
@UTCTimeNow,
@Username
FROM
tblimport_n t
LEFT JOIN classmaster_n cm ON t.class_code = cm.classcode
WHERE
cm.classcode IS NULL
group by t.class_name, t.class_code
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.