Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Apr 2010
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts

    mysql optimization

    I have a DB table with more then 30,000 rows (users)

    I use this SELECT statment to get name of user by id:

    PHP Code:
    $result $mysqli->query("SELECT name,last_name FROM users WHERE id=$id LIMIT 1");

                    while (
    $row mysqli_fetch_assoc($result)){


                        
    $name=$row['name'];

                        
    $lastname=$row['last_name'];

                    } 
    // while

                    
    mysqli_free_result($result); 
    I tried this also but I didnt saw any diffrent:

    PHP Code:
    if($mysqli_result $mysqli->query("SELECT name,last_name FROM users WHERE id=$id LIMIT 1")){

        
    $result $mysqli_result->fetch_row();

            
    $name=$result[0];

            
    $lastname=$result[1];        
        
        }
        
    mysqli_free_result($mysqli_result); 
    But it take to long.. how can I optimize it to run faster?

  • #2
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    Is the ID column setup as the primary key for the table?
    OracleGuy

  • #3
    New Coder
    Join Date
    Apr 2010
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts
    yes, id is my primary key.

  • #4
    Super Moderator guelphdad's Avatar
    Join Date
    Mar 2006
    Location
    St. Catharines, Ontario Canada
    Posts
    2,634
    Thanks
    4
    Thanked 148 Times in 139 Posts
    how long does the query take to run outside of PHP? figure out if there is a db problem before you find out if there is a PHP problem.

    Looks straightforward and if ID has an index on it you should be talking fractions of a second. 30,000 rows is negligible in size.


  •  

    Posting Permissions

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