我得到了这个查询,但它真的很慢。
我想要这个查询:1 个随机(id,word,meaning)
和 1 个随机不正确任何另一个词的含义。如何通过查询获得更好的性能?
$offset_result = mysql_query("SELECT FLOOR(RAND() * COUNT(*)) AS offset FROM words WHERE APPROVAL=1 AND PERIOD<".$_SESSION['statistics']['d']." OR ( PERIOD=".$_SESSION['statistics']['d']." AND WEEK<=".$_SESSION['statistics']['h'].")");
$offset_row = mysql_fetch_object($offset_result);
$offset = $offset_row->offset;
$words = mysql_query("SELECT ID,WORD,MEANING,
(SELECT MEANING FROM words WHERE ID!=w.ID AND APPROVAL=1 AND WORD!=w.WORD AND NOT FIND_IN_SET(w.MEANING, OTHER_MEANING) AND ID >= (SELECT FLOOR( MAX(ID) * RAND()) FROM words) ORDER BY ID LIMIT 1) AS INCORRECT
FROM words w
LIMIT $offset, 1");
这是表格布局
CREATE TABLE words (
ID int(11) NOT NULL AUTO_INCREMENT,
APPROVAL tinyint(1) NOT NULL DEFAULT '1',
WORD varchar(255) COLLATE utf8_turkish_ci NOT NULL,
MEANING varchar(255) COLLATE utf8_turkish_ci NOT NULL,
OTHER_MEANING varchar(255) COLLATE utf8_turkish_ci NOT NULL,
PERIOD tinyint(1) NOT NULL,
WEEK tinyint(2) NOT NULL,
PRIMARY KEY (ID),
KEY APPROVAL (APPROVAL) )
ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_turkish_ci AUTO_INCREMENT=769 ;