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
 query analyzer hangs

Author  Topic 

pitmet
Starting Member

17 Posts

Posted - 2011-01-05 : 18:09:24
Can anyone tell me why query analyzer hangs on the following query? I have to use task manager to kill it. It's the compute sum statement that hangs things up but I need to have the total value for each account in a single row. I've tried summing in the select statement but it returns all rows of data for each account instead of grouping.Thanks in advance. The query:

select s.meta_ID, a.address1a, a.address1b, a.city1, a.state1, a.zip1,a.badaddress1,a.mailflag,p.lastname,p.firstname,
s.IDtype,s.stattype,s.gamingdate,s.Twin
from Cds_Account a (nolock)

join Cds_Player p (nolock)
on a.account_ID=p.account_ID

join Cds_Statday s (nolock)
on p.account_ID=s.meta_ID

where s.meta_ID=a.account_ID and s.IDtype='p'
and s.meta_ID=P.account_ID and s.IDType='p'

and s.meta_ID=a.account_ID and a.address1a is not null
and s.meta_ID=a.account_ID and a.address1a<>''

and a.state1='AZ' or a.state1='Arizona'

and s.meta_ID=p.account_ID and s.stattype like 'SL%'

and a.mailflag= 'Y'
and a.badaddress1 = 'N'
and s.gamingdate>= '12/25/10'
and
s.twin>=10

group by s.meta_ID,a.address1a,a.address1b,a.city1,a.state1,a.zip1,a.badaddress1, a.mailflag,p.lastname,p.firstname, s.IDType,s.stattype,s.twin,s.gamingdate
order by s.meta_ID
compute sum(s.twin) by s.meta_ID




MET

MIK_2008
Master Smack Fu Yak Hacker

1054 Posts

Posted - 2011-01-06 : 00:48:39
Try to execute it with TOP for few row of the data and check as if it hangs for that too ? e.g.

Select Top 5 .....
....
....

Make sure that you are pressing the Execute Button instead of Debug.... Some time when i click it it takes minutes for a query which could be executed in seconds .. so i feel the same..

Cheers!
Go to Top of Page

MIK_2008
Master Smack Fu Yak Hacker

1054 Posts

Posted - 2011-01-06 : 00:54:53
Also, I don't know why are you using the Group by clause and Compute Sum .... as there is no aggregate function used in the Select so seems meaningless for me ... Check the following query if this works (i have removed the unnecessary checks in the where clause which are already imposed in the Joining by the ON operator)

select
s.meta_ID, a.address1a, a.address1b, a.city1, a.state1, a.zip1,a.badaddress1,a.mailflag,p.lastname,p.firstname,
s.IDtype,s.stattype,s.gamingdate,s.Twin
from Cds_Account a (nolock)
INNER join Cds_Player p (nolock) on a.account_ID=p.account_ID
INNER join Cds_Statday s (nolock) on p.account_ID=s.meta_ID
where s.IDtype='p'
and a.address1a is not null and a.address1a<>''
and (a.state1='AZ' or a.state1='Arizona')
and s.stattype like 'SL%'
and a.mailflag= 'Y'
and a.badaddress1 = 'N'
and s.gamingdate>= '12/25/10'
and s.twin>=10

Cheers!
MIK

Go to Top of Page

pitmet
Starting Member

17 Posts

Posted - 2011-01-06 : 02:05:48
quote:
Originally posted by MIK_2008

Also, I don't know why are you using the Group by clause and Compute Sum .... as there is no aggregate function used in the Select so seems meaningless for me ... Check the following query if this works (i have removed the unnecessary checks in the where clause which are already imposed in the Joining by the ON operator)

select
s.meta_ID, a.address1a, a.address1b, a.city1, a.state1, a.zip1,a.badaddress1,a.mailflag,p.lastname,p.firstname,
s.IDtype,s.stattype,s.gamingdate,s.Twin
from Cds_Account a (nolock)
INNER join Cds_Player p (nolock) on a.account_ID=p.account_ID
INNER join Cds_Statday s (nolock) on p.account_ID=s.meta_ID
where s.IDtype='p'
and a.address1a is not null and a.address1a<>''
and (a.state1='AZ' or a.state1='Arizona')
and s.stattype like 'SL%'
and a.mailflag= 'Y'
and a.badaddress1 = 'N'
and s.gamingdate>= '12/25/10'
and s.twin>=10

Cheers!
MIK





I know there was redundancy in the where clause but that was because it was not filtering out the boolean flags and I thought perhaps with redundancy it may aleviate that problem. I'm using a group by because I need the "s.twin" value field summed for each account and since I'm using a sum function I have to group and also I want it grouped by s.meta_ID. Your code would not give me the desired results. Thanks anyway.
Regards.....

MET
Go to Top of Page

pitmet
Starting Member

17 Posts

Posted - 2011-01-06 : 02:11:33
I should also let you guys know that for every account(meta_ID) there are maybe a thousand transactions - hence my need for a summation and group.

MET
Go to Top of Page

pitmet
Starting Member

17 Posts

Posted - 2011-01-06 : 18:00:29
Nobody has any ideas as to why this would hang query analyzer or is there another way to sum and group by meta_ID in this query? Summing in the select clause doesn't get me a single row for each account.

MET
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2011-01-06 : 18:16:58
Query Analyzer is obsolete, so there may not be many people here who use it, or have an opinion on the matter. I use it, it crashes several times a week, I ignore it and restart it.

Maybe your query is failing at the server end, and QA is sitting patiently waiting for a reply. Anything in the server's LOG folder - particularly an exception "crash dump" report? I presume if you are using QA that you are using SQL 2000 and that used to cough at a number of complex queries (but raised an exception report when it did) - if you are not on SP4 then it would be worth considering applying that. (But maybe you are connecting QA to a later version of SQL - that's what I do)

Can you fudge it with:

select s.meta_ID, a.address1a, a.address1b, a.city1, a.state1, a.zip1,a.badaddress1,a.mailflag,p.lastname,p.firstname,
s.IDtype,s.stattype,s.gamingdate,s.Twin
INTO #TEMP
from Cds_Account a (nolock)
...
SELECT * FROM #TEMP

??

And you know that the NOLOCK hint is the work of the devil? I don't suppose that is the cause because the side effects of NOLOCK are virtually unrepeatable, so it would be unlikely to cause the same effect twice, but as a separate issue if this is a query intended to be used by end users, not solely DBAs who know the consequences of NOLOCK, then there is a high risk that those users will be making business decisions based on inaccurate data - NOLOCK retrieves dirty data that is inappropriate for a report such as this very infrequently, but it does happen.
Go to Top of Page

pitmet
Starting Member

17 Posts

Posted - 2011-01-07 : 08:15:29
quote:
Originally posted by Kristen

Query Analyzer is obsolete, so there may not be many people here who use it, or have an opinion on the matter. I use it, it crashes several times a week, I ignore it and restart it.

Maybe your query is failing at the server end, and QA is sitting patiently waiting for a reply. Anything in the server's LOG folder - particularly an exception "crash dump" report? I presume if you are using QA that you are using SQL 2000 and that used to cough at a number of complex queries (but raised an exception report when it did) - if you are not on SP4 then it would be worth considering applying that. (But maybe you are connecting QA to a later version of SQL - that's what I do)

Can you fudge it with:

select s.meta_ID, a.address1a, a.address1b, a.city1, a.state1, a.zip1,a.badaddress1,a.mailflag,p.lastname,p.firstname,
s.IDtype,s.stattype,s.gamingdate,s.Twin
INTO #TEMP
from Cds_Account a (nolock)
...
SELECT * FROM #TEMP

??

And you know that the NOLOCK hint is the work of the devil? I don't suppose that is the cause because the side effects of NOLOCK are virtually unrepeatable, so it would be unlikely to cause the same effect twice, but as a separate issue if this is a query intended to be used by end users, not solely DBAs who know the consequences of NOLOCK, then there is a high risk that those users will be making business decisions based on inaccurate data - NOLOCK retrieves dirty data that is inappropriate for a report such as this very infrequently, but it does happen.



Your're right we are on 2000 and no this is not an end user query. I was not aware that nolock had these issues. So what't the alternative to possibly locking transaction tables? I think we will be going to 2008 in the near future but for now I have to use what we have. I will try your suggestion of using a temp table when I return to work on Monday - that may work.I did not find anything in the servers log folder and thought that was odd. Thanks for your reply.

MET
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2011-01-07 : 12:30:36
"I was not aware that nolock had these issues"

You have few choices for tackling such issues in SQL 2000. If you move to SQL2005 (or later) then have a look at READ_COMMITED_SNAPSHOT - that is a database-level setting (so no hints required per-query) and prevents reads blocking writes (which is mostly what NOLOCK was used for in SQL2000 and earlier).

Beware that:

NOLOCK will include a row that has been inserted/updated which is subsequently rolled back (conventional stuff, so most probably you are aware of that, and usually that is not too much of an issue - someone might have just added a record that they delete in two minutes time anyway ... so same difference [except where related records are not-yet-present for that incomplete data perhaps])

However, fewer people are aware of the problems that arise when index pages are split. A new entry is added to an index, the page is already full so it is split into two. At that moment in time, using NOLOCK, you may get the original page, the original page now only containing half the records (and then not get the second page 'coz its not yet written) [so you are missing some rows] or you might get the original "full" first page and ALSO the second half-filled index page [so you are getting some rows twice].

This is, as you can imagine, very hard to reproduce and very hard to QA for. Normally the symptoms are "I got duplicate rows on my report" and no one can reproduce the problem so it is ignored. Or the user just presses refresh and the problem apparently disappears ...

Not good if business decisions are made on data displayed in such (thankfully) rare scenarios. It tends to erode trust / confidence in the Systems people though ("This darn error keeps happening, IT never manage to fix and and think we are making it up ..." so they stop telling you about it, and probably other things to - although I'm probably painting the picture blacker than it actually is !)
Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2011-01-07 : 12:53:23
There are also issues with Read Committed and Repeatable Read that no one ever seems to talk about. Also, NOLOCK has it's place, but you need to be aware of the issues so you don't use it in the wrong place.
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2011-01-07 : 12:58:05
"There are also issues with Read Committed and Repeatable Read that no one ever seems to talk about"

I don't use them, but I'd be keen to know of the issues so I am aware of them - if you have a link (or can be bothered to knock up a short description), thanks
Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2011-01-07 : 13:07:26
Well a quick look at BOL can tell you that Read Commited allows for Nonrepeatable Reads and Phanton and Repeatable Read will alow Phantom reads.

But, more to your point here are some links:
http://sqlblog.com/blogs/alexander_kuznetsov/archive/2009/04/10/selects-under-read-committed-and-repeatable-read-may-return-incorrect-results.aspx

http://blogs.msdn.com/b/craigfr/archive/2007/05/02/query-plans-and-read-committed-isolation-level.aspx
Go to Top of Page

pitmet
Starting Member

17 Posts

Posted - 2011-01-13 : 15:01:44
Kristen,
I tried using a temp table and inserting nearly a million rows was relativley quick but when I went to compute sum by account_ID on the temp table I got the same response - the query just ran forever - never giving a result set. I don't know what gives with this issue. Luckily I have a work around which necessitates a clean up in Access but it works. Wish I could find a way to make the query work. I have never seen a compute sum hang up a query like this.

MET
Go to Top of Page

pitmet
Starting Member

17 Posts

Posted - 2011-01-13 : 22:09:26
Should I move this topic to another forum, like maybe sql 2000 programming or something? Looks like I need some sql guru help here.

MET
Go to Top of Page

MIK_2008
Master Smack Fu Yak Hacker

1054 Posts

Posted - 2011-01-14 : 00:02:12
I am not sure if someone can exactly identify the reason for your issue as we can just guess upon the given information .. there might be some thing which we are missing ... If you can provide some sample data of the tables in joins along with your desired output, we will be able to check your query against that data and may be some of us will let you know if its the problem in Query or anything else!


Cheers!
MIK
Go to Top of Page

pitmet
Starting Member

17 Posts

Posted - 2011-01-14 : 09:32:48
quote:
Originally posted by MIK_2008

I am not sure if someone can exactly identify the reason for your issue as we can just guess upon the given information .. there might be some thing which we are missing ... If you can provide some sample data of the tables in joins along with your desired output, we will be able to check your query against that data and may be some of us will let you know if its the problem in Query or anything else!


Cheers!
MIK



I won't be able to do anything until after Monday as that is a holiday here and I am normally off Friday through Sunday but I will attempt to get you the information as soon as I am back in my office. Being a gaming operation I cannot access the server from home. Thanks..


MET
Go to Top of Page

russell
Pyro-ma-ni-yak

5072 Posts

Posted - 2011-01-14 : 09:55:14
Shouldn't be using compute sum anymore.

I would also try to do the rollup in the front end rather than in the query. If, for some reason, you must perform the rollup in the query, try aggreegating AFTER the data is retrieved.

1. select the data (without the rollup) into a temp table.
2. index the temp table
3. perform aggregations against the temp table and return result.

Anyway, first and foremost:

Are all the columns in your join conditions indexed? And are their statistics up to date?
Same question for all of the columns in the WHERE clause.
Go to Top of Page

pitmet
Starting Member

17 Posts

Posted - 2011-01-15 : 08:09:08
You say shouldn't be using compute sum anymore -why? I'm attempting to get a summation of twin for each Id so that the twin field has a total for each individual ID in one row. Also I'm not sure what you mean by rollup. But I did pull all the records and put them in a temp table but I didn't index the temp table. All the joins are indexed but not sure about the where clauses will have to check data dictionary Tuesday. I was able to fetch almost a million records into the temp table in less than a minute but when I attempted the compute sum(twin) by ID against the temp table the query hung. I don't know of another way to get the desired result without using comput sum????

MET
Go to Top of Page

MIK_2008
Master Smack Fu Yak Hacker

1054 Posts

Posted - 2011-01-15 : 12:13:14
Met: run the below query in the respective database and post the result set ..just want to know how many rows are there in the table based on specified conditions ... I don't think if it really needed to introduce Indexes at this stage ... as you are saying that the query is not giving any result at all ...


select
Count(1)

from Cds_Account a (nolock)
INNER join Cds_Player p (nolock) on a.account_ID=p.account_ID
INNER join Cds_Statday s (nolock) on p.account_ID=s.meta_ID
where s.IDtype='p'
and a.address1a is not null and a.address1a<>''
and (a.state1='AZ' or a.state1='Arizona')
and s.stattype like 'SL%'
and a.mailflag= 'Y'
and a.badaddress1 = 'N'
and s.gamingdate>= '12/25/10'
and s.twin>=10

Go to Top of Page

russell
Pyro-ma-ni-yak

5072 Posts

Posted - 2011-01-16 : 11:02:13
MIK, millions of rows and you don't think indexes will help? I beg to differ.

Pitmet, Compute sum is deprecated and will be removed. See this regarding compute sum/Rollup.
Go to Top of Page

MIK_2008
Master Smack Fu Yak Hacker

1054 Posts

Posted - 2011-01-17 : 01:42:55
@Russell, I admit that indexes improves performance and is one of the Main Factor in Performance Optimization. For me its not wise to implement indexes in an OLTP sort of System, unless you don't have any other choice. By this i do not mean that i disagree with you regarding the performance improvement of a query by implementing a missing index. I just meant that without knowing the cause of the problem we should not jump onto the index implementation .. as it has its own side Effects (in case of OLTP systems).

Reason due to which I disagreed with index strategy is the OP's statement "the query just ran forever - never giving a result set". Because of this I said that the Indexes dont seems to be a matter at this stage ... We use indexes in order to reduce the overall cost of a query (simply to optimize it). But in this case the query is not giving any result at all... even after a minute, two, ten, or an hour.

Thus for me there are two possible reasons of this issue!

1) Locking of the in-use table
2) Query problem
Go to Top of Page
    Next Page

- Advertisement -