Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Query Optimisation - can this be optimised

Posted on 2011-03-24
5
Medium Priority
?
229 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-
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Recently I was talking with Tim Sharp, one of my colleagues from our Technical Account Manager team about MongoDB’s scalability. While doing some quick training with some of the Percona team, Tim brought something to my attention...
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 …
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…
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…

722 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