我有以下查詢ORDER BY RAND造成非常高負荷
$samecars = $this->QueryResult("SELECT * FROM carads where STATUS='1' and DEL='0'
and TITLE !='' and IMAGE1 != '' and IMAGE1 != '-1' and PRICE BETWEEN $pricelow
and $pricehigh order by RAND() LIMIT 0,3");
當我刪除RAND()
查詢被執行幾乎是瞬間,如果我添加大約需要10-30秒RAND()
。
餐桌上有大約100萬行。 我需要RAND()才能使用隨機顯示。
更多細節:
QueryResult
看起來像這樣
public function QueryResult($strQuery) {
$this->connector();
$query = mysqli_query($this->link, $strQuery);
$arr = array();
if ($query) {
while ($result = mysqli_fetch_object($query)) {
array_push($arr, $result);
}
}
$this->close();
return $arr;
}
我也試過在SQL添加下面的示例命令
SELECT * FROM carads where STATUS='1' and DEL='0'
and TITLE !='' and IMAGE1 != '' and IMAGE1 != '-1' and PRICE BETWEEN 8000
and 15000 order by RAND() LIMIT 0,3"
和RAND()被高亮紅,當我執行它通過#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '"' at line 3
是的,它會的!..... –
@MitchWheat我該如何克服這一點? – user3140607
在你的sql中不需要最後的引號'「' –