Go Back   CodingForums.com > :: Server side development > MySQL

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-04-2013, 12:51 PM   PM User | #1
happybanana
New Coder

 
Join Date: Nov 2012
Posts: 13
Thanks: 2
Thanked 0 Times in 0 Posts
happybanana is an unknown quantity at this point
mysql query solution to this problem?

Hi,

I have a database with two tables that I'm using to construct one large HTML table out of. I've coded a solution but it uses a loop inside a loop with some mysql_data_seek hacks and it's getting slow. I'm hoping to speed it up.

One database table is essentially providing a 'parent title' and the other table is providing child sub parts to each title.

For example, table parent:

id text
1.1.2 This is a parent row
1.1.3 This is another parent row

table child:

id text
1.1.2.a This is child text belonging to 1.1.2
1.1.2.b This is child text belonging to 1.1.2
1.1.2.c This is child text belonging to 1.1.2
1.1.3.a This is another child row

Now I *could* just combine everything into one table but that gets awkward for me elsewhere in my program.

The code I created (and the core logic I need to replicate) needs to check that parent.id >= child.id AND parent.id < a mysql pointer I have looking ahead in child.id. This is because the table has to format with parent and child parts formatted correctly.

I hope I've explained that OK. Can anyone offer any suggestions?
happybanana is offline   Reply With Quote
Old 02-04-2013, 03:37 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,741
Thanks: 4
Thanked 2,465 Times in 2,434 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
The problem is you have no relationship enforcement, so no matter what you do you'll end up with complicated queries or magnitude increasing loops. Both of which will slow your code down.
The best thing to do would be to simply add the parent id to the child table since you have a 1 to many relationship. Fortunately that can be done easily by slicing up the existing id, so long as the format is identical for every record a regex can be used to match the x.x.x values out of the existing id. You can then either retain the child's id as x.x.x.Y, or you can use a composite key of the parent/child with x.x.x and Y.
Querying than becomes exceedingly easy:
Code:
SELECT pt.text, c.text
FROM parent_table pt
INNER JOIN child_table c ON c.parent_id = pt.id
ORDER BY pt.id, c.id ASC
Which now becomes a single loop. Formatting is then controlled simply by tracking changes in the parent. Using seeking is very slow and should be avoided in favour of ordering instead.
__________________
PHP Code:
header('HTTP/1.1 420 Enhance Your Calm'); 
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
happybanana (02-19-2013)
Old 02-04-2013, 09:29 PM   PM User | #3
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,556
Thanks: 62
Thanked 4,055 Times in 4,024 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Well, to be fair, you don't really have to change the existing tables, at all.

You will get better performance if you do. That is if you convert your child table to:
Code:
id     subid   text
1.1.2   a      This is child text belonging to 1.1.2
1.1.2   b      This is child text belonging to 1.1.2
1.1.2   c      This is child text belonging to 1.1.2
1.1.3   a      This is another child row
but you *can* do that on the fly, in the query, if you have no control over the existing data.

You could do:
Code:
SELECT pt.text, c.text
FROM parent_table pt, child_table c 
WHERE pt.id = SUBSTR( c.id, 1, LENGTH(pt.id) )
ORDER BY pt.id, c.id
But that SUBSTR function will mean that you won't be able to take advantage of any index on child_table.id and, of course, you pay the penalty of the extra time needed to use SUBSTR.

It's not a good idea. It's just something you'd do if you were desperate.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Users who have thanked Old Pedant for this post:
happybanana (02-19-2013)
Old 02-05-2013, 03:03 AM   PM User | #4
happybanana
New Coder

 
Join Date: Nov 2012
Posts: 13
Thanks: 2
Thanked 0 Times in 0 Posts
happybanana is an unknown quantity at this point
Thanks for the advice gents, that's pointed me in the right direction.
happybanana is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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 On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:39 AM.


Advertisement
Log in to turn off these ads.