开发者

mysql queries which is the best

开发者 https://www.devze.com 2023-02-14 14:35 出处:网络
i want to know which way is better (faster) from those sql methods; 1st method: public static function getlistusers($limit=88888888888888 ) {

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 开发者_如何学Pythonis 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消