Solved

Query Optimisation - can this be optimised

Posted on 2011-03-24
5
223 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
ID: 35207463
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
ID: 35207559
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
ID: 35207706
Alternatively, you can run the SQL query analyzer to finetune your query
0
 
LVL 13

Accepted Solution

by:
darren-w- earned 0 total points
ID: 35208450
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-
ID: 35239093
Worked out  a solution
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
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…

911 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

23 Experts available now in Live!

Get 1:1 Help Now