Help optimizing a query!
I am customizing a .net application which involves moving large amount of data from one SQL DB to another SQL DB. The scenario goes as follows: The data that is being moved or being inserted into the other database comes from a Security Access Control System which logs all the access transactions on its SQL DB named "PWNT". My application is an Attendance & Time Management application. In my application, I'm trying to use the Security Access Control DB to populate my application DB tables. Specifically, the employees attendance table. My DB is named "MASTMS". I wrote the following tsql code: ----This code segment moves the "IN" (Employees logging in to the system) transactions from the security access control DB ----to Attendance management application DB insert into AccountEmployeeAttendance(AccountId,AccountEmployeeId,AttendanceDate,AccountAbsenceTypeId, AttendanceTime,InOut) select xx.AccountId ,xx.AccountEmployeeId ,xx.AttendanceDate,xx.AccountAbsenceTypeId, xx.AttendanceTime ,XX.InOut from( SELECT MASTMS.dbo.accountemployee.AccountId, MASTMS.dbo.accountemployee.AccountEmployeeId, CONVERT (DATETIME, CONVERT(CHAR(10),EV_LOG.EVNT_DAT, 103),103) AS AttendanceDate, Null AS AccountAbsenceTypeId, MIN(EV_LOG.EVNT_DAT) as AttendanceTime, ---takes the first transaction as the 'IN' transaction 'In' AS InOut ---Explicitly flagging the transactions as 'IN' FROM pwnt.dbo.EV_LOG INNER JOIN pwnt.dbo.BADGE_C ON pwnt.dbo.EV_LOG.CARDNO = pwnt.dbo.BADGE_C.CARDNO INNER JOIN MASTMS.dbo.accountemployee ON pwnt.dbo.EV_LOG.CARDNO = MASTMS.dbo.accountemployee.EmployeeCode WHERE CONVERT (DATETIME, CONVERT(CHAR(10),EV_LOG.EVNT_DAT, 103),103) NOT IN (SELECT AttendanceDate FROM AccountEmployeeAttendance where InOut='In') GROUP BY MASTMS.dbo.accountemployee.AccountId, MASTMS.dbo.accountemployee.AccountEmployeeId, CONVERT(CHAR(10),EV_LOG.EVNT_DAT, 103) )xx ----This code segment moves the "OUT" (Employees logging out of the system) transactions from the security access control DB ---- to Attendance management application DB insert into AccountEmployeeAttendance(AccountId,AccountEmployeeId,AttendanceDate,AccountAbsenceTypeId, AttendanceTime,InOut) select xx.AccountId ,xx.AccountEmployeeId ,xx.AttendanceDate,xx.AccountAbsenceTypeId, xx.AttendanceTime ,XX.InOut from( SELECT MASTMS.dbo.accountemployee.AccountId, MASTMS.dbo.accountemployee.AccountEmployeeId, CONVERT (DATETIME, CONVERT(CHAR(10),EV_LOG.EVNT_DAT, 103),103) AS AttendanceDate, Null AS AccountAbsenceTypeId, MAX(EV_LOG.EVNT_DAT) as AttendanceTime,---takes the last transaction as the 'Out' transaction 'Out' AS InOut ---Explicitly flagging the transactions as 'Out' FROM pwnt.dbo.EV_LOG INNER JOIN pwnt.dbo.BADGE_C ON pwnt.dbo.EV_LOG.CARDNO = pwnt.dbo.BADGE_C.CARDNO INNER JOIN MASTMS.dbo.accountemployee ON pwnt.dbo.EV_LOG.CARDNO = MASTMS.dbo.accountemployee.EmployeeCode WHERE CONVERT (DATETIME, CONVERT(CHAR(10),EV_LOG.EVNT_DAT, 103),103) NOT IN (SELECT AttendanceDate FROM AccountEmployeeAttendance where InOut='Out') GROUP BY MASTMS.dbo.accountemployee.AccountId, MASTMS.dbo.accountemployee.AccountEmployeeId, CONVERT(CHAR(10),EV_LOG.EVNT_DAT, 103) )xx The problem is that the condition which I am using to make sure no duplicate data is written. This condition made the query run for an about two minutes. Is there another way I can use for the condition to accelerate the query ???? Kindly help!!!!Ahmad A. Al Tamimi
July 24th, 2012 9:05am

You might want to try the T-SQL forum.MCTS, MCITP - Please mark posts as answered where appropriate.
Free Windows Admin Tool Kit Click here and download it now
July 24th, 2012 9:31am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics