REMEMBER MYSQL

MINTE9
2REMEMBER




Order by rand

Order by rand is very slow. Order by index (random) is recommended.
 
SELECT * FROM article ORDER BY RAND() LIMIT 1;
... 1 lines
˄˄˄
 
ALTER TABLE article ADD random FLOAT NOT NULL;

ALTER TABLE ADD INDEX random (random);

UPDATE article SET random = RAND();

INSERT INTO article (..., random) VALUES (..., RAND());

SELECT * FROM article WHERE random > 

    (SELECT RAND() as rand) ORDER BY random LIMIT 1;
... 6 lines
˄˄˄

Daily Rand

Get random item by seed (day).
 
SELECT * FROM article ORDER BY rand("20190131") # date(Ymd)    
... 1 lines
˄˄˄

Order by array

Get items from array, then order by field.
 
SELECT * FROM documents 

    WHERE id IN (3, 7, 5) ORDER BY FIELD (id, 3, 7, 5)
... 2 lines
˄˄˄

Questions    
No. 2   Concat fields
CARD
01

        A B C D E F
🔔
1/4