Pawan Nogariya
Pawan Nogariya

Reputation: 8950

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). This is the current state

tblImport contains 27k records

tblImport has fulltext index on class_code

classmaster_n contains 27k records

classmaster_n has index on classcode

Is it taking considerable time? I doubt!

In future both the tables can have 0.1 million records! 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.

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

Changed FULLTEXT index type in tblImport_n table to normal index

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

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

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

I think I made it!!!

I just changed tblImport_n.Class_Code to non null column and the query took just 27secs!!

I am still testing all the cases...

Upvotes: 0

Views: 338

Answers (2)

echo_Me
echo_Me

Reputation: 37233

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

Upvotes: 0

Joe Taras
Joe Taras

Reputation: 15379

Try with NOT EXISTS clause:

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

Upvotes: 1

Related Questions