Recursive Programming
#1
Posted 31 August 2007 - 10:34 AM
does anybody know of an alternative to recursive programming for getting an unlimited amount of subs out of a mysql db? recursive programming is the only good way i know of, but it's really not very performant.
for example a simple tree file folder for an asset management system:
sql would be something like:
id, parent_id, name
if I have hundreds of folders in there, it's really unperformant...
Any ideas are welcome.
Sincerely,
Chris
#2
Posted 31 August 2007 - 01:18 PM
If you are worried about load, then you don't need to show them all at the same time, you can show one or two levels, with links to show the lower levels and so on.
Matt
#3
Posted 31 August 2007 - 01:47 PM
Mr. Matt, on Aug 31 2007, 08:18 PM, said:
If you are worried about load, then you don't need to show them all at the same time, you can show one or two levels, with links to show the lower levels and so on.
Matt
Ya it's not the problem to program a recursive function, the only problem is the performance as I wanted to load everything and this can be quite much...
#4
Posted 31 August 2007 - 04:31 PM
Recursion is the only way if you want to retrieve everything. I would recommend, as Matt said, to only show so many levels at once, and link to others, if your reasoning is that you have too much load for it. Usually there is no problem when going a good 20 levels down in recursion without too many problems, but I'm guessing you are planning on having like. way too many folders to be worth it. If you have so many levels, it would make more sense to do the linking method anyways, so you aren't displaying so many folders at once to the user and overwhelming them.
#5
Posted 31 August 2007 - 06:29 PM
#6
Posted 01 September 2007 - 12:36 AM
#7
Posted 01 September 2007 - 11:01 AM
Besides, the whole point of recursion is the meeting of a condition to stop it. With out that condition, you have infinite recursion, and that is more deadly than an infinite loop (which would stop on the same condition most likely).
Can it stand 1000+ folders? I don't think anything in this universe can, but you DEFINATELY don't want to use recursion of your going that deep.
This will give you an idea:
/* NOTE: This was found in the php.net user comments and has been slightly modified from
* it's orginial version. I can no longer find the original comment... */
function rread_dir($folder){
/* $stack = list of folders */
$stack[] = $folder;
while($stack){
/* Pop a folder off of the stack of folders */
$current_dir = array_pop($stack);
/* Slash fix */
$current_dir = substr($current_dir,-1) == '/' ? substr($current_dir,0,-1) : $current_dir;
/* Make sure we opened the directory */
if($dh = opendir($current_dir)){
/* Run through the files in the directory */
while(($file = readdir($dh)) !== false){
if($file != '.' && $file != '..'){
$list[] = $current_dir.'/'.$file;
if(is_dir($file)){
/* Add the directory to the stack, so we can run through it */
$stack[] = $current_file;
}
}
}
}else{
die('rread_dir: Could not open <b>'.($current_dir ? $current_dir : '/').'</b>');
}
}
return $list;
}
Edited by rc69, 01 September 2007 - 11:06 AM.
#8
Posted 02 September 2007 - 03:54 AM
it is the most important point,just a little reminder.
I think memory is the key for it, huge of memory may enforce its success.
Edited by mattcch2007, 02 September 2007 - 03:55 AM.
#9
Posted 02 September 2007 - 07:41 AM
#10
Posted 02 September 2007 - 03:54 PM
I'll say this again, if you want to go 1000+ levels deep, you will NOT find anything that will perform at an acceptable rate. Recursion would only add to the problem. If you can find a way to implement the loop algorithm i provided, that would likely be your best option.
#11
Posted 02 September 2007 - 06:22 PM
in general;
for such deep level recursive method/function,
there may be the 'stack overflow' problem (forget "heap" part at this moment);
so large memory media is required or use some hard disk space for swapping,
then you maybe need a "memory manager" routine for such task;
maybe m$ guys are specialized in creating this kind of routines
just for your reference only.
Edited by mattcch2007, 02 September 2007 - 06:22 PM.
#12
Posted 03 September 2007 - 01:28 AM
Thank you so much for your help! That's why I love p2l
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users
