MATCH排序的慢SQL查询按相关性排序

时间:2017-11-30 09:28:36

标签: mysql performance match relevance

我有以下查询

SELECT
    product.AID,
    product.ART_ID,
    product.EAN,
    productdetails.DESCRIPTION_SHORT,
    MAX( 
        (100000 * (MATCH(productdetails.DESCRIPTION_SHORT) AGAINST ('"psen in1p"' IN BOOLEAN MODE)))+
        (100000 * (MATCH(product.ART_ID) AGAINST ('"psen in1p"' IN BOOLEAN MODE)))+
        (100000 * (MATCH(product.EAN) AGAINST ('"psen in1p"' IN BOOLEAN MODE)))+
        (100000 * (MATCH(product.SUPPLIER_ALT_PID) AGAINST ('"psen in1p"' IN BOOLEAN MODE)))+
        (10 * (MATCH(productdetails.DESCRIPTION_LONG) AGAINST ('*psen in1p*' IN BOOLEAN MODE)))+
        (2 * (MATCH(productdetails.KEYWORD) AGAINST ('+psen +in1p' IN BOOLEAN MODE)))

    ) AS relevance

FROM 
    tbl_product as product 
INNER JOIN 
    `tbl_product_details` as productdetails ON product.AID = productdetails.AID 
WHERE MATCH 
    (product.ART_ID,
     product.EAN,
     product.SUPPLIER_ALT_PID,
     product.ERP_GROUP_SUPPLIER) AGAINST ('*psen* *in1p*' IN BOOLEAN MODE)
OR MATCH 
    (productdetails.DESCRIPTION_SHORT,
     productdetails.DESCRIPTION_LONG,
     productdetails.MANUFACTURER_TYPE_DESC,
     productdetails.KEYWORD) AGAINST ('*psen* *in1p*' IN BOOLEAN MODE)
GROUP BY 
    product.AID 
ORDER BY
    relevance DESC

我的问题是,查询需要大约3秒才能实现。如果我在没有ORDER BY的情况下运行Statement,则需要大约0,0096秒,这是完美的。我不知道为什么需要这么长时间。我已经尝试使用相同的结果对Subselect进行Subselect和Order(大约需要3秒才能完成。对于没有ORDER BY的Subselect也是如此。

数据库在tbl_product_details中有大约600,000条记录和超过1百万条记录。

我感谢你对这个问题的任何帮助。

按顺序解释查询(3秒)

id  select_type  table           type   possible_keys                                                                                                                                                               key                              key_len                      ref     rows   Extra
1   SIMPLE       product         index  PRIMARY,tbl_product_catalog_id_foreign,tbl_product_supplier_id_foreign,tbl_product_art_id_index,tbl_product_ean_index,SUPPLIER_ALT_PID,ART_ID_2,ft_artid,ft_ean,ft_sapid    PRIMARY                          4  NULL                      569643  Using  temporary; Using filesort
1   SIMPLE       productdetails  ref    tbl_product_details_aid_foreign                                                                                                                                             tbl_product_details_aid_foreign  5  shop_meyle1.product.AID   1       Using  where

解释没有订单的查询(0,01秒)

id  select_type  table           type   possible_keys                                           key                              key_len    ref                      rows    Extra  
1   SIMPLE       product         index  PRIMARY,tbl_product_catalog_id_foreign,tbl_product...   PRIMARY                          4          NULL                     569643  NULL
1   SIMPLE       productdetails  ref    tbl_product_details_aid_foreign                         tbl_product_details_aid_foreign  5          shop_meyle1.product.AID  1       Using where

解释没有Order By和Subselect(3秒)的查询

id  select_type  table           type   possible_keys                                           key                              key_len    ref                      rows    Extra  
1   PRIMARY      <derived2>      ALL    NULL                                                    NULL                             NULL       NULL                     569643  NULL
2   DERIVED      product         index  PRIMARY,tbl_product_catalog_id_foreign,tbl_product...   PRIMARY                          4          NULL                     569643  NULL
2   DERIVED      productdetails  ref    tbl_product_details_aid_foreign                         tbl_product_details_aid_foreign  5          shop_meyle1.product.AID  1       Using where

0 个答案:

没有答案