Wrox Programmer Forums
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 27th, 2003, 10:36 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 149
Thanks: 0
Thanked 0 Times in 0 Posts
Default Query runs forever

This query has been running for 16 minutes now, and still has not returend anything. I am using MS SQL 2000 and running this inside SQL Query Analyzer. The same query runs in Access on an Access database in 30 seconds.

Any ideas what I did wrong in converting over to SQL?

SELECT dbo.tblArCust.CustId,
SUM(dbo.T_SetupSheetHistoryCombinationsDetail.Comb oLengthActual * dbo.T_SetupSheetHistoryProduction.QtyGood / 12 * dbo.vAvgWtPerFt_Setup_ECN_WOID_SubRev.AvgWt_Setup_ ECN_WOID_SubRev)
AS GoodLbs, SUM(dbo.T_SetupSheetHistoryCombinationsDetail.Comb oLengthActual * dbo.T_SetupSheetHistoryProduction.QtyGood / 12) AS GoodFeet,
SUM(dbo.T_SetupSheetHistoryProduction.TotalSetup / 60) AS SetupHrs, SUM(dbo.T_SetupSheetHistoryProduction.TotalRun / 60) AS RunHrs,
SUM(dbo.T_SetupSheetHistoryProduction.TotalDown / 60) AS DownHrs, SUM(dbo.T_SetupSheetHistoryProduction.QtyGood) AS SumOfQtyGood,
SUM(dbo.T_SetupSheetHistoryProduction.QtyRejected) AS SumOfQtyRejected,
SUM(dbo.T_SetupSheetHistoryCombinationsDetail.Comb oLengthActual * dbo.T_SetupSheetHistoryProduction.QtyRejected / 12) AS BadFeet,
SUM(dbo.T_SetupSheetHistoryCombinationsDetail.Comb oLengthActual * dbo.T_SetupSheetHistoryProduction.QtyRejected / 12 * dbo.vAvgWtPerFt_Setup_ECN_WOID_SubRev.AvgWt_Setup_ ECN_WOID_SubRev)
AS BadLbs, AVG(dbo.vAvgWtPerFt_Setup_ECN_WOID_SubRev.AvgWt_Se tup_ECN_WOID_SubRev) AS AvgOf_AvgWt_Setup_ECN_WOID_SubRev,
COUNT(dbo.T_SetupSheetHistoryProduction.Shift) AS CountOfShift, SUM(dbo.T_SetupSheetHistoryProduction.TotalScrapTi me / 60) AS ScrapHrs
FROM dbo.tblArCust INNER JOIN
dbo.T_SetupSheetHistoryProduction INNER JOIN
dbo.T_SetupSheetHistoryMaterialDetail ON dbo.T_SetupSheetHistoryProduction.WOIDSub = dbo.T_SetupSheetHistoryMaterialDetail.WOIDSub AND
dbo.T_SetupSheetHistoryProduction.Revision = dbo.T_SetupSheetHistoryMaterialDetail.Revision AND
dbo.T_SetupSheetHistoryProduction.ECN = dbo.T_SetupSheetHistoryMaterialDetail.ECN AND
dbo.T_SetupSheetHistoryProduction.WONum = dbo.T_SetupSheetHistoryMaterialDetail.WOID AND
dbo.T_SetupSheetHistoryProduction.QuoteID = dbo.T_SetupSheetHistoryMaterialDetail.QuoteID INNER JOIN
dbo.T_SetupSheetHistoryCombinationsDetail ON
dbo.T_SetupSheetHistoryProduction.WOIDSub = dbo.T_SetupSheetHistoryCombinationsDetail.WOIDSub AND
dbo.T_SetupSheetHistoryProduction.ECN = dbo.T_SetupSheetHistoryCombinationsDetail.ECN AND
dbo.T_SetupSheetHistoryProduction.WONum = dbo.T_SetupSheetHistoryCombinationsDetail.WOID AND
dbo.T_SetupSheetHistoryProduction.QuoteID = dbo.T_SetupSheetHistoryCombinationsDetail.QuoteID AND
dbo.T_SetupSheetHistoryProduction.ComboCustPartNum = dbo.T_SetupSheetHistoryCombinationsDetail.ComboCus tPartNum INNER JOIN
dbo.T_SetupSheetHistoryHeader ON dbo.T_SetupSheetHistoryProduction.WOIDSub = dbo.T_SetupSheetHistoryHeader.WOIDSub AND
dbo.T_SetupSheetHistoryProduction.Revision = dbo.T_SetupSheetHistoryHeader.Revision AND
dbo.T_SetupSheetHistoryProduction.ECN = dbo.T_SetupSheetHistoryHeader.ECN AND
dbo.T_SetupSheetHistoryProduction.WONum = dbo.T_SetupSheetHistoryHeader.WOID AND
dbo.T_SetupSheetHistoryProduction.QuoteID = dbo.T_SetupSheetHistoryHeader.QuoteID LEFT OUTER JOIN
dbo.vAvgWtPerFt_Setup_ECN_WOID_SubRev ON
dbo.T_SetupSheetHistoryProduction.WOIDSub = dbo.vAvgWtPerFt_Setup_ECN_WOID_SubRev.WOIDSub AND
dbo.T_SetupSheetHistoryProduction.WONum = dbo.vAvgWtPerFt_Setup_ECN_WOID_SubRev.WONum AND
dbo.T_SetupSheetHistoryProduction.Revision = dbo.vAvgWtPerFt_Setup_ECN_WOID_SubRev.Revision AND
dbo.T_SetupSheetHistoryProduction.ECN = dbo.vAvgWtPerFt_Setup_ECN_WOID_SubRev.ECN AND
dbo.T_SetupSheetHistoryProduction.QuoteID = dbo.vAvgWtPerFt_Setup_ECN_WOID_SubRev.QuoteID ON
dbo.tblArCust.CustId = dbo.T_SetupSheetHistoryHeader.CustId
WHERE (dbo.T_SetupSheetHistoryProduction.Date BETWEEN '1-1-1990' AND '1-1-2004')
GROUP BY dbo.tblArCust.CustId
__________________
Mitch
 
Old October 27th, 2003, 11:08 AM
sal sal is offline
Friend of Wrox
 
Join Date: Oct 2003
Posts: 702
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Too many joins!

How many records are you hitting on each table?

Sal
 
Old October 27th, 2003, 11:58 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 149
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Sal, if you mean number of records in each table, then:

tblArCust =1,366
T_SetupSheetHistoryMaterialDetail=7,127
T-SetupSheetHistoryProduction=37,289
T_SetupSheetHistoryCombinationsDetail=7,548
T_SetupSheetHistoryHeader=7,096
vAvgWtPerFt_Setup_ECN_WOID_SubRev=6,762

The Access query returns 168 records
 
Old October 27th, 2003, 11:59 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

Did you insure that all tables are properly indexed and that all referential (FOREIGN KEY) constraints are defined?

Jeff Mason
Custom Apps, Inc.
www.custom-apps.com
 
Old October 27th, 2003, 12:12 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 149
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Jeff, All my tables have indexes, however when I went back to compair them and the ones in Access some of the fields in MS SQL are not indexed like they are in Access. I can create thoese that are missing.

And, I am not sure what you mean by: "all referential (FOREIGN KEY) constraints are defined?" I know what a "foreign key" is (at least I think I do) but not how to define it.
 
Old October 27th, 2003, 01:42 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 149
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I ran that sp against the data that the upsize wizard crated that had all of the indexes made and it finally ran, but it still took 10 min 37 sec in side Query Anylizer!

Is there a better way to do this?
 
Old October 27th, 2003, 02:49 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

Quote:
quote:Originally posted by Mitch
 Jeff, All my tables have indexes, however when I went back to compair them and the ones in Access some of the fields in MS SQL are not indexed like they are in Access. I can create thoese that are missing.

And, I am not sure what you mean by: "all referential (FOREIGN KEY) constraints are defined?" I know what a "foreign key" is (at least I think I do) but not how to define it.
A FOREIGN KEY constraint (aka referential integrity constraint) defines a data relationship that exists between two tables. A foreign key in one table is typically a primary key in another. I say typically because in fact a foreign key constraint only need be a unique value (i.e. a column which has a unique index defined on it) in the other table. Of course, a primary key by definition is unique, so usually the constraints are set up referring to primary keys, but they don't have to be.

Anyway, the presence of a foreign key constraint insures that values in one table are guaranteed to be found in another.

The constraint is usually implemented (internally) by creating a unique index on the columns involved in the relationship. Then, the query processor can validate any update by doing a simple index lookup.

A side effect of this is that JOINs are made faster by the presence of these indexes. Since most often you construct JOINS based on such foreign key relationships, when you declare the relationship you get better performing JOINs "for free".

You can declare a foreign key relationship most easily by using the database diagram and clicking on a table's primary key column and dragging it to the referring table. A dialog will pop up that lets you define the columns involved in the relationship.

You can also use the ALTER TABLE ADD CONSTRAINT command, as, for example:
Code:
ALTER TABLE yourtable ADD 
    CONSTRAINT FK_yourconstraintname FOREIGN KEY 
    (someforeignkeycolumn)
    REFERENCES someothertable
        (someothertableprimarykey);
I have found that the upsizing wizard does not always do the best possible job defining indexes and constraints - you need to look the resultant table definitions over carefully to insure that all the indexes that you need have been properly defined.

Run the query in query analyzer again, and before you do, click Query->Show Execution Plan. After the query runs, inspect the tab labeled "Execution Plan". This shows what operations the query processor does to execute your query. In your case it will no doubt be somewhat complex. Look for operations labeled "table scan". These are sequential reads throughout the entire contents of a table (and obviously can be slow if the table is large). Look at the columns involved in the scan and consider defining an index on them.

You may also want to consider adding indexes on columns used in WHERE clauses, as this may help the process of selecting which rows are involved in JOINs. This can be overdone, as there is overhead involved in maintaining an index whenever a row is inserted, updated, or deleted.

You could also try running the "Index tuning wizard" located in the "query" menu in Query Analyzer. I have had mixed results using this tool, but it may be worth a try...


Jeff Mason
Custom Apps, Inc.
www.custom-apps.com
 
Old October 27th, 2003, 03:43 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 149
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Ok, I went from 16 min to 1 min 25 sec by removing the 3 sorts from the sub query: vAvgWtPerFt_Setup_ECN_WOID_SubRev

That's crazy to me becasue that view only takes 2 seconds to run!!

1.25 min is still better then 10 but not the 30 seconds of Access and not the split second I excpected by using MS SQL.

I'll look at the indiexes again, and go from there.
 
Old October 27th, 2003, 03:48 PM
sal sal is offline
Friend of Wrox
 
Join Date: Oct 2003
Posts: 702
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Try using a stored procedure. they do run faster because they stay in memory and keep thei execution plan.
Also make sure that you use the correct field data types. Access is bad about using longer data types during upsizing than you really need.

Sal
 
Old October 27th, 2003, 04:09 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 149
Thanks: 0
Thanked 0 Times in 0 Posts
Default

But how do I use a SP as a sub query? I used the View because It would allow me to use it.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Blocking a process before it runs asidre Other Programming Languages 0 August 11th, 2006 01:35 AM
Class Method takes Forever to run. hexOffender VB.NET 2002/2003 Basics 1 April 11th, 2006 09:18 AM
xp_sendmail executes forever bsrini SQL Server 2000 2 March 14th, 2006 06:56 PM
Query runs in SQL Server but not in ASP Ron Howerton ASP.NET 1.0 and 1.1 Basics 2 July 14th, 2003 04:33 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.