projects@computerra.com
Starting Member
1 Post |
Posted - 2007-02-02 : 06:35:23
|
Hello friends,I’m facing performance related problem while running following query on SQL Server 2000.This query is basically used to find last location of each unit that are passed. Here I am passing data like “‘26','27','28','29','30','31','32','33','34','35','36','37','38','39','40','41','42','43','44','45','46','47','48','49','50'" in @Units variable. But it takes too much time and I don’t get output. Table is having around 5 Million records.Query: SELECT Alias, tblUnit.UnitID, Location, Latitude, Longitude,Convert(varchar(10),UnitHistoryDate,1) + ' ' + substring(Convert(varchar(40),UnitHistoryDate,109),12,9) + ' ' + substring(Convert(varchar(40),UnitHistoryDate,109),25,2) as [Date/Time], unithistoryid FROM tblUnit INNER JOIN tblUnitHistory ON tblUnit.UnitID = tblUnitHistory.UnitIDWHERE tblUnitHistory.UnitHistoryDate IN (SELECT MAX(UnitHistoryDate) FROM tblUnitHistory WHERE tblUnitHistory.UnitID in (' + @Units + ') GROUP BY tblUnitHistory.UnitID) AND tblUnit.UnitID in (' + @Units + ')ORDER BY tblUnit.UnitIDTable Structure:UnitHistoryID int Primary KeyUnitID int Location varchar(200) Latitude decimal 9 Longitude decimal 9 Speed decimal 5 BatteryVoltage decimal 5 ReasonCode intDistanceFromLastLocation decimal 9 UnitHistoryDate datetimeIndexes:1. Clustered Index on Column UnitID2. Non-clustered Index on Column UnitHistoryDate3. Non-clustered Index on Column UnitHistoryIDPlease help me to write optimize query and suggest me the steps to require making this query faster. Any help is appreciated. Thanks in advance.Regards,Raj |
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2007-02-02 : 13:27:26
|
Is UnitID indexed on both tables? I suspect it's the primary key on one of your tables, probably tblUnit. You automatically get an index when you create a primary key. You don't automatically get an index with the foreign key though. So it must be added manually to the child table, probably tblUnitHistory. Or is UnitID the PK in both tables?Tara Kizer |
 |
|