Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Query running too long with extra JOIN

Author  Topic 

duanecwilson
Constraint Violating Yak Guru

273 Posts

Posted - 2010-05-20 : 11:02:22
I have the following query that creates a temporary table with one indexed column, populates it and then selects from other tables to create an Employee cross reference from 2 merged companies on Employee IDs. It works fine and quickly until I add the last join which is to get only those employees that have OneNote licenses (at the most 2500). I tried a RIGHT join and then an INNER join because I only want those from the list that have this program. But the query just sits there. There are NULL Employees in the OneNote table, so I excluded them in the final WHERE clause:
select #t.P_User 
,H.EMPLID
,[SoftwareReporting].[dbo].[FormatName] ((CASE WHEN charindex(',', e.[NAME]) > 0 THEN replace(e.[NAME], ',', ', ') ELSE e.[NAME] END),'F') FirstName
,[SoftwareReporting].[dbo].[FormatName] ((CASE WHEN charindex(',', e.[NAME]) > 0 THEN replace(e.[NAME], ',', ', ') ELSE e.[NAME] END),'L') LastName
from #t
left join ERS_ePeople e
on E.EMPLOYEE_ID = #t.p_user
inner join HR_NSV_PM H
ON [SoftwareReporting].[dbo].[FormatName] ((CASE WHEN charindex(',', e.[NAME]) > 0 THEN replace(e.[NAME], ',', ', ') ELSE e.[NAME] END),'F') = H.FIRST_NAME
AND [SoftwareReporting].[dbo].[FormatName] ((CASE WHEN charindex(',', e.[NAME]) > 0 THEN replace(e.[NAME], ',', ', ') ELSE e.[NAME] END),'L') = H.LAST_NAME
Inner join OneNoteUsersPM O on O.EMPLID = H.EMPLID
where e.NAME is not null and O.EMPLID is not null
order by P_User
I know the FormatName function looks weird, but it works, and fast, too. The only problem is adding that last join. I added indexes to all the appropriate columns, too, thinking that was the problem, but it is still just churning along with no results. There are less than 100,000 rows in any of the tables, and as I said, runs quickly without the last join.

Duane

Bustaz Kool
Master Smack Fu Yak Hacker

1834 Posts

Posted - 2010-05-24 : 19:30:25
What does the execution plan show?

=======================================
A couple of months in the laboratory can save a couple of hours in the library. -Frank H. Westheimer, chemistry professor (1912-2007)
Go to Top of Page

AndrewMurphy
Master Smack Fu Yak Hacker

2916 Posts

Posted - 2010-05-25 : 03:37:19
Why don't you also cleanuyp/standardise your "e.name" field to remove the "special characters" that cause your equality conditions to automatically fail. Having to put in CASE statements like you are doing will be a never ending headache. It would be best to put some effort into your front-end to prevent "bad" data getting into your system.

The performance hit from invoking the "replace" during the "joins" won't help either.
Go to Top of Page
   

- Advertisement -