加快搜索查询速度(大型联结表)

发布于 2024-12-12 00:08:43 字数 1208 浏览 0 评论 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

上述查询的解释给出:(带有索引) https://i.sstatic.net/8Fe8C.gif

提前致谢,对我的不好表示歉意英语。

I'm writing a multistore webshop application in PHP/MYSQL. The products table has about a million records, currently i'm having 5 stores with all products in it and about 3 stores with a few products. The product's price sometimes varies from store to store. Because it should be possible to add or delete certain products for let's say only shop 2, i created a junction table. My tables looks like this:

products(id,name,description,price,media) ~1M records
stores(id,name)
products_stores(id,product_id,store_id,price) ~5M records

When searching for products the query takes about 20 seconds. I added indices to products(name,description) + products_stores(product_id,store_id). Is there any way I could speed up the process? Most of the records in products_stores are the same (except for the store_id), but I'd like to keep it flexible.

The query:

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

I added a FULLTEXT index on only name and deleted the ORDER BY statement, but it seems to make no difference. My indices are now:

Products(name) BTREE
Products(name,description) FULLTEXT
Products(name) FULLTEXT

EXPLAIN of the above query gives: (with indices)
https://i.sstatic.net/8Fe8C.gif

Thanks in advance and sorry for my bad English.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

酒解孤独 2024-12-19 00:08:43

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

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上实现类似全文搜索的任何方法

<一个href="https://stackoverflow.com/questions/5472241/how-to-avoid-using-temporary-in-many-to-many-queries/5473513#5473513">如何避免在许多情况下“使用临时”一对多查询?

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;

希望这有帮助:)

read the following links about clustered primary keys:

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 and NoSQL: Help me to choose the right one

Any way to achieve fulltext-like search on InnoDB

How to avoid "Using temporary" in many-to-many queries?

60 million entries, select entries from a certain month. How to optimize database?

then redesign your product_stores table something along the lines of:

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

hope this helps :)

温折酒 2024-12-19 00:08:43

因为您正在寻找来自特定商店的产品,所以我会在商店 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

Because you are looking for products from a specific store, I would have an index on the store ID AND the Name, then adjust the query to pre-qualify the products FROM the given store instead of looking at all products first.

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文