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 12»»

Query optimization Expand / Collapse
Author
Message
Posted Wednesday, December 12, 2012 12:35 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Monday, May 20, 2013 10:44 PM
Points: 1,143, Visits: 229
Hello,
I have two tables
#InnerTable which have around 7000 rows
DCl.CompanyToken which have around 20000000 rows

when i am firing below query, it takes around 30 mins to execute and return 787 rows..
please help to optimize it


SELECT
a.SearchTokenNumber ,
a.SearchRowID ,
CT.CompanyID ,
CT.CompanyRowID ,
a.SearchField ,
a.MaxPossibleScore ,
a.AssignedWeight ,
MAX(a.Weight) Weight
FROM
#InnerTable a
INNER JOIN DCl.CompanyToken CT ON a.CompanyTokenNumber = CT.CompanyTokenNumber
WHERE AND CT.Token = a.Token
GROUP BY a.SearchTokenNumber ,
a.SearchRowID ,
CT.CompanyID ,
CT.CompanyRowID ,
a.SearchField ,
a.MaxPossibleScore ,
a.AssignedWeight


Note..
CREATE INDEX IX_LocalIdentifierID ON #InnerTable (CompanyTokenNumber) index is applied on #InnerTable.

/****** Object: Index [ix_CL_DCL_CompanyToken] Script Date: 12/12/2012 13:04:19 ******/
CREATE CLUSTERED INDEX [ix_CL_DCL_CompanyToken] ON [DCL].[CompanyToken]
(
[CompanyTokenNumber] ASC,
[Token] ASC,
[CompanyID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
index is applied on DCl.CompanyToken
Post #1395465
Posted Wednesday, December 12, 2012 12:38 AM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Yesterday @ 2:11 AM
Points: 9,378, Visits: 6,473
Can you post the actual query plan?



How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?

Member of LinkedIn. My blog at LessThanDot.

MCSA SQL Server 2012 - MCSE Business Intelligence
Post #1395467
Posted Wednesday, December 12, 2012 12:49 AM


SSC-Dedicated

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

Group: General Forum Members
Last Login: Today @ 4:30 AM
Points: 37,742, Visits: 30,021
Please post table definitions, index definitions and execution plan, as per http://www.sqlservercentral.com/articles/SQLServerCentral/66909/


Gail Shaw
Microsoft Certified Master: SQL Server 2008, 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 #1395474
Posted Wednesday, December 12, 2012 1:40 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Monday, May 20, 2013 10:44 PM
Points: 1,143, Visits: 229
Please find attachment

Thanks,
Aadhar


  Post Attachments 
Query.zip (11 views, 25.04 KB)
Post #1395492
Posted Wednesday, December 12, 2012 1:49 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Monday, January 28, 2013 1:45 AM
Points: 386, Visits: 199
How many rows does it need to access in the large table - is it most of them or a few.
Are the group by columns independent? Can some be omitted from the group by and a max used in the resultset - i.e. if you omit some any column from the group by clause will you always get less groups?



Cursors never.
DTS - only when needed and never to control.
Post #1395495
Posted Wednesday, December 12, 2012 2:07 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Monday, May 20, 2013 10:44 PM
Points: 1,143, Visits: 229
large table has 20000000 rows and all group by are required
By ommiting single group by will raise me to an error..

Post #1395509
Posted Wednesday, December 12, 2012 2:48 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Monday, January 28, 2013 1:45 AM
Points: 386, Visits: 199
The large table has that many rows but are they all involved in the join to the temp table or does that join filter the rows accessed and if so by how much. If you only require a few thousand rows from the large table it could help to split the query into two.

Removing a column from the group by will give an error unless you aggregate the column in the select clause by using (e.g.) max()

So if you remove a.AssignedWeight from the group by clause and change the entry in the select clause to max(a.AssignedWeight) do you get less entries returned or is a.AssignedWeight dependent on the other values?



Cursors never.
DTS - only when needed and never to control.
Post #1395528
Posted Wednesday, December 12, 2012 3:12 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Monday, May 20, 2013 10:44 PM
Points: 1,143, Visits: 229
This is what u are telling same thing.

If i split query and i take sub data and group it or i take temp table to store partial data and use it to further join, It will take same time.
I tried but couldnt succeed.
Post #1395542
Posted Wednesday, December 12, 2012 3:17 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Monday, January 28, 2013 1:45 AM
Points: 386, Visits: 199
Do you want to answer the questions I asked?



Cursors never.
DTS - only when needed and never to control.
Post #1395547
Posted Wednesday, December 12, 2012 4:09 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Monday, May 20, 2013 10:44 PM
Points: 1,143, Visits: 229
I tried but it produces less rows..
Post #1395574
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse