php query optimizing, a small issue

Discussion in 'PHP' started by hip_hop_x, Dec 30, 2007.

  1. #1
    What's more optimized, from the both examples bellow?

    Example 1:
    $query=mysql_query("SELECT * FROM `DB` WHERE id>20 LIMIT 10;");
    $query=mysql_query("SELECT * FROM `table2` WHERE id>20 LIMIT 10;");
    $query=mysql_query("SELECT * FROM `db_table WHERE id>0 LIMIT 30;");
    PHP:
    Example 2:
    $query=mysql_query("SELECT * FROM `DB` WHERE id>20 LIMIT 10; SELECT * FROM `table2` WHERE id>20 LIMIT 10; SELECT * FROM `db_table WHERE id>0 LIMIT 30;");
    PHP:

     
    hip_hop_x, Dec 30, 2007 IP
  2. piniyini

    piniyini Well-Known Member

    Messages:
    514
    Likes Received:
    26
    Best Answers:
    0
    Trophy Points:
    170
    #2
    I've never ran a query as shown in example 2, is that even possible?
     
    piniyini, Dec 30, 2007 IP
  3. hip_hop_x

    hip_hop_x Active Member

    Messages:
    522
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    90
    #3
    I never did that from example 2, but I guess cause it works, since I saw in phpmyadmin, that it can execute multiple sql commands by adding ; after each query.
     
    hip_hop_x, Dec 30, 2007 IP
  4. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #4
    Optimization wise it makes no difference.

    However, following standard coding guidelines, the first is more user-friendlier and easier to operate with.

    If you want to optimize, instead of selecting *, just select the fields you are only going to use. Also use mysql_fetch_assoc,mysql_fetch_row instead of mysql_fetch_array.

    Peace,
     
    Barti1987, Dec 30, 2007 IP
  5. hip_hop_x

    hip_hop_x Active Member

    Messages:
    522
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    90
    #5
    in case if I need all the db details, I'll use *, but in those cases, is any difference? Who's faster, the example 1, or example 2. Or same speed?
     
    hip_hop_x, Dec 30, 2007 IP
  6. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #6
    Barti1987, Dec 30, 2007 IP
  7. Dagon

    Dagon Active Member

    Messages:
    122
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    60
    #7
    are you sure example 2 actually works?

    I thought it was not possible because of the sql injection risks associated.
     
    Dagon, Dec 30, 2007 IP
  8. temp2

    temp2 Well-Known Member

    Messages:
    1,231
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    150
    Digital Goods:
    2
    #8
    I don't think #2 works. Because return result will be #Resource; #2 will return 3 Resource; when how will(should) you do to get arrays from these #Resource?
     
    temp2, Dec 30, 2007 IP
  9. hip_hop_x

    hip_hop_x Active Member

    Messages:
    522
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    90
    #9
    never tried, and I'm not sure that might work. This topic can be closed.
     
    hip_hop_x, Dec 30, 2007 IP
  10. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #10
    How many results do you get from the second method, as opposed to the first ?
     
    joebert, Dec 30, 2007 IP