我有2张桌子。
数据库存储引擎: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
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
注意:
so
只有30个探针。ORDER BY
是重复的(因为),因为子查询在外部查询使用时没有顺序。但是LIMIT
需要对数据进行排序。所需的索引(其他索引在这里没有用):
p: FULLTEXT(product_link)
so: (id) -- the PRIMARY KEY