問題描述
我有一個 webapp 開發(fā)問題,我已經(jīng)為它開發(fā)了一個解決方案,但我正在嘗試尋找其他想法來解決我所看到的一些性能問題.
I have a webapp development problem that I've developed one solution for, but am trying to find other ideas that might get around some performance issues I'm seeing.
問題說明:
- 用戶輸入多個關鍵字/標記
- 應用程序搜索令牌的匹配項
- 每個令牌需要一個結果
- 即,如果一個條目有 3 個令牌,我需要 3 次條目 id
- 為令牌匹配分配 X 點
- 根據(jù)點對條目 id 進行排序
- 如果點值相同,則使用日期對結果進行排序
我希望能夠做但還沒有弄清楚的是發(fā)送 1 個查詢,該查詢返回類似于 in() 結果的內(nèi)容,但為每個條目 id 的每個令牌匹配返回一個重復的條目 id已檢查.
What I want to be able to do, but have not figured out, is to send 1 query that returns something akin to the results of an in(), but returns a duplicate entry id for each token matches for each entry id checked.
有沒有比我正在做的更好的方法來做到這一點,即使用多個單獨的查詢對每個令牌運行一個查詢?如果是這樣,實現(xiàn)這些的最簡單方法是什么?
Is there a better way to do this than what I'm doing, of using multiple, individual queries running one query per token? If so, what's the easiest way to implement those?
編輯
我已經(jīng)標記了條目,因此,例如,see spot run"的條目 id 為 1,三個標記,see"、spot"、run",它們位于單獨的標記表中,帶有與它們相關的條目 ID,因此表格可能如下所示:edit
I've already tokenized the entries, so, for example, "see spot run" has an entry id of 1, and three tokens, 'see', 'spot', 'run', and those are in a separate token table, with entry ids relevant to them so the table might look like this:'see', 1 'spot', 1 'run', 1 'run', 2 'spot', 3
推薦答案
您可以使用 MySQL 中的UNION ALL"在一個查詢中實現(xiàn)這一點.
you could achive this in one query using 'UNION ALL' in MySQL.
只需遍歷 PHP 中的令牌,為每個令牌創(chuàng)建一個 UNION ALL:
Just loop through the tokens in PHP creating a UNION ALL for each token:
例如,如果標記是x"、y"和z",您的查詢可能看起來像這樣
e.g if the tokens are 'x', 'y' and 'z' your query may look something like this
SELECT * FROM `entries` WHERE token like "%x%" union all SELECT * FROM `entries` WHERE token like "%y%" union all SELECT * FROM `entries` WHERE token like "%z%" ORDER BY score ect...
order 子句應該將整個結果集作為一個操作,這正是您所需要的.
The order clause should operate on the entire result set as one, which is what you need.
就性能而言,它不會那么快(我猜),但是對于數(shù)據(jù)庫而言,速度方面的主要開銷通常是從 PHP 向數(shù)據(jù)庫引擎發(fā)送查詢并接收結果.使用這種技術,這只會發(fā)生一次,而不是每個令牌一次,因此性能會提高,我只是不知道是否足夠.
In terms of performance it won't be all that fast (I'm guessing), however with databases the main overhead in terms of speed is often sending the query to the database engine from PHP and receiving the results. With this technique this only happens once instead of once per token, so performance will increase, I just don't know if it'll be enough.
這篇關于操作方法:對搜索結果進行排名的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權益,請聯(lián)系我們刪除處理,感謝您的支持!