+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: Relational Databases

  1. #1
    Join Date
    Apr 2009
    Location
    Trapped in my own little world.
    Posts
    2,487
    Rep Power
    33

    Relational Databases

    Relationship Databases
    This term is used for achieving database efficiency. For a good explanation of what/why click here. This tutorial will show you some common types of this. I will use one example and keep improving it starting from the beginning. The first thing I will do is give you a table structure, then we will improve it using the 2 most common techniques: “1 to 1”, and “1 to many”.
    Code:
    Current table structure:
    id, username, email, password, street_addy, city, state, zip, phone, browser, group_1, group_2, group_3, is_admin, verified
    1 to 1 Relationship
    The first thing you notice is that this table has a lot of columns, because we will not use them every time we search for a user, for instance validating login does not require ‘city’ or displaying a post may not necessarily require ‘phone’, etc. So we would use our 1 to 1 relationship to help simplify this process. The new table should contain it’s own unique id and the id of the user it corresponds with “uid” in this case.
    Current table structure:
    Code:
    Users:
    id, username, email, password, verified, is_admin
    Code:
    Extra:
    id, uid, street_addy, city, state, zip, phone, browser, group_1, group_2, group_3
    When needing all of these this can still appear as the first one using a query like this:
    Code:
    SELECT * FROM `Users` LEFT JOIN `Extra` On Users.id=Extra.uid


    1 to Many Relationship
    The next problem we might notice is that we have a fixed number of groups, which might be what we want, but let's say this application we wanted as many as the user wanted, we don't wanna append a new column everytime the user wants a new one, right? How do we fix this? We might make a new table called "groups" that would have the same unique id, the user's id that points to the user and the group id, so 3 fields.
    Code:
    Users:
    id, username, email, password, verified, is_admin
    Code:
    Extra:
    id, uid, street_addy, city, state, zip, phone, browser
    Code:
    Groups:
    id, uid, group_id
    If you don't know much about the user this might be a bit more of a complicated query, depending on what you already know. If you know the user id# then try this:
    Code:
    SELECT * FROM `Groups` WHERE `uid`=2
    If you don't know the user id# if you just know the username or something you could try something like this:
    Code:
    SELECT * FROM `Groups` WHERE `uid`=(SELECT `id` FROM `Users` WHERE `username`='BlaineSch')


    There are lots of other relationships but that is all for this tutorial!

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Jordan Guest

    Re: Relational Databases

    Well written, +rep!

  4. #3
    Join Date
    Apr 2009
    Location
    Trapped in my own little world.
    Posts
    2,487
    Rep Power
    33

    Re: Relational Databases

    Thank you Jordan! Any constructive criticism?

  5. #4
    mnmtanish is offline Newbie
    Join Date
    Sep 2009
    Posts
    23
    Rep Power
    0

    Re: Relational Databases

    I'm really a newbie on mysql stuff but I have a question about this line
    (It has nothing to do with Relational Databases. It's just a mySQL optimization question.)
    Code:
    SELECT * FROM `Groups` WHERE `uid`=(SELECT `id` FROM `Users` WHERE `username`='BlaineSch')
    I have been advised to use 2 separate queries than using nested stuff.
    First Get the uid and store it temporarily. Then Get all other data using the uid. Like for example
    Code:
    First:
    SELECT `id` FROM `Users` WHERE `username`='BlaineSch'
    And then:
    SELECT * FROM `Groups` WHERE `uid`='$temporary_uid_variable'
    I heard that it will run SELECT `id` FROM `Users` WHERE `username`='BlaineSch' for each record on the Groups table.

    Is it real or is MySQL smart enough to figure it out OR is requesting 2 queries worse than using nested queries ?

  6. #5
    Join Date
    Apr 2009
    Location
    Trapped in my own little world.
    Posts
    2,487
    Rep Power
    33

    Re: Relational Databases

    I really don't think mysql would make it to do the query for each row that seems a bit insane, if it did however I would suggest doing the two queries. But I personally have never tested the two for speed.

    Why not run a few tests and post up your results?

  7. #6
    mnmtanish is offline Newbie
    Join Date
    Sep 2009
    Posts
    23
    Rep Power
    0

    Re: Relational Databases

    I'll need some what you call BIG database to check it.
    I'll first write a php code to generate some dummy data. I'll post the result soon.

    (I don't like to head this thread off-topic)

  8. #7
    mnmtanish is offline Newbie
    Join Date
    Sep 2009
    Posts
    23
    Rep Power
    0

    Re: Relational Databases

    I did the test with 200 rows of data I generated at generatedata.com And here's the result:
    Code:
    * No-index
    Sub    :    0.0012
    Double :    0.0002 + 0.0008 = 0.0010
    * Indexed
    Sub    :    0.0003 OR 0.0004
    Double :    0.0002 + 0.0002 = 0.0004
    I think I'll have to try this with more Dummy data.
    Last edited by mnmtanish; 12-27-2009 at 06:54 AM. Reason: just tweakin

  9. #8
    Join Date
    Apr 2009
    Location
    Trapped in my own little world.
    Posts
    2,487
    Rep Power
    33

    Re: Relational Databases

    Yea try for like 10k or 100k rows.

  10. #9
    mnmtanish is offline Newbie
    Join Date
    Sep 2009
    Posts
    23
    Rep Power
    0

    Re: Relational Databases

    I did the test again with 10k dummy rows and here's the results
    Code:
    Indexed
    Sub    :    0.1970
    Double :    0.0151 + 0.0089 = 0.0240
    I did the sub query version first. And I don't know if these results can be accepted as solid data as MySQL loves to cache stuff.

  11. #10
    Join Date
    Apr 2009
    Location
    Trapped in my own little world.
    Posts
    2,487
    Rep Power
    33

    Re: Relational Databases

    If you think mysql is caching itself why not do the test multiple times in various orders and post the averages?

+ Reply to Thread
Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 5
    Last Post: 04-04-2011, 07:34 AM
  2. Advice on importing spreadsheet data into a relational DBMS
    By Kaishain in forum Database & Database Programming
    Replies: 3
    Last Post: 11-12-2010, 07:09 PM
  3. Replies: 1
    Last Post: 11-02-2009, 05:38 AM
  4. Relational Database to HTML Table
    By millsy007 in forum PHP Development
    Replies: 1
    Last Post: 02-09-2009, 05:20 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts