Solved

SQL 2005 subquery

Posted on 2009-07-08
12
265 Views
Last Modified: 2012-05-07
The following query times out in SQL 2005 and does not time out in SQL 2000.
select opbud_temp_accruals.rptg_cc,
opbud_temp_accruals.service_date,
opbud_temp_accruals.loe
from opbud_temp_accruals
where opbud_temp_accruals.rptg_cc + convert(varchar(6),opbud_temp_accruals.service_date,112) <> all
(select opbud_hist.rptg_cc + convert(varchar(6),opbud_hist.prod_date,112)
from opbud_hist)

The workaround to get it not to time out in SQL 2005 is to put my subquery in a temp table.
select opbud_temp_accruals.rptg_cc,
opbud_temp_accruals.service_date,
opbud_temp_accruals.loe
from opbud_temp_accruals
where opbud_temp_accruals.rptg_cc + convert(varchar(6),opbud_temp_accruals.service_date,112) <> all
(select rptg_cc_date from #opbud_hist_temp)

Is this a known bug in SQL 2005?  I could not find the answer anywhere.

Thanks
0
Comment
Question by:cogc_it
  • 5
  • 4
  • 3
12 Comments
 
LVL 39

Expert Comment

by:BrandonGalderisi
ID: 24804205
If the database was taken from 2000 and restored to 2005, did you reindex your database?  SQL Server 2005's query optimizer works much differently than SQL Server 200's and queries will not necessarily perform in an identical manner.

One thing toe mention is that your conversions:

convert(varchar(6),opbud_temp_accruals.service_date,112)
convert(varchar(6),opbud_hist.prod_date,112)

Will not utilize any indexes.  So there will be a table scan on opbud_temp_accruals.
0
 

Author Comment

by:cogc_it
ID: 24804589
I rebuild the index and it still didn't work.  As far as the conversions you mentioned, the same query works in 2000 and it takes only 4 seconds to run but times out in 2005.  Is there something different in 2005?
0
 
LVL 39

Expert Comment

by:BrandonGalderisi
ID: 24805712
Is "all" a column in one of your tables?  

What are your table/index structures?
0
 

Author Comment

by:cogc_it
ID: 24806591
All is a SQL function.  Rptg_CC and Prod_Date are my keys for both tables and indexes.  
0
 
LVL 39

Expert Comment

by:BrandonGalderisi
ID: 24806717
What if you change it to a not exists.


Also, try looking at your execution plan and see where the query time is being spent.
select opbud_temp_accruals.rptg_cc,

opbud_temp_accruals.service_date,

opbud_temp_accruals.loe

from opbud_temp_accruals

where not exists

     (select 1 from opbud_hist

     where opbud_hist.rptg_cc + convert(varchar(6),opbud_hist.prod_date,112) = opbud_temp_accruals.rptg_cc + convert(varchar(6),opbud_temp_accruals.service_date,112) 

     )

     

Open in new window

0
 
LVL 6

Expert Comment

by:IncisiveOne
ID: 24809909
Exactly.

There is also a problem with taking two existing columns:
opbud_temp_accruals.rptg_cc + convert(varchar(6),opbud_temp_accruals.service_date,112)
and concatenating them into one column in the temp table:
rptg_cc_date
This will create problems where there are none.  One example of many such problems is inability to use the extant indices.  It may "look like" a problem with SQL 2005, since it was "working" in 2000, but th efact is, it always was sub-standard and very poor implementation that performed badly in 2000, and performed abyssmally in 2005.  It is not a bug or  problem or limitation in SQL 2005; it is a huge bug in your code, which showed up with the new optimiser.  No vendor promises backward compatibility for buggy code.

Cheers

0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 6

Expert Comment

by:IncisiveOne
ID: 24813951
> Exactly.

That referred to Brandon's first post.  Didn't realise there would be so many intervening posts.

> Also, try looking at your execution plan and see where the query time is being spent.

Good point.  But I think we know that both tables are being table scanned due to inability to use the indices due to mismatched datatypes.  And the exorbitant time reported.  Although it would confirm it to OP.  The temp table is going to be a dog.

Cheers
0
 

Author Comment

by:cogc_it
ID: 24813985
The code is to find out if records from one table exist in another table.  The concatenating of two existing columns and inserting them into a temp table is the workaround for 2005.  You can only concatenate if the data exists so there is no problem when the data is not there.  This is basic SQL and is not substandard or buggy code.  
0
 

Author Comment

by:cogc_it
ID: 24814053
Once again, the temp table works for 2005.  I will contact Microsoft concerning this issue.
0
 
LVL 39

Accepted Solution

by:
BrandonGalderisi earned 350 total points
ID: 24814849
>This is basic SQL and is not substandard or buggy code.

I have to disagree.  And PLEASE don't take offense but doing comparisons on concatenated columns (A+B = A+B) is substandard.

This change may at least allow an index scan to be performed instead of a table scan assuming that the rptg_CC is indexed in both locations and includes the prod_date/service_date accordingly.
select opbud_temp_accruals.rptg_cc,

opbud_temp_accruals.service_date,

opbud_temp_accruals.loe

from opbud_temp_accruals

where not exists

     (select 1 from opbud_hist

     where opbud_hist.rptg_cc = opbud_temp_accruals.rptg_cc 

     and convert(varchar(6),opbud_hist.prod_date,112) = convert(varchar(6),opbud_temp_accruals.service_date,112) 

     )

     

Open in new window

0
 
LVL 6

Expert Comment

by:IncisiveOne
ID: 24820172
> This is basic SQL and is not substandard or buggy code.

Let's keep personal feelings out of this; it is a technical problem we are dealing with.  The concept of standards is that they are beyond the opnion of coders, and even experts.  I am not going to list all the (a) table design (b) SQL use (c) programming method used, that break standards; or why standards exist.  The simple fact is, if (a) (b) and (c) were even reasonably compliant:
- the problem would not exist
- you would not have different behaviour in different version of MS SQL
- you would not be seeking help

The fact that you have to change code to make it work is ready, indisputable evidence that it is a coding error (examination will lead to (a) (b) and (c) errors as well).  That you had substandard code working in SQL 2000 does not make it acceptable or standard (they were many workarounds to SQL 2000 limitations, and that which you chose was, and remains substandard).

Simply put standard-compliant tables, SQL, and code is impervious to SQL version changes.  That's just one  value of standards.

The requirment here (and originally) was simple SQL, the implementation, by evidence, is substandard and buggy code.
0
 

Author Comment

by:cogc_it
ID: 24823013
I was not seeking help on the coding.  I kept my old code to put it in the temp table.  My original question was when I put the subquery in a temp table it did not time out in 2005 versus not putting it in the temp table.  By putting it in temp table and also using not exists as suggested I get the same results set and both return results in 4 seconds.  I did try not exists before I posted my question but I was concatenating my columns just as Brandon as suggest initially.  I gave the points to Brandon because his suggestion did work, it didn't mean I used his suggestion.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now