表:users
與full text search
索引在列username
。按相關度排序的全文搜索排序
查詢:
SELECT
MATCH (username) AGAINST ('shaharyar' IN NATURAL LANGUAGE MODE) AS `score`,
uid,
first_name,
username,
`status`,
created
FROM users
WHERE
MATCH (username) AGAINST ('shaharyar' IN NATURAL LANGUAGE MODE) AND
uid <> 164125 -- to prevent self profile search
ORDER BY
score DESC,
created DESC
問題:請問Match
條款執行2次?
我知道MySQL的排序由score
降序默認,但在這裏我需要這就是爲什麼我選擇列雙排序條件。
解釋擴展:
+----+-------------+-------+----------+------------------+----------+---------+-----+------+----------+-----------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+----------+------------------+----------+---------+-----+------+----------+-----------------------------+
| 1 | SIMPLE | users | fulltext | PRIMARY,username | username | 0 | | 1 | 100 | Using where; Using filesort |
+----+-------------+-------+----------+------------------+----------+---------+-----+------+----------+-----------------------------+
關於150K +記錄在表中。
不幸的是,您的查詢沒有使用索引*(與Explain一起檢查)*。掃描86K +行。我認爲它的原因是條件不在WHERE子句中,而是在子句之後。 – Shaharyar