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
 General SQL Server Forums
 New to SQL Server Programming
 Time Out Expired

Author  Topic 

chriztoph
Posting Yak Master

184 Posts

Posted - 2010-04-26 : 23:07:38
Please help!
What's wrong with the query? i got time-out expired..I already Indexed the tables..

here is my query:

SELECT     dbo.tblAdmin_Positions.Position, LTRIM(dbo.tbl_SeamanMain.LastName) AS Lastname, LTRIM(dbo.tbl_SeamanMain.FirstName) AS Firstname, 
LTRIM(dbo.tbl_SeamanMain.MiddleName) AS Middlename, dbo.tblPayroll_Allottees.AllotteeName, dbo.tblAdmin_Banks.BankName,
REPLACE(dbo.tblPayroll_Allottees.AccountNo, '-', '') AS AccountNo, 'PHP' AS AcctCurrency, 0 AS AllotUSD,
dbo.tblPayroll_Allottees.NetAllotment AS AllotPHP, dbo.tblPayroll_VesselLog.ExRate, dbo.tblPayroll_VesselLog.VesselCode,
dbo.tblAdmin_Vessels.Vessel, dbo.tblPayroll_Allottees.AllotteeID, dbo.tblAdmin_Banks.BankAbbrv, dbo.tblAdmin_Branches.BranchName,
dbo.tblAdmin_Positions.IDPosDep, dbo.tblPayroll_VesselLog.PrincipalCode, dbo.tblPayroll_VesselLog.Payroll_Period,
dbo.tblAdmin_Branches.BankCode, dbo.tblAdmin_Positions.Officer, dbo.tblPayroll_Details.BatchNum, dbo.tblAdmin_Vessels.BatchNo,
dbo.tblPayroll_VesselLog.PayrollID, dbo.tblPayroll_Details.SeamanID, dbo.tblAdmin_Positions.PositionAbbrv
FROM dbo.tblAdmin_Vessels RIGHT OUTER JOIN
dbo.tblAdmin_Banks RIGHT OUTER JOIN
dbo.tblAdmin_Branches ON dbo.tblAdmin_Banks.BankCode = dbo.tblAdmin_Branches.BankCode RIGHT OUTER JOIN
dbo.tblPayroll_Allottees RIGHT OUTER JOIN
dbo.tblPayroll_Details RIGHT OUTER JOIN
dbo.tblPayroll_VesselLog ON dbo.tblPayroll_Details.PayrollID = dbo.tblPayroll_VesselLog.PayrollID AND
dbo.tblPayroll_Details.PayrollPeriod = dbo.tblPayroll_VesselLog.Payroll_Period ON
dbo.tblPayroll_Allottees.PayrollID = dbo.tblPayroll_Details.PayrollID AND
dbo.tblPayroll_Allottees.SeamanID = dbo.tblPayroll_Details.SeamanID LEFT OUTER JOIN
dbo.tblAdmin_Positions RIGHT OUTER JOIN
dbo.tbl_SeamanMain ON dbo.tblAdmin_Positions.PositionCode = dbo.tbl_SeamanMain.PositionCode ON
dbo.tblPayroll_Details.SeamanID = dbo.tbl_SeamanMain.SeamanID ON dbo.tblAdmin_Branches.BranchCode = dbo.tblPayroll_Allottees.BranchCode ON
dbo.tblAdmin_Vessels.VesselCode = dbo.tblPayroll_VesselLog.VesselCode
WHERE (dbo.tblPayroll_Details.Included = 1) AND (dbo.tblPayroll_Allottees.PaymentType = 1) AND (dbo.tbl_SeamanMain.IsSeamanActive = 1) AND
(dbo.tblPayroll_VesselLog.Closed = 1) AND (dbo.tblPayroll_Allottees.NetAllotment > 0) AND (dbo.tblAdmin_Branches.BankCode = 2) AND
(dbo.tblAdmin_Vessels.Active = 1) AND (REPLACE(dbo.tblPayroll_Allottees.AccountNo, '-', '') IS NOT NULL) AND
(dbo.tblPayroll_VesselLog.PrincipalCode = 170) AND (dbo.tblPayroll_VesselLog.Payroll_Period = 1004)

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-04-26 : 23:30:52
what does estimated execution plan suggests? what are costly steps?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-04-26 : 23:32:28
quote:
Originally posted by chriztoph

I already Indexed the tables.



You'll need to actually show us the indexes on each of the tables in this gigantic query.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

chriztoph
Posting Yak Master

184 Posts

Posted - 2010-04-26 : 23:43:02
dbo.tblPayroll_Details.Included = Non-Clustered
dbo.tblPayroll_Allottees.PaymentType = Non-Clustered
dbo.tbl_SeamanMain.IsSeamanActive = Non-Clustered
dbo.tblPayroll_VesselLog.Closed = Non-Clustered
dbo.tblPayroll_Allottees.NetAllotment = Non-Clustered
dbo.tblAdmin_Branches.BankCode = Non-Clustered
dbo.tblAdmin_Vessels.Activedbo.tblPayroll_Allottees.AccountNo = Non-Clustered
dbo.tblPayroll_VesselLog.PrincipalCode = Clustered
dbo.tblPayroll_VesselLog.Payroll_Period = Non-Clustered

Am I correct with the indexing? can you give any suggestions on how to indexed properly..thanks..

@visakh16, what do you mean estimated execution plan?
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-04-26 : 23:45:28
You are missing many indexes. Each join condition should be indexed.

SQL Server automatically adds an index on primary keys, but it does not do it for us with foreign keys. Indexes should be added to most, if not all, foreign keys as those are what are typically used in joins.

Some of the indexes you posted look to be unnecessary such as the one on Payroll_VesselLog.Closed. Are the only possible values 0 and 1? If that's the case, then the index needs to be dropped due to selectivity.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

chriztoph
Posting Yak Master

184 Posts

Posted - 2010-04-26 : 23:51:37
ok, thanks..so i need to indexed all those in joins(Foreign Keys).what's the maximum number of columns in one Index?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-04-27 : 00:00:23
quote:
Originally posted by chriztoph

dbo.tblPayroll_Details.Included = Non-Clustered
dbo.tblPayroll_Allottees.PaymentType = Non-Clustered
dbo.tbl_SeamanMain.IsSeamanActive = Non-Clustered
dbo.tblPayroll_VesselLog.Closed = Non-Clustered
dbo.tblPayroll_Allottees.NetAllotment = Non-Clustered
dbo.tblAdmin_Branches.BankCode = Non-Clustered
dbo.tblAdmin_Vessels.Activedbo.tblPayroll_Allottees.AccountNo = Non-Clustered
dbo.tblPayroll_VesselLog.PrincipalCode = Clustered
dbo.tblPayroll_VesselLog.Payroll_Period = Non-Clustered

Am I correct with the indexing? can you give any suggestions on how to indexed properly..thanks..

@visakh16, what do you mean estimated execution plan?


there's an option to view estimated execution plan from top menu

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

chriztoph
Posting Yak Master

184 Posts

Posted - 2010-04-27 : 00:33:23
thanks guys..it really works fine now..so fast..
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-04-27 : 00:38:13
quote:
Originally posted by chriztoph

ok, thanks..so i need to indexed all those in joins(Foreign Keys).what's the maximum number of columns in one Index?



Well you don't put them in one index. An index can only span one table, unless you are using indexed views which is unlikely. Don't worry about the maximum number of columns in an index (the value is in BOL but I don't have it with me at the moment), you'll never reach the maximum.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

chriztoph
Posting Yak Master

184 Posts

Posted - 2010-05-03 : 23:10:40
how to reindex all the indexes of all my databases?
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-05-03 : 23:47:21
Here's my custom script to rebuild or defrag indexes: http://weblogs.sqlteam.com/tarad/archive/2009/11/03/DefragmentingRebuilding-Indexes-in-SQL-Server-2005-and-2008Again.aspx

To run it against the databases, you can use another one of my scripts: http://weblogs.sqlteam.com/tarad/archive/2009/11/03/DefragmentingRebuilding-Indexes-in-SQL-Server-2005-and-2008Again.aspx


Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

chriztoph
Posting Yak Master

184 Posts

Posted - 2010-05-05 : 03:27:39
Last question for this post..

what is the best type of index for Foreign Keys?Clustered or Non-Clustered?
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-05-05 : 03:37:21
That is a strange question. You don't base the type of index on foreign keys. You base it on queries and data.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

chriztoph
Posting Yak Master

184 Posts

Posted - 2010-05-05 : 03:48:49
Ok thanks..
Go to Top of Page
   

- Advertisement -