我有一个像这样的SQL查询:
SELECT `users`.`name`, `users`.`username`, `users`.`bio`, `users`.`city`, `users`.`photo`
FROM (`onlines`) JOIN `users`
ON `onlines`.`username`=`users`.`username`
WHERE `users`.`offline_status` = 0
AND `users`.`perma_ban` = 0
AND `users`.`is_premium` = 1
GROUP BY `onlines`.`username`
ORDER BY RAND() LIMIT 27
我正在使用兰德,但我对表现不满意。如何优化此查询?
我读到了这个:
How can i optimize MySQL's ORDER BY RAND() function?
但我正在使用内部联接,所以这个问题对我没有帮助。
答案 0 :(得分:0)
一种方法是添加where
子句。因此,如果您知道大约有多少行符合条件,那么您可以减少数据。所以,如果您知道大约1000个,那么在最终order by
之前随机抽取大约50个:
and rand()*1000 < 50
当然,您需要估算总数。
答案 1 :(得分:0)
首先从If TeamHome_PlayerNumber1.Value = TeamHome_PlayerNumber2.Value Or TeamHome_PlayerNumber1.Value = TeamHome_PlayerNumber3.Value Or TeamHome_PlayerNumber1.Value = TeamHome_PlayerNumber3.Value Or TeamHome_PlayerNumber1.Value = TeamHome_PlayerNumber4.Value Or TeamHome_PlayerNumber1.Value = TeamHome_PlayerNumber5.Value Or TeamHome_PlayerNumber1.Value = TeamHome_PlayerNumber6.Value Or TeamHome_PlayerNumber1.Value = TeamHome_PlayerNumber7.Value Or TeamHome_PlayerNumber1.Value = TeamHome_PlayerNumber8.Value Or TeamHome_PlayerNumber1.Value = TeamHome_PlayerNumber9.Value Or TeamHome_PlayerNumber1.Value = TeamHome_PlayerNumber10.Value Or TeamHome_PlayerNumber1.Value = TeamHome_PlayerNumber11.Value Or TeamHome_PlayerNumber1.Value = TeamHome_PlayerNumber12.Value Or TeamHome_PlayerNumber1.Value = TeamHome_PlayerNumber13.Value Or TeamHome_PlayerNumber1.Value = TeamHome_PlayerNumber14.Value Then
MessageBox.Show("Numer zawodnika 1 się powtarza!", "Powtórzenie!", MessageBoxButtons.OK, MessageBoxIcon.Error)
ElseIf TeamHome_PlayerNumber2.Value = TeamHome_PlayerNumber3.Value Or TeamHome_PlayerNumber2.Value = TeamHome_PlayerNumber3.Value Or TeamHome_PlayerNumber2.Value = TeamHome_PlayerNumber4.Value Or TeamHome_PlayerNumber2.Value = TeamHome_PlayerNumber5.Value Or TeamHome_PlayerNumber2.Value = TeamHome_PlayerNumber6.Value Or TeamHome_PlayerNumber2.Value = TeamHome_PlayerNumber7.Value Or TeamHome_PlayerNumber2.Value = TeamHome_PlayerNumber8.Value Or TeamHome_PlayerNumber2.Value = TeamHome_PlayerNumber9.Value Or TeamHome_PlayerNumber2.Value = TeamHome_PlayerNumber10.Value Or TeamHome_PlayerNumber2.Value = TeamHome_PlayerNumber11.Value Or TeamHome_PlayerNumber2.Value = TeamHome_PlayerNumber12.Value Or TeamHome_PlayerNumber2.Value = TeamHome_PlayerNumber13.Value Or TeamHome_PlayerNumber2.Value = TeamHome_PlayerNumber14.Value Then
MessageBox.Show("Numer zawodnika 2 się powtarza!", "Powtórzenie!", MessageBoxButtons.OK, MessageBoxIcon.Error)
ElseIf TeamHome_PlayerNumber3.Value = TeamHome_PlayerNumber4.Value Or TeamHome_PlayerNumber3.Value = TeamHome_PlayerNumber5.Value Or TeamHome_PlayerNumber3.Value = TeamHome_PlayerNumber6.Value Or TeamHome_PlayerNumber3.Value = TeamHome_PlayerNumber7.Value Or TeamHome_PlayerNumber3.Value = TeamHome_PlayerNumber8.Value Or TeamHome_PlayerNumber3.Value = TeamHome_PlayerNumber9.Value Or TeamHome_PlayerNumber3.Value = TeamHome_PlayerNumber10.Value Or TeamHome_PlayerNumber3.Value = TeamHome_PlayerNumber11.Value Or TeamHome_PlayerNumber3.Value = TeamHome_PlayerNumber12.Value Or TeamHome_PlayerNumber3.Value = TeamHome_PlayerNumber13.Value Or TeamHome_PlayerNumber3.Value = TeamHome_PlayerNumber14.Value Then
MessageBox.Show("Numer zawodnika 3 się powtarza!", "Powtórzenie!", MessageBoxButtons.OK, MessageBoxIcon.Error)
'And like this to TeamHome_PlayerNumber14.Value
End If
随机挑选27个随机usernames
,并给出此限制:
users
然后你快完成了。 WHERE `users`.`offline_status` = 0
AND `users`.`perma_ban` = 0
AND `users`.`is_premium` = 1
几乎没有为查询提供任何内容;它所做的就是消除onlines
中未显示的任何用户名。所以只需添加
onlines
然后转到提到的链接,或转到my blog,它会更好地扩展。它提供了5个有效的答案。但是,如果不回答更多问题,我无法告诉你哪个答案最适合你的情况。