If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Query Optimization

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-03-09, 17:20
cbisson cbisson is offline
Registered User
 
Join Date: Feb 2009
Posts: 1
Query Optimization

Hello everyone,

What I'm gonna ask is kinda complicated so I'll try to be as clear as possible.

I'm trying to make large system that works with "connections", so you can have items connected to each other, for example, you can have a bank of sections, and connect them as you wish to make sub-sections et so on.

So you link your sections, you can link them to one or more menus, and connect them to one or many projects. Basically, it let's you configure your data in any pratically any way you wish.

So everything is alright with the system itself, but I've come to a point where a lot of data must be recovered with many items connected with each other.

For example, if I connect a section to another section, I need to get all connections back to the menu to make sure that the menu accepts sub-sections or sub-sub-sections or any lower level section.

The challenge, is that, since anything can be connected basically anywhere, I don't know how many "levels" there are, or in other words, I don't know how much "parent items" my "child item" as, so I cannot know how to formulate my query since I don't know how many items I must get...

I don't know if I'm clear enough because a lot of details are needed to understand.

But basically, is there a way to loop or add conditions inside a query? Would there be another way then making one query to check each level since that could use a lot of queries for one verification.

Any suggestions/comments would be appreciated and don't hesitate to ask questions if you need more details...

Chris
Reply With Quote
  #2 (permalink)  
Old 02-03-09, 23:06
guelphdad guelphdad is offline
Registered User
 
Join Date: Mar 2004
Posts: 440
do a search on tree traversals, specifically I'd recommend searching on Joe Celko
Reply With Quote
  #3 (permalink)  
Old 02-04-09, 05:22
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
Quote:
Originally Posted by cbisson
Any suggestions/comments would be appreciated
This thread explained a lot about different ways of storing hierarchies. Can you have loops in your connections or do they spread out like a tree (or hierarchy)?

You can calculate all the children under a connection by first inserting the original connection(s) into a temp table. Then repeat inserting any children of the connections in your temp table that haven't already been inserted. Keep looping until you don't add any more children. Stop and return the rows in your temp table. Do all this in a stored proc with a repeat loop.

If the connections don't change too often then I'd use a cache table to hold all the children underneath any given connection - this will make getting all the children much much faster but you will need to recalculate the children on a regular basis.

Mike
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On