select * from user where 1=1因为表中根本就没有名称为1的字段,所以该SQL等效于select * from user,这个SQL语句很明显是全表扫描,需要大量的IO操作,数据量越大越慢,建议查询时增加必输项,即where 1=1后面追加一些常用的必选条件,并且将这些必选条件建立适当的索引,效率会大大提高
不一定是where 1=1,这么写当然没什么区别有些程序是需要判断的,比如更新或者删除程序的时候需要这么判断update msg set name='张三' where id=1 从后向前看程序 类似:if(id==1)就执行update
function runtime($mode=0) {
static $t;
if(!$mode) {
$t = microtime();
return;
}
$t1 = microtime();
list($m0,$s0) = split(" ",$t);
list($m1,$s1) = split(" ",$t1);
return sprintf("%.3f ms",($s1+$m1-$s0-$m0)*1000);
}
$sql = 'select * from users';
$sql2 = 'select * from users where 1 = 1';
runtime();
$mysqli->query($sql);
echo '1', runtime(1), '<br />';
runtime();
$mysqli->query($sql2);
echo '1', runtime(1), '<br />';