Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Query Optimisation - can this be optimised

Posted on 2011-03-24
5
Medium Priority
?
231 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 600 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 400 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

885 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