如何通过全文加速我的MySQL查询

问题描述 投票:0回答:1

我有2张桌子。

  1. 特殊订单(“订单”表)-包含420000个数据
  2. special_order_product(产品表),它与order_id链接到special_order表。有1000000数据

数据库存储引擎:InnoDB

我尝试根据左连接查找包含关键字的产品的订单。查询需要10到15秒。另外,我将全文索引用于product_link列的其他索引(special_order.id,special_order_product.order_id)

SPECIAL_ORDER表格:Table structure

特殊订单产品表:Table structure

查询:

SELECT distinct(special_order.id), special_order.admin_id, special_order.user_id, 
  special_order.total_price, special_order.created_at, special_order.updated_at 
FROM special_order LEFT JOIN special_order_product 
  ON special_order.id = special_order_product.order_id 
WHERE MATCH(product_link) AGAINST ('trendyol' IN BOOLEAN MODE) 
order by created_at desc limit 30

结果:设置30行(15.730秒)

查询说明:Explain of the query

在special_order表上的索引:Special order table indexes

在special_order_product表上的索引:enter image description here

mysql optimization full-text-search innodb
1个回答
0
投票
  • DISTINCT不是函数;它将删除SELECT列的集合。
  • 您似乎想找到product_link值,但您说这是选项(LEFT)。也许LEFT是错误的?

建议改写:

SELECT  so.id, so.admin_id, so.user_id, so.total_price,
        so.created_at, so.updated_at
    FROM ( SELECT p.order_id 
             FROM special_order_product AS p
             WHERE  MATCH(p.product_link) AGAINST ('trendyol' IN BOOLEAN MODE)
             order by  p.created_at desc
             limit  30
         ) AS p2
    JOIN  special_order AS so  ON so.id = p2.order_id
    order by  so.created_at desc

注意:

  • 派生表(子查询)专注于获取30个项目,然后停止。
  • 因此,so只有30个探针。
  • 我认为没有必要进行重复数据删除。
  • ORDER BY是重复的(因为),因为子查询在外部查询使用时没有顺序。但是LIMIT需要对数据进行排序。

所需的索引(其他索引在这里没有用):

p: FULLTEXT(product_link)
so: (id)  -- the PRIMARY KEY
© www.soinside.com 2019 - 2024. All rights reserved.