mysqlʹ��rand������ѯ��¼�ĸ�Ч�ʷ���
һֱ��Ϊmysql������ѯ�������ݣ�����
SELECT * FROM `table` ORDER BY RAND() LIMIT 5
�Ϳ����ˡ�
������������һ�²ŷ�������Ч�ʷdz��͡�һ��15�������Ŀ⣬��ѯ5�����ݣ���ȻҪ8������
�鿴�ٷ��ֲᣬҲ˵rand()����ORDER BY �Ӿ��лᱻִ�ж��Σ���ȻЧ�ʼ��ܵ͡�
You cannot use a column with RAND() values in an ORDER BY clause, because ORDER BY would evaluate the column multiple times.
����Google�����ϻ����϶��Dz�ѯmax(id) * rand()��������ȡ���ݡ�
SELECT *
FROM `table` AS t1 JOIN (SELECT ROUND(RAND() * (SELECT MAX(id) FROM `table`)) AS id) AS t2
WHERE t1.id >= t2.id
ORDER BY t1.id ASC LIMIT 5;
��������������������5����¼�������취ֻ����ÿ�β�ѯһ������ѯ5�Ρ���������Ҳֵ�ã���Ϊ15�����ı�����ѯֻ��Ҫ0.01�벻����
�������������õ���JOIN��mysql����̳������ʹ��
SELECT *
FROM `table`
WHERE id >= (SELECT FLOOR( MAX(id) * RAND()) FROM `table` )
ORDER BY id LIMIT 1;
�Ҳ�����һ�£���Ҫ0.5�룬�ٶ�Ҳ���������Ǹ����������仹���кܴ����ࡣ�ܾ���ʲô�ط���������
�����Ұ�������д��һ�¡�
SELECT * FROM `table`
WHERE id >= (SELECT floor(RAND() * (SELECT MAX(id) FROM `table`)))
ORDER BY id LIMIT 1;
���£�Ч���������ˣ���ѯʱ��ֻ��0.01��
�������ٰ���������һ�£�����MIN(id)���жϡ������ʼ���Ե�ʱ������Ϊû�м���MIN(id)���жϣ�������һ����ʱ�����Dz�ѯ�����е�ǰ�漸�С�
������ѯ�����ǣ�
SELECT * FROM `table`
WHERE id >= (SELECT floor( RAND() * ((SELECT MAX(id) FROM `table`)-(SELECT MIN(id) FROM `table`)) + (SELECT MIN(id) FROM `table`)))
ORDER BY id LIMIT 1;
SELECT *
FROM `table` AS t1 JOIN (SELECT ROUND(RAND() * ((SELECT MAX(id) FROM `table`)-(SELECT MIN(id) FROM `table`))+(SELECT MIN(id) FROM `table`)) AS id) AS t2
WHERE t1.id >= t2.id
ORDER BY t1.id LIMIT 1;
������php�ж��������������зֱ���ѯ10�Σ�
ǰ����ʱ�� 0.147433 ��
������ʱ�� 0.015130 ��
��������JOIN�����ֱ����WHERE��ʹ�ú���Ч�ʻ�Ҫ�ߺܶࡣ
- ת����ע����Դ��ITѧϰ�� ��ַ��http://www.t086.com/ �����������Ƽ�������
- �ر������� ��վ�������ر�������ֹת�ص�ר�������������¿�������ת�أ���������ע��������ԭʼ���ߡ����°�Ȩ������ԭʼ�������С����ڱ���վת�����µĸ��˺���վ�����DZ�ʾ������л�⡣������վת�ص������а�Ȩ��������ϵ���ǣ����ǻᾡ�����Ը�����
- �������е���ɫ
- �������ж�IP��ַ��A��B���C��
- ������ϵ��Ц�� (ʷ����ȫ��
- ��Google���������ŵ���ͨv4.0
- ���ٶ�ͳ��URL��hm������utm�����Ķ�Ӧ��ϵ
- ��ƻ������MACbook Air���ݼ���ȫ
- ��Linux��ʹ��host��dig��nslookup��ѯDNS
- �����Լ���VPS�Google��������
- ���������ԡ���װ Windows XP ����ϵͳ��ͼ��ȫ����
- ��С�ڴ�VPS֮Nginx+PHP-fpm�߸����Ż���ѹ�����Է���