我的表tbltemp
有大约9万记录。这些列是:id
(主键,自动增量),name
,qty
, price
, status
,mod_date
,created_date
。Mysql查询时间长
我的查询是:
SELECT *
FROM tbltemp
WHERE qty > 3
ORDER By Rand()
LIMIT 50
大约需要7到10秒来执行。我怎样才能优化我的表或查询?
我的表tbltemp
有大约9万记录。这些列是:id
(主键,自动增量),name
,qty
, price
, status
,mod_date
,created_date
。Mysql查询时间长
我的查询是:
SELECT *
FROM tbltemp
WHERE qty > 3
ORDER By Rand()
LIMIT 50
大约需要7到10秒来执行。我怎样才能优化我的表或查询?
这是order by rand()
的替代品。试试这个一次,看看它是否
select @lims := floor(1+rand()*(count(*) - 1)) from tbltemp;
PREPARE mystatement FROM "SELECT * FROM tbltemp WHERE qty > 3 LIMIT ?, 50";
EXECUTE mystatement USING @lims;
在您的查询:
SELECT * FROM tbltemp WHERE qty > 3 ORDER By Rand() LIMIT 50
查询优化请看看:
qty
列。
可能重复[如何优化MySQL的ORDER BY RAND()函数?](http://stackoverflow.com/questions/1244555/how-can-i-optimize-mysqls-order-by-rand功能) – Interrobang
为什么你必须在这里执行'ORDER By Rand()'? – Rahul
query' select * from tbltemp where qty> 3 ORDER By id Limit 50'需要多长时间? – Rahul