使用此查詢來獲取與履行所有三項必需的詞項(聯想,筆記本電腦,計算機)字樣的產品優化查詢:如何使用兩個內部加入
SELECT t1.id, t1.name, t1.price FROM
(SELECT p.id AS productid, name, price
FROM products p JOIN productwords pw ON p.id = pw.productid
JOIN words w ON pw.wordid = w.id WHERE word.term = 'lenovo') t1
INNER JOIN
(SELECT p.id AS productid, name, price
FROM products p JOIN productwords pw ON p.id = pw.productid
JOIN words w ON pw.wordid = w.id WHERE word.term = 'laptop') t2
INNER JOIN
(SELECT p.id AS productid, name, price
FROM products p JOIN productwords pw ON p.id = pw.productid
JOIN words w ON pw.wordid = w.id WHERE word.term = 'computer') t3
ON
t1.productid = t2.productid
AND
t1.productid = t3.productid
ORDER BY t1.name
據我所看到的,查詢考慮每個術語的整個單詞表(這些表具有索引,數據庫是MySql)。
查詢是否可以以更好的方式重寫,所以它會變得更快? (這些表格包含數百萬行)
例如,對於子集,因此'筆記本電腦'搜索僅考慮與'lenovo'匹配的行 - 而'計算機'搜索僅考慮匹配第一個'lenovo'的行,然後'筆記本電腦'。
謝謝!
見http://meta.stackoverflow.com/questions/333952/why-should-i-provide-an-mcve-for什麼似乎對我來說是一個非常簡單的sql查詢 – Strawberry