简体   繁体   中英

mysql queries which is the best

i want to know which way is better (faster) from those sql methods;

1st method:

public static function getlistusers($limit=88888888888888 ) {
$sql = "SELECT id,name FROM users order by id desc limit 0,$limit";
$st = mysql_query( $sql ) or die(mysql_error());
$list = array();
while ( $row = mysql_fetch_assoc($st) ) {
$picss = new users( $row );
$list[] = $picss;
}   
return ( array ( "users" => $list) );
@mysql_free_result($st);

for print output i use

foreach() the array $users ;


2nd method

$sql = "SELECT id,name FROM users order by id desc limit 0,$limit";
$st = mysql_query( $sql ) or die(mysql_error());
while ( $row = mysql_fetch_assoc($st) ) {
extract($row);
return "Id: $id and The Name is : $name";
}   
@mysql_free_result($st);
}

===========

i want to know which is faster and safty for sql load.

Regards Al3in

$sql = "SELECT id,name FROM users order by id desc limit 0,$limit";
$st = mysql_query( $sql ) or die(mysql_error());
while ( $row = mysql_fetch_assoc($st) ) {
    extract($row);
    return "Id: $id and The Name is : $name";
}   
@mysql_free_result($st);

I doubt this approach will even work. Because, even though you limit it to 1 or a million, the loop will only run once because of return "Id: $id and The Name is : $name"; . So if you're comparing this and the other method, the other method would obviously work better.

Unless you're assigning to an array instead of returning. In which case the second method has an unnecessary function call extract which puts two variables into the heap.

Both are essentially the same. They execute the same query and retreive the results in the same way. The advantage of the first method is that it returns a list of data arrays that each represent a records in the database. All individual can be used any way you want. The second approach returns only a single string. The entire while loop is useless there.

So the second may be faster, because it only retrieves a single row, but from here that looks more like an error than like an actual implementation decision.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM