Solved

Query Optimisation - can this be optimised

Posted on 2011-03-24
5
227 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 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

Technology Partners: 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

Suggested Solutions

Title # Comments Views Activity
Complex SQL Server WHERE CLause 9 40
sql trace 4 29
SQL - Update field value based on two other fields in same table. 13 43
Export individual report to pdf 2 41
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
These days, all we hear about hacktivists took down so and so websites and retrieved thousands of user’s data. One of the techniques to get unauthorized access to database is by performing SQL injection. This article is quite lengthy which gives bas…
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…

710 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