Join or not join - that's my newbie question!

Discussion in 'MySQL' started by goldensea80, Feb 12, 2007.

  1. #1
    Sorry if my question are quite noob here. But suppose I have to search the database for the main table named: X, but to show the result, there are lots of tables depend on X like: States, Cities, Districts, Countries, users,...The question is should I JOIN all the tables together so that I use just one query - or I search using minimal query, then for each result row, I use other queries to fetch the details?
    Thanks.
     
    goldensea80, Feb 12, 2007 IP
  2. ccoonen

    ccoonen Well-Known Member

    Messages:
    1,606
    Likes Received:
    71
    Best Answers:
    0
    Trophy Points:
    160
    #2
    Personally, i would left outer join all queries, get your RS and close the DB connection. Then use variables in a huge loop to detect when a state or city etc... has changed. (just my 2 cents though... I always try to keep as much as possible in the query)
     
    ccoonen, Feb 12, 2007 IP
  3. pr0xy122

    pr0xy122 Peon

    Messages:
    1,649
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    0
    #3
    delete the queries, then retry.
     
    pr0xy122, Feb 12, 2007 IP
  4. goldensea80

    goldensea80 Well-Known Member

    Messages:
    422
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    128
    #4
    No, I am planning, no queries yet. I am just consider which is the best way to do? One query with lots of JOIN or several queries.
     
    goldensea80, Feb 12, 2007 IP
  5. designcode

    designcode Well-Known Member

    Messages:
    738
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    118
    #5
    There is no hard and fast rule, Multiple joins will take sometime as taken by multiple queries. In fact with multiple queries, you can fetch data in a more better way (properly structured and linked).
     
    designcode, Feb 13, 2007 IP
  6. MarkusJ_NZ

    MarkusJ_NZ Well-Known Member

    Messages:
    240
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    108
    #6
    Try to do a little joining across tables as possible but sometimes it is impossible not to. It also depends on the number of records in each table.

    If you are using MSSQL there is a handy tool called SQL Query Profiler which will show you how a query is going to run and lets you optimise your queries.

    Cheers
    MarkusJ
     
    MarkusJ_NZ, Feb 22, 2007 IP
  7. spachev

    spachev Peon

    Messages:
    42
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    With MySQL, a lot (more than 5) tables with a bunch of left joins could be slow. If you get to that point it might be a good idea to start splitting the queries.
     
    spachev, Mar 8, 2007 IP