Solved

Query Optimisation - can this be optimised

Posted on 2011-03-24
5
222 Views
Last Modified: 2012-05-11
Hi,

I have this query, and its running a bit inefficiently, can anyone offer a way of improving its performance?

this is the join bit

FROM        category AS c1 INNER JOIN
                     category AS c2 ON c2.categoryId LIKE c1.categoryId + '___' INNER JOIN
                    metadata AS m2 ON c2.permCatId = m2.contentId AND m2.contentTable = 'category' INNER JOIN
                    category AS c3 ON c3.categoryId LIKE c2.categoryId + '___' INNER JOIN
                    metadata AS m3 ON c3.permCatId = m3.contentId AND m3.contentTable = 'category'

Open in new window

0
Comment
Question by:darren-w-
5 Comments
 
LVL 1

Expert Comment

by:edfreels
Comment Utility
Try this:

FROM        category AS c
INNER JOIN metadata AS m ON
      c.permCatId = m.contentId
where c.CategoryID like '%___' and m.contentTable = 'category'
0
 
LVL 4

Assisted Solution

by:RGBDart
RGBDart earned 150 total points
Comment Utility
To recieve best answer you should provide us with
1) scripts for tables "category" and "metadata"
2) full select statement (fields in query outout may affect performance)
3) explanation - what result do you want to get from the query (edfreels tried to guess it, but I'm not sure if he suceeded).

Anyways, you may try to improve performance of your current query by creating following indexes:

create index ix_category_speedup on category (categoryId)
create index ix_category_speedup2 on category (permCatId)
create index ix_metadata_speedup on metadata (contentTable, contentId)

0
 
LVL 8

Assisted Solution

by:EvilKnievel
EvilKnievel earned 100 total points
Comment Utility
Alternatively, you can run the SQL query analyzer to finetune your query
0
 
LVL 13

Accepted Solution

by:
darren-w- earned 0 total points
Comment Utility
Hi,

Found a solution:
FROM         dbo.category AS c1 
                      INNER JOIN category AS c2
					  ON len(c2.categoryId) = len(c1.categoryId)+3 and c2.categoryId LIKE c1.categoryId + '___'
                      INNER JOIN metadata AS m2 
					  ON c2.permCatId = m2.contentId AND m2.contentTable = 'category'
					  INNER JOIN dbo.category AS c3
                      ON len(c3.categoryId) = len(c2.categoryId)+3 AND c3.categoryId LIKE c2.categoryId + '___' 
					  INNER JOIN metadata AS m3
					  ON c3.permCatId = m3.contentId AND m3.contentTable = 'category'

Open in new window


this reduces the query time from 7 to 0 seconds

@edfreels this brought back the wrong data
@ EvilKnievel & RGBDart did look at these options but could not/not possible to  modify indexes on metadata etc, as the query analyzer recommended.


0
 
LVL 13

Author Closing Comment

by:darren-w-
Comment Utility
Worked out  a solution
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This article explains all about SQL Server Piecemeal Restore with examples in step by step manner.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now