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
 Please Help make this more efficient

Author  Topic 

mrtweaver
Yak Posting Veteran

67 Posts

Posted - 2009-05-06 : 13:04:34
I have the following queries.
                           View: DJT_MECH_REPORT

SELECT machine, start, shift, mech1, mech2, verb1, prod_date
FROM dbo.DJT_MECH_PAGED
UNION ALL
SELECT machine, start, shift, mech1, mech2, verb1, prod_date
FROM dbo.DJT_SHOW_MECH_ARRIVE
UNION ALL
SELECT TOP (100) PERCENT machine, start, shift, mech1, mech2, verb1, prod_date
FROM dbo.DJT_Mech_show_logout
ORDER BY machine, start, shift

===================================================================== View: DJT_MECH_PAGED

SELECT TOP (100) PERCENT dbo.Prodtrack.machine, dbo.Prodtrack.shift, dbo.Prodtrack.start, dbo.Prodtrack.mechpg, dbo.Prodtrack.mech1,
dbo.Prodtrack.mech2, CASE WHEN dbo.Prodtrack.mechpg = 0 THEN 'pageinit' END AS verb1, dbo.Prodtrack.prod_date
FROM dbo.DJT_SHOW_MECH_PAGE_RESP INNER JOIN
dbo.Prodtrack ON dbo.DJT_SHOW_MECH_PAGE_RESP.machine = dbo.Prodtrack.machine AND
dbo.DJT_SHOW_MECH_PAGE_RESP.shift = dbo.Prodtrack.shift AND dbo.DJT_SHOW_MECH_PAGE_RESP.start = dbo.Prodtrack.stop
WHERE (dbo.Prodtrack.mechpg = 0) AND (dbo.Prodtrack.mech1 = 0) AND (dbo.Prodtrack.status <> 'lgin')
ORDER BY dbo.Prodtrack.machine, dbo.DJT_SHOW_MECH_PAGE_RESP.start

==================================================================View: DJT_SHOW_MECH_ARRIVE

SELECT TOP (100) PERCENT dbo.Prodtrack.machine, dbo.Prodtrack.start, dbo.Prodtrack.mech1, dbo.Prodtrack.shift, dbo.Prodtrack.mechpg,
dbo.Prodtrack.mech2, CASE WHEN dbo.Prodtrack.operator != 0 AND
dbo.Prodtrack.mech1 != 0 THEN 'Respond' WHEN dbo.prodtrack.jobtask = 9999999999 AND dbo.prodtrack.operator = 0 AND
dbo.prodtrack.mech1 <> 0 THEN 'Maint.' ELSE 'Setup' END AS verb1, dbo.Prodtrack.operator, dbo.Prodtrack.prod_date
FROM dbo.Prodtrack INNER JOIN
dbo.Prodtrack AS Prodtrack_1 ON dbo.Prodtrack.machine = Prodtrack_1.machine AND dbo.Prodtrack.prod_date = Prodtrack_1.prod_date AND
dbo.Prodtrack.start = Prodtrack_1.stop
WHERE (dbo.Prodtrack.mech1 <> 0) AND (Prodtrack_1.mech1 = 0)
ORDER BY dbo.Prodtrack.machine, dbo.Prodtrack.start

==================================================================View: DJT_SHOW_MECH_LOGOUT

SELECT TOP (100) PERCENT dbo.Prodtrack.machine, dbo.Prodtrack.shift, dbo.Prodtrack.start, dbo.Prodtrack.mechpg, CASE WHEN dbo.Prodtrack.mechpg = 0 AND
dbo.Prodtrack.mech1 = 0 THEN 'Logout' ELSE 'END' END AS verb1, dbo.Prodtrack.mech1, dbo.Prodtrack.mech2, dbo.Prodtrack.prod_date
FROM dbo.Prodtrack INNER JOIN
dbo.DJT_SHOW_MECH_PAGE_RESP ON dbo.Prodtrack.machine = dbo.DJT_SHOW_MECH_PAGE_RESP.machine AND
dbo.Prodtrack.shift = dbo.DJT_SHOW_MECH_PAGE_RESP.shift AND dbo.Prodtrack.start = dbo.DJT_SHOW_MECH_PAGE_RESP.stop
WHERE (dbo.Prodtrack.mechpg <> 1) AND (dbo.Prodtrack.mech1 = 0) OR
(dbo.Prodtrack.status = 'lgof') AND (dbo.Prodtrack.logged <> N' qc ')
ORDER BY dbo.Prodtrack.machine, dbo.Prodtrack.start





View: DJT_SHOW_MECH_PAGE_RESP

SELECT TOP (100) PERCENT machine, shift, start, stop, mechpg, mech1, mech2, prod_date, CASE WHEN t1.mech1 = 0 AND
t1.mechpg = 1 THEN 'LiteOn' WHEN t1.mech1 > 0 AND t1.mechpg = 1 THEN 'MechResp' WHEN t1.mech1 > 0 AND t1.mechpg = 0 AND
status = 'byps' THEN 'Bypass' ELSE t1.status END AS verb1
FROM dbo.Prodtrack AS t1
WHERE (mechpg = 1) OR
(mechpg = 0) AND (mech1 <> 0)
ORDER BY machine, start, stop, shift



Prodtrack is an actual table all these other items are views.


When I attempt to run the DJT_MECH_REPORT view, it takes 20 seconds or longer, when I attempt to open this view for modification I get the unable to parser text message. Not sure if this parser error is related to the slow time it takes to run this view.

All the other views I can run independantly, I can open them with no problems, and eacy of them take less than 3 seconds.

I wrote these views just as seen here, but I am pretty sure there must be a more efficient way. So I am asking the experts to look these over, critiqe them, provide feedback, and assist me in getting this things to run more efficient.

Thanks and have a great day.

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-05-06 : 13:06:24
What indexes do you have on the underlying tables? Hopefully you have indexes on all of the join columns.

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

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2009-05-06 : 13:15:49
Might help to take the TOP and ORDER BY clauses out of the view definitions, it's certainly not speeding up the query.
Go to Top of Page

mrtweaver
Yak Posting Veteran

67 Posts

Posted - 2009-05-06 : 14:01:38
As for the order by I can take those out, however everytime I try to take the TOP statements out, SQL places them right back in, I have tried to take them out, save the views, then execute the query, and as soon as I execute or open to modify the TOP is right back in there.

quote:
Originally posted by robvolk

Might help to take the TOP and ORDER BY clauses out of the view definitions, it's certainly not speeding up the query.

Go to Top of Page

mrtweaver
Yak Posting Veteran

67 Posts

Posted - 2009-05-06 : 14:07:32
If you look closely at the queries all queries here are views with the exception of the one(s) that have a reference to table name: PRODTRACK. And I have checked my PRODTRACK table against the view(s) to ensure that what the views require in the WHERE clause is indexed. And it is. I have a index which contains mechpg, mech1, and status, which if I understand the way SQL works since all three views reference these columns in one way or another then this index should work for all three views.

There are other views that used a different set of WHERE conditions and their is also a INDEX for those in the PRODTRACK table.


quote:
Originally posted by tkizer

What indexes do you have on the underlying tables? Hopefully you have indexes on all of the join columns.

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

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."

Go to Top of Page

whitefang
Enterprise-Level Plonker Who's Not Wrong

272 Posts

Posted - 2009-05-06 : 14:11:16
Why do you need a TOP 100 PERCENT in the DJT_MECH_REPORT query?
Go to Top of Page

mrtweaver
Yak Posting Veteran

67 Posts

Posted - 2009-05-06 : 14:36:31
ASK Microsoft?? As I said SQL Server keeps placing it there not me. I do all my programming what limited amount I know thru the Studio Manager GUI.

quote:
Originally posted by whitefang

Why do you need a TOP 100 PERCENT in the DJT_MECH_REPORT query?

Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2009-05-06 : 14:44:48
If you include an ORDER BY clause in a view definition, SQL Server requires a TOP PERCENT.
Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2009-05-06 : 14:46:09
quote:
I do all my programming what limited amount I know thru the Studio Manager GUI.
Don't use the view designer in Management Studio. Or if you must, script out the view definition, then change it to remove the TOP and ORDER BY clauses, then recreate/alter the view.
Go to Top of Page

mrtweaver
Yak Posting Veteran

67 Posts

Posted - 2009-05-08 : 10:56:43
Ok I have taken out the TOP and the ORDER statement and it did not really help. If I run the following query:

select machine,start,shift,mech1,mech2 from djt_mech_report as i where datediff(d,i.prod_date,'{manual entered date}')=0 and shift =1

this query takes about 3 seconds and generates good results. If I add to the query and make it look like this:

select machine,start,shift,mech1,mech2 from djt_mech_report as i where datediff(d,i.prod_date,'{manual entered date}')=0 and shift =1 and machine = 13

This query runs in about the same time frame. However is I change the query and delete all but the datediff after the where statement. Like follows:

select machine,start,shift,mech1,mech2 from djt_mech_report as i where datediff(d,i.prod_date,'{manual entered date}')=0

Now when I run this query it takes 30 seconds or longer to execute. This is something I can not explain. Was hoping one of you could explain it. Our IT guys think that since their is a table which feeds the three view that are then unioned together to create one view, that if you attach to the unioned view and use somehting that is non indexed on the main feeding table that that will cause this. I dont know but I dont want to just pick at straws. Would like an experts answer.

Thanks and have a great day.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-05-08 : 11:19:26
do you have an index on i.prod_date field? also what determines value of manually entered date?
Go to Top of Page

mrtweaver
Yak Posting Veteran

67 Posts

Posted - 2009-05-08 : 11:30:49
Yes I do have a Index on i.prod_date, it is non clustered, asc.

As for what determines the manually entered data, well I just pick a date and type it in. For testing purposes.

Now a new question arrises, sometimes in the ads at the top of this forum you will see one for SQL Check. Our IT dept downloaded that and ran it and it appears we have a large number of LOCK WAITS and LOCK TIMEOUTS on the database in question. Yet on other SQL databases we have none of these. SInce this is our data collection server it gets hit pretty hard. The IT dept thinks that by default only one application/event can open a database table at any one point in time. So is it true that by default you only get a 1 to 1 relationship? (1 event to 1 table).


quote:
Originally posted by visakh16

do you have an index on i.prod_date field? also what determines value of manually entered date?

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-05-08 : 12:35:58
then replace

where datediff(d,i.prod_date,'{manual entered date}')=0

by

where i.prod_date>=dateadd(d,datediff(d,0,'{manual entered date}'),0)
and i.prod_date< dateadd(d,datediff(d,0,'{manual entered date}')+1,0)


and try query
Go to Top of Page

mrtweaver
Yak Posting Veteran

67 Posts

Posted - 2009-05-08 : 13:42:53
Nope, with this adaptation now I get server timeouts, unless I add something extra such as AND SHIFT =1. If I add that then it only takes a couple seconds. We did however find that we were getting a lot of lock error. Our IT staff downloaded Deadlock Detector and we will try using that to find out where the problems are.



quote:
Originally posted by visakh16

then replace

where datediff(d,i.prod_date,'{manual entered date}')=0

by

where i.prod_date>=dateadd(d,datediff(d,0,'{manual entered date}'),0)
and i.prod_date< dateadd(d,datediff(d,0,'{manual entered date}')+1,0)


and try query

Go to Top of Page
   

- Advertisement -