我得到了這個查詢工作,但它真的很慢。MySQL查詢緩慢運行
我想用以下查詢: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 ;
我不確定我完全理解了這個問題,但您可以使用SELECT ... ORDER BY RAND()LIMIT 1'從結果中獲取一個隨機行。 –
那你的數據庫模式是什麼,你設置了哪些索引? –
**您需要向我們展示表和索引定義。**診斷慢查詢需要全表和索引定義,而不僅僅是描述或釋義。也許你的表格定義不好。也許索引沒有正確創建。也許你沒有一個你認爲你做過的那個專欄的索引。沒有看到表和索引定義,我們不能說。如果你知道如何做一個'EXPLAIN'或者得到一個執行計劃,那就把結果也放在問題中。 –