MS SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsDatabasesMS SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old July 26th, 2012, 07:53 AM
scheffetz scheffetz is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2012
Posts: 2 scheffetz User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 22 m 51 sec
Reputation Power: 0
'SELECT TOP...' killing query performance?

This query takes about 7 seconds to execute

Select * into testtable from (
select 'unmapped local product' as 'output', a.dashboardDescription as dashboardDescription
,a.[Product Global Category Name] as Product_Global_Category_Name
,a.[Product Local Id] as Product_Local_Id
,a.[Product Local Name] as product_local_name
,SUM(DASHBOARD_PREMIUM_USD) as premium
from pm_Policies_Live a
left join build_premiumShareSubProductMapping_temp b
on a.dashboardDescription = b.dashboardDescription
and a.[Product Local Id] = b.[Product Local Id]
and a.[Product Global Category Name] = b.[Product Global Category Name]
where b.dashboardDescription is null
and dashboard_exclusion <> 1 and DASHBOARD_FLAG =1
group by
a.dashboardDescription
,a.[Product Global Category Name]
,a.[Product Local Id]
,a.[Product Local Name]
)a



Now if i change the first line to

Select top 10 * into testtable from (

the query took over 10 mins to complete....

Why would using the 'top 10' affect the performance so much?
I am trying to limit the number of results I get back. With some queries, using 'select top' works fine, but in others it severely affects performance.

Reply With Quote
  #2  
Old July 26th, 2012, 08:03 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 26,373 r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 1 Week 2 Days 6 h 54 m 49 sec
Reputation Power: 4140
i don't understand why you need the outer query...
Code:
select top 100
       'unmapped local product' as 'output'
     , a.dashboardDescription as dashboardDescription
     , a.[Product Global Category Name] as Product_Global_Category_Name
     , a.[Product Local Id] as Product_Local_Id
     , a.[Product Local Name] as product_local_name
     , SUM(DASHBOARD_PREMIUM_USD) as premium
  into testtable     
  from pm_Policies_Live a
left 
  join build_premiumShareSubProductMapping_temp b
    on a.dashboardDescription = b.dashboardDescription
   and a.[Product Local Id] = b.[Product Local Id]
   and a.[Product Global Category Name] = b.[Product Global Category Name]
 where b.dashboardDescription is null
   and dashboard_exclusion <> 1 
   and DASHBOARD_FLAG =1
group 
    by a.dashboardDescription
     , a.[Product Global Category Name] 
     , a.[Product Local Id]
     , a.[Product Local Name]


btw, which table do DASHBOARD_PREMIUM_USD, dashboard_exclusion, and DASHBOARD_FLAG belong to?
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book

Reply With Quote
  #3  
Old July 26th, 2012, 08:23 AM
scheffetz scheffetz is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2012
Posts: 2 scheffetz User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 22 m 51 sec
Reputation Power: 0
Quote:
Originally Posted by r937
i don't understand why you need the outer query...
Code:
select top 100
       'unmapped local product' as 'output'
     , a.dashboardDescription as dashboardDescription
     , a.[Product Global Category Name] as Product_Global_Category_Name
     , a.[Product Local Id] as Product_Local_Id
     , a.[Product Local Name] as product_local_name
     , SUM(DASHBOARD_PREMIUM_USD) as premium
  into testtable     
  from pm_Policies_Live a
left 
  join build_premiumShareSubProductMapping_temp b
    on a.dashboardDescription = b.dashboardDescription
   and a.[Product Local Id] = b.[Product Local Id]
   and a.[Product Global Category Name] = b.[Product Global Category Name]
 where b.dashboardDescription is null
   and dashboard_exclusion <> 1 
   and DASHBOARD_FLAG =1
group 
    by a.dashboardDescription
     , a.[Product Global Category Name] 
     , a.[Product Local Id]
     , a.[Product Local Name]


btw, which table do DASHBOARD_PREMIUM_USD, dashboard_exclusion, and DASHBOARD_FLAG belong to?


good catch, i missed that. they belong to table pm_Policies_Live.

i fixed that but it did not help with the performance issue. those columns were unique to pm_Policies_Live so it never caused an issue.

its complicated to explain, but i do need the outer query....

I gave 1 specific query as an example of where i am running into problems. in my stored procedure it would actually look like:

set @query2 = 'select * into tablename from (' + @query + ')a'

exex sp_executesql query2

Reply With Quote
  #4  
Old July 26th, 2012, 08:59 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 26,373 r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 1 Week 2 Days 6 h 54 m 49 sec
Reputation Power: 4140
you'll want to examine the execution plan for both queries, with TOP and without

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > 'SELECT TOP...' killing query performance?

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap