473,473 Members | 2,179 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

IN (subquery) slower than calculation @VAL and then IN (@VAL)?

-- I have a situation where doing

-- first example
-- 1. Get series of values througha query into a string (@val)like
'1,2,3,4':

declare @val varchar(4000)
select @Val = @val + cast(myval as varchar) + ',' -- myval is an
integer variable
from xyz
where xyz.field = 33

SET @val = left(@val, len(@val) - 1)

-- 2. EXEC a query using IN (' + @val + ')'

EXEC('
select *
from qpr
where qpr.fieldx IN (' + @val + ')
')

-- is much faster than doing

-- second example
select *
from qpr
where qpr.fieldx IN (select myval
from xyz
where xyz.field = 33)

-- Since second example does not have a correlateed query, why is it
slower?

-- Thanks in advance,
-- Caveman

Jul 23 '05 #1
6 1590
I'm not an expert but using subquery is usually slower. Depends on
slowness of query you use for subquery.

Josko
Jul 23 '05 #2
Caveman (IB*******@xemaps.com) writes:
-- first example
-- 1. Get series of values througha query into a string (@val)like
'1,2,3,4':

declare @val varchar(4000)
select @Val = @val + cast(myval as varchar) + ',' -- myval is an
integer variable
from xyz
where xyz.field = 33
This sort of query may work - or may not. The behaviour of it is undefined.
See http://support.microsoft.com/default.aspx?scid=287515.
SET @val = left(@val, len(@val) - 1)

-- 2. EXEC a query using IN (' + @val + ')'

EXEC('
select *
from qpr
where qpr.fieldx IN (' + @val + ')
')

-- is much faster than doing

-- second example
select *
from qpr
where qpr.fieldx IN (select myval
from xyz
where xyz.field = 33)

-- Since second example does not have a correlateed query, why is it
slower?


Do I guess right when I say that there is a non-clustered index on
qpr.fieldx?

In such case, in the first query, the optimzer has exact information
about the values in the query and can from it statistics make an accurate
estimate of how many rows that will be hit. Presumably, the conclusions
is that the index can be used.

In the second example, the optimizer has less information - it only
has the statistics on xyz. Therefore the estimate is less accurate, and
in fear of too many rows being hit, the optimizer selects to scan the
table. Using a non-clusterd index when many rows qualify can be disastrous,
since for each hit in the index, there must be an access to the data
page. (Important exception: the index covers the query. In this case
there is no need to access the data pages at all.)

There is another issue hiding here. If you increase the number in the
list, the EXEC version will take a considerable toll the first time
you run it, because it takes a very long time for the optimizer to
detmermine the plan. As long as the query is exactly the same, future
invocations will be rapid. But change a value in the list, and it will
take a long time again.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #3
Caveman (IB*******@xemaps.com) writes:
-- second example
select *
from qpr
where qpr.fieldx IN (select myval
from xyz
where xyz.field = 33)


Wouldn't this be faster anyway?

select *
from qpr
inner join xyz on qpr.fieldx = xyz.field
where xyz.field = 33

Jul 23 '05 #4
Q
"Ross Presser" <rp******@imtek.com> wrote in message
Wouldn't this be faster anyway?

select *
from qpr
inner join xyz on qpr.fieldx = xyz.field
where xyz.field = 33


and wouldn't this be even faster?

select *
from qpr
inner join xyz on qpr.fieldx = xyz.field and xyz.field = 33

one could ask: physical independence, where are you?
Jul 23 '05 #5
On Wed, 9 Feb 2005 09:08:27 -0500, Ross Presser wrote:
Caveman (IB*******@xemaps.com) writes:
-- second example
select *
from qpr
where qpr.fieldx IN (select myval
from xyz
where xyz.field = 33)


Wouldn't this be faster anyway?

select *
from qpr
inner join xyz on qpr.fieldx = xyz.field
where xyz.field = 33


Hi Ross,

Maybe - but it wouldn't return the same result. More columns, to begin
with. And more rows as well, assuming that column xyz.myval is not unique.

(Oh, and all this of course assuming that you correct the first xyz.field
to xyz.myval - otherwise, you'd get completely different results).

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Jul 23 '05 #6
Q (Q@not.yet) writes:
"Ross Presser" <rp******@imtek.com> wrote in message
Wouldn't this be faster anyway?

select *
from qpr
inner join xyz on qpr.fieldx = xyz.field
where xyz.field = 33


and wouldn't this be even faster?

select *
from qpr
inner join xyz on qpr.fieldx = xyz.field and xyz.field = 33


No. Yours and Ross's queries are exactly the same. The optimizer will
rewrite both to the same internal represenation.

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: grayFalcon | last post by:
Hello! I've got a small problem here. I'm trying to write some code that would generate a drop-down menue for me, where I'd just need to enter the menu- and submenu items into an array. The...
22
by: KsAdmin | last post by:
I have a question which has had me stumped for a few days now. I have a form that I add the values of fields together and display the total in a total field. I have the calculations working...
3
by: Nigel C | last post by:
My problem is best explained by way of tables and examples... I am using Access 2000 (in case this is relevant) and I have a table with the following fields... Field names ------------ Code ...
6
by: Spycat | last post by:
Hi all and happy holidays! I should start off by stating I am NOT a PHP programmer. I say that so that in any response to me, you will speak very s-l-o-w-l-y or I won't know what you're talking...
11
by: Holger | last post by:
Hi I have not been able to figure out how to do compound statement from C - "<test>?<true-val>:<false-val>" But something similar must exist...?! I would like to do the equivalent if python...
3
by: james121285 | last post by:
I have been trying this program for ages and am not getting very far. I am trying to input data from a seperate file and use it to work out the max and min values of the data. I have done the second...
19
by: LucasLondon | last post by:
Hi there, First of all apologies for the long post. Hope someone can offer some advice. I have about 200 columns of time series data that I need to perform a correlation analysis on in terms...
1
by: cybernerdsx2 | last post by:
hi all, I am not sure if this is the right group to post this question but I don't want to cross post this message over to other groups as well. So that's why I choose the closest match to post...
13
by: Salochin | last post by:
Hi Guys.. still trying to do this on my own but need help with below code.. im slowly getting there and loving it (so wish I had discovered this stuff years ago, im actually enjoying my self...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.