0

我正在用 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

在此先感谢并为我的英语不好感到抱歉。

4

2 回答 2

0

因为您正在寻找来自特定商店的产品,所以我会在商店 ID 和名称上创建一个索引,然后调整查询以对来自给定商店的产品进行资格预审,而不是先查看所有产品。

SELECT STRAIGHT_JOIN
      P.id, 
      P.name, 
      P.drager, 
      P.import, 
      P.price, 
      P.description, 
      P.units 
   FROM 
      Product_Stores PS
         JOIN Products P
            on PS.Product_ID = P.ID
           AND P.Name like 'back%'
   WHERE
      PS.Store_ID = 1
   order by
      P.Description
   limit
      500
于 2011-10-25T14:32:50.573 回答
0

阅读以下有关集群主键的链接:

http://dev.mysql.com/doc/refman/5.0/en/innodb-index-types.html

http://www.xaprb.com/blog/2006/07/04/how-to-exploit-mysql-index-optimizations/

MySQL 和 NoSQL:帮我选一个合适的

在 InnoDB 上实现类似全文搜索的任何方式

如何避免在多对多查询中“使用临时”?

6000万条条目,选择某月的条目。如何优化数据库?

然后按照以下方式重新设计您的 product_stores 表:

create table products_stores
(
store_id int unsigned not null,
product_id int unsigned not null,
...
primary key (store_id, product_id)
)
engine=innodb;

希望这可以帮助 :)

于 2011-10-25T12:53:38.557 回答