Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Werid Behaviour of TSQL


Werid Behaviour of TSQL

Author
Message
psingla
psingla
Right there with Babe
Right there with Babe (728 reputation)Right there with Babe (728 reputation)Right there with Babe (728 reputation)Right there with Babe (728 reputation)Right there with Babe (728 reputation)Right there with Babe (728 reputation)Right there with Babe (728 reputation)Right there with Babe (728 reputation)

Group: General Forum Members
Points: 728 Visits: 1249
I have complex view and try to fetch data from it.
In following code query 2 takes 1 sec to execute and query 1 keeps on running for hours.
I tried to reduce the time of query 1 to same as query 2 but no luck.I tried force order,hash join,creating clustered on temp table,type conversion etc.

This is one of the most complicated behavior I have seen in SQL server.any idea why it is happening so

SET statistics io ON
SET TRANSACTION isolation level READ uncommitted

DROP TABLE tempdb..#ps_temp

CREATE TABLE #ps_temp
(
a numeric(13,0),
b numeric(3,0)
)

DECLARE @a numeric(13,0),
@b numeric(3,0)

---fetches only 1 record
INSERT INTO #ps_temp
(a,
b)
SELECT a,
b
FROM tbl_x
WHERE d = 'L140502021514'


SELECT @a = a,
@b = b
FROM #ps_temp


-----------------query 1---------------------------
SELECT t.a,
t.b
FROM #ps_temp d
inner JOIN dbo.vw_complex AS t -- (hash)
ON t.a = d.a
AND t.b = d.b

-- option (force order)
-----------------query 2---------------------------
SELECT t.a,
t.b
FROM #ps_temp d
inner JOIN dbo.vw_complex AS t -- (hash)
ON t.a = @a
AND t.b = d.b

Pramod
SQL Server DBA | MCSE SQL Server 2012/2014

in.linkedin.com/in/pramodsingla/
http://pramodsingla.wordpress.com/
Eirikur Eiriksson
Eirikur Eiriksson
SSCertifiable
SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)

Group: General Forum Members
Points: 6743 Visits: 17721
First a quick question, why the dirty read (READ UNCOMMITTED)?

Without more information such as execution plans, view code, schema structure etc., it is hard to tell what the problem is.
Cool
ChrisM@Work
ChrisM@Work
SSCrazy Eights
SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)

Group: General Forum Members
Points: 8975 Visits: 19028
psingla (5/6/2014)
I have complex view and try to fetch data from it.
In following code query 2 takes 1 sec to execute and query 1 keeps on running for hours.
I tried to reduce the time of query 1 to same as query 2 but no luck.I tried force order,hash join,creating clustered on temp table,type conversion etc....


Check the execution plan of query1 for timeout. You could post the actual execution plans for both queries if you are interested in more than guesses.

“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45127 Visits: 39923
psingla (5/6/2014)
I have complex view and try to fetch data from it.
In following code query 2 takes 1 sec to execute and query 1 keeps on running for hours.
I tried to reduce the time of query 1 to same as query 2 but no luck.I tried force order,hash join,creating clustered on temp table,type conversion etc.

This is one of the most complicated behavior I have seen in SQL server.any idea why it is happening so

SET statistics io ON
SET TRANSACTION isolation level READ uncommitted

DROP TABLE tempdb..#ps_temp

CREATE TABLE #ps_temp
(
a numeric(13,0),
b numeric(3,0)
)

DECLARE @a numeric(13,0),
@b numeric(3,0)

---fetches only 1 record
INSERT INTO #ps_temp
(a,
b)
SELECT a,
b
FROM tbl_x
WHERE d = 'L140502021514'


SELECT @a = a,
@b = b
FROM #ps_temp


-----------------query 1---------------------------
SELECT t.a,
t.b
FROM #ps_temp d
inner JOIN dbo.vw_complex AS t -- (hash)
ON t.a = d.a
AND t.b = d.b

-- option (force order)
-----------------query 2---------------------------
SELECT t.a,
t.b
FROM #ps_temp d
inner JOIN dbo.vw_complex AS t -- (hash)
ON t.a = @a
AND t.b = d.b


Without seeing the tables or the code for the view and the fact that the setup code refers to a "tbl_x" table that doesn't seem to be available, I'd have to say that the relationship of t.a = d.a produces a many-to-many join rivaliing a Cartesian Product. Look at your execution plan for counts on arrows that are much bigger than the number of rows expected from each table.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search