Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««1234»»»

NOT IN query very expensive, 100% CPU Expand / Collapse
Author
Message
Posted Thursday, September 13, 2012 7:46 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Monday, August 24, 2015 9:31 AM
Points: 2,926, Visits: 5,410

...
If you did it in the office...


Looks like he is not working in the "office". He is a TEACHER!
I just can only image that his students love him to the bits, especially when they start to behave as their "teacher" and expose his bright ideas when trying to get a job - they must have huge success.

If some one would tell me on T-SQL interview that "We, SQL developers, do not use IDENTITY as it's just MS SQL Server proprietary feature and it's completely crap candidate for PK", I would just ask one more question (just for having some fun): "What other crap and proprietary features of SQL Server you don't use?" then thanked candidate for attendance and get him out of office.

At the same time, may be his students are trained to be trainers? That is fine...


_____________________________________________
"The only true wisdom is in knowing you know nothing"
"O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!"
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
Post #1358561
Posted Thursday, September 13, 2012 7:52 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Tuesday, September 1, 2015 6:27 AM
Points: 66, Visits: 416
Jeff Moden (9/12/2012)

Have you tried the NOT IN without the DISTINCT?

Also, what's with all the LTRIM/RTRIM stuff? Data should be properly stored without leading spaces and trailing spaces hardly ever matter except when determining the actual number of bytes a strinng may contain.


Yep, same deal with NOT IN without the DISTINCT, 100% CPU for way too long.

Who knows with the LTRIM/RTRIM. This was set up before my time here. We're trying to clean it up though. Trust me, I know it's a mess. :) Take for instance the left(table2.column2,2), if you're going to use the left 2 spaces for stuff all the time, why not create another column and index it, right? I'm not sure what the original developers were thinking. Probably just wanted to get it done and go home/didn't care.

Thanks for the help!
Post #1358569
Posted Thursday, September 13, 2012 8:39 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Yesterday @ 3:04 PM
Points: 22,099, Visits: 34,993
scogeb (9/13/2012)
Jeff Moden (9/12/2012)

Have you tried the NOT IN without the DISTINCT?

Also, what's with all the LTRIM/RTRIM stuff? Data should be properly stored without leading spaces and trailing spaces hardly ever matter except when determining the actual number of bytes a strinng may contain.


Yep, same deal with NOT IN without the DISTINCT, 100% CPU for way too long.

Who knows with the LTRIM/RTRIM. This was set up before my time here. We're trying to clean it up though. Trust me, I know it's a mess. :) Take for instance the left(table2.column2,2), if you're going to use the left 2 spaces for stuff all the time, why not create another column and index it, right? I'm not sure what the original developers were thinking. Probably just wanted to get it done and go home/didn't care.

Thanks for the help!


What would really help us help you is if you would provide the DDL (CREATE TABLE statement) for the tables involved including the indexes currently defined on the tables. Some sample data (as INSERT INTO statements) for the tables, and the actual execution plan for the query (if possible) as a .sqlplan file.

Right now, you are getting some help, but basically just shots in the dark.



Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #1358618
Posted Thursday, September 13, 2012 8:47 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Tuesday, September 1, 2015 6:27 AM
Points: 66, Visits: 416
Well I did some more digging and it looks like the ltrim/rtrim is doing something because I get different results if I take them out. From what I gathered so far table1.column1 has trailing spaces in some cases and it looks like that causes the differences. Although there are quit a few nulls in column1 and column2 in both tables as well. It really is a mess. I question the data validation on this as well as I think the program wasn't written very well and now we have to deal with it.
Post #1358622
Posted Thursday, September 13, 2012 8:52 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Tuesday, September 1, 2015 6:27 AM
Points: 66, Visits: 416
Lynn Pettis (9/13/2012)

What would really help us help you is if you would provide the DDL (CREATE TABLE statement) for the tables involved including the indexes currently defined on the tables. Some sample data (as INSERT INTO statements) for the tables, and the actual execution plan for the query (if possible) as a .sqlplan file.

Right now, you are getting some help, but basically just shots in the dark.


Your shots in the dark have helped greatly! I think I'm good on this one. Thanks a ton!
Post #1358627
Posted Thursday, September 13, 2012 9:31 AM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Today @ 5:31 AM
Points: 42,790, Visits: 39,731
scogeb (9/13/2012)
Jeff Moden (9/12/2012)

Have you tried the NOT IN without the DISTINCT?

Also, what's with all the LTRIM/RTRIM stuff? Data should be properly stored without leading spaces and trailing spaces hardly ever matter except when determining the actual number of bytes a strinng may contain.


Yep, same deal with NOT IN without the DISTINCT, 100% CPU for way too long.


Yeah, it will be. SQL ignores DISTINCT when specified in an IN/NOT IN subquery.



Gail Shaw
Microsoft Certified Master: SQL Server, MVP
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass

Post #1358661
Posted Thursday, September 13, 2012 10:19 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Monday, August 24, 2015 9:31 AM
Points: 2,926, Visits: 5,410
Can you try this:


SELECT column1,'99' [a],left(column2,2) [b], max(column3) [c], 'Yes' [d], 'Test' [e]
,ltrim(rtrim(column1))+ltrim(rtrim(left(column2,2))) as test
INTO #pre1
FROM table1
WHERE column4 = 'Yes'
GROUP BY column1, left(column2,2)

SELECT DISTINCT ltrim(rtrim(column1))+ltrim(rtrim(left(column2,2))) as test
INTO #pre2
FROM table2

SELECT p1.column1, p1.a, p1.b, p1.c, p1.d, p1.e
FROM #pre1 p1
WHERE NOT EXISTS(SELECT 1 FROM #pre2 p2 WHERE p2.test = p1.test)


You may also try creating unique clustered index on #pre2 (test)


_____________________________________________
"The only true wisdom is in knowing you know nothing"
"O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!"
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
Post #1358698
Posted Thursday, September 13, 2012 10:50 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 9:12 AM
Points: 38,373, Visits: 35,295
GilaMonster (9/13/2012)
scogeb (9/13/2012)
Jeff Moden (9/12/2012)

Have you tried the NOT IN without the DISTINCT?

Also, what's with all the LTRIM/RTRIM stuff? Data should be properly stored without leading spaces and trailing spaces hardly ever matter except when determining the actual number of bytes a strinng may contain.


Yep, same deal with NOT IN without the DISTINCT, 100% CPU for way too long.


Yeah, it will be. SQL ignores DISTINCT when specified in an IN/NOT IN subquery.


Thanks, Gail. I learned something new today.

Just to verify, I tried it a couple of different ways. On larger tables, it'll sometimes add a "Sort (Distinct Sort)" to the execution plan if the optimizer thinks it will help but I couldn't get it to add such a sort using DISTINCT on smaller tables.


--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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1358717
Posted Thursday, September 13, 2012 11:26 AM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Today @ 5:31 AM
Points: 42,790, Visits: 39,731
http://sqlinthewild.co.za/index.php/2011/01/18/distincting-an-in-subquery/


Gail Shaw
Microsoft Certified Master: SQL Server, MVP
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass

Post #1358735
Posted Thursday, September 13, 2012 11:57 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Friday, August 28, 2015 7:46 AM
Points: 29, Visits: 1,034
In this case:

distinct and no where clause (full scan?!?!??!?! almost... Very slow!!)

options: NOT EXISTS ... sometimes use: (SELECT TOP 1 ....)

Query analyser and execution plan (performance, index - helps a lot).

Thanks,

Carlos

P.S.: Sorry, my english is very, very BAD !!!

long time just watching ..... I'll try to write ...

NEED TO ASK, PARTICIPATE: NOLOCK, JOINS, MODELING ...

I used Oracle for several years. I'm using SQLSERVER 1 YEAR AGO, I'm enjoying it ..
Post #1358762
« Prev Topic | Next Topic »

Add to briefcase ««1234»»»

Permissions Expand / Collapse