我正在用 PHP/MYSQL 编写一个多商店网店应用程序。产品表有大约一百万条记录,目前我有 5 家商店,里面有所有产品,大约 3 家商店有一些产品。产品的价格有时因商店而异。因为应该可以为商店 2 添加或删除某些产品,所以我创建了一个联结表。我的表如下所示:
products(id,name,description,price,media) ~1M records
stores(id,name)
products_stores(id,product_id,store_id,price) ~5M records
搜索产品时,查询大约需要 20 秒。我在 products(name,description) + products_stores(product_id,store_id) 中添加了索引。有什么办法可以加快这个过程吗?products_stores 中的大多数记录都是相同的(store_id 除外),但我想保持灵活性。
查询:
SELECT
Product.id,
Product.name,
Product.drager,
Product.import,
Product.price,
Product.description,
Product.units
FROM
products AS Product
INNER JOIN
products_stores AS ProductStore ON (
ProductStore.product_id = Product.id
AND
ProductStore.store_id =1
)
WHERE
name LIKE 'bach%'
ORDER BY
Product.description asc
LIMIT
500
我只在 name 上添加了 FULLTEXT 索引并删除了 ORDER BY 语句,但似乎没有什么区别。我的指数现在是:
Products(name) BTREE
Products(name,description) FULLTEXT
Products(name) FULLTEXT
上述查询的解释给出:(带索引) http://i.stack.imgur.com/8Fe8C.gif
在此先感谢并为我的英语不好感到抱歉。