使用FULL TEXT索引优化MySQL查询

时间:2018-04-03 06:23:48

标签: mysql sql query-optimization

如果关键字不止一个,我的查询会花费太多时间(例如:"相框")。该表位于InnoDB引擎中。我在表中有以下索引(索引名称类型列):

  1. fldForSearch FULLTEXT fldForSearch
  2. fldProductsOffered FULLTEXT fldProductsOffered
  3. fldCompanyProfile FULLTEXT fldCompanyProfile
  4. profile_product FULLTEXT fldCompanyProfile,fldProductsOffered
  5. product_brand FULLTEXT fldProductsOffered,fldCompanyBrand
  6. profile_products_brand FULLTEXT fldCompanyProfile,fldProductsOffered,fldCompanyBrand
  7. fldCompanyBrand FULLTEXT fldCompanyBrand
  8. fldForProductSearch FULLTEXT fldForProductSearch
  9. cp_cb_po_ps FULLTEXT fldCompanyProfile
  10. fs_cb FULLTEXT fldForSearch,fldCompanyBrand
  11. fs_cb_ps FULLTEXT fldForSearch,fldCompanyBrand,fldForProductSearch
  12. 以下是查询:

    SELECT count( S.fldProfileId ) total, group_concat( DISTINCT (
    U.fldCountry
    )
    SEPARATOR ', ' ) AS allCountryIds,
    MATCH (
    S.fldForSearch, S.fldCompanyBrand
    )
    AGAINST (
    '"photo frames"'
    ) AS rank1, (
    
    MATCH (
    S.fldForSearch, S.fldCompanyBrand
    )
    AGAINST (
    'photo photos'
    )
    AND MATCH (
    S.fldForSearch, S.fldCompanyBrand
    )
    AGAINST (
    'frame frames'
    )
    ) AS rank2,
    MATCH (
    S.fldForProductSearch
    )
    AGAINST (
    '"photo frames"'
    ) AS rank3, (
    
    MATCH (
    S.fldForProductSearch
    )
    AGAINST (
    'photo photos'
    )
    AND MATCH (
    S.fldForProductSearch
    )
    AGAINST (
    'frame frames'
    )
    ) AS rank4
    FROM tblUserMaster U, tblSupplierProfileMaster AS S
    WHERE S.fldHideSource = 'N'
    AND S.fldUserId = U.fldUserId
    AND U.fldUserStatus = 'Y'
    AND S.fldProfileStatus = 'A'
    AND MATCH (
    S.fldForSearch, S.fldCompanyBrand, S.fldForProductSearch
    )
    AGAINST (
    ' photo photos frame frames'
    )
    

    我有FULLTEXT搜索中存在的所有列的索引组合。 查询第一次花费时间,但下次快速运行。

    我重新创建了所有索引但没有效果。即使Optimize Table没有帮助。

    更新:上述查询的结果是:

    +-------+--------------------+--------------------+-------+-------------+-------+
    | total | allCountryIds      | rank1              | rank2 | rank3       | rank4 |
    +-------+--------------------+--------------------+-------+-------------+-------+
    |  3390 | 222, 104, 223, ... | 14.645795822143555 |     1 | 60246.34375 |     1 |
    +-------+--------------------+--------------------+-------+-------------+-------+
    

    表格结构:

    CREATE TABLE `tblSupplierProfileMaster` (
     `fldProfileId` int(11) NOT NULL AUTO_INCREMENT,
     `fldUserId` int(11) NOT NULL DEFAULT '0',
     `fldProductsOffered` text NOT NULL,
     `fldCompanyProfile` text NOT NULL,
     `fldCompanyBrand` text NOT NULL,
     `fldForSearch` text NOT NULL,
     `fldForProductSearch` longtext NOT NULL,
     `fldDateOfCreation` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
     `fldLastModifyDate` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
     `fldProfileStatus` enum('A','P','D') NOT NULL DEFAULT 'A',
     PRIMARY KEY (`fldProfileId`),
     UNIQUE KEY `fldUserId` (`fldUserId`),
     UNIQUE KEY `fldProfileStatus` (`fldProfileStatus`,`fldHideSource`,`fldUserId`),
     FULLTEXT KEY `fldForSearch` (`fldForSearch`),
     FULLTEXT KEY `fldProductsOffered` (`fldProductsOffered`),
     FULLTEXT KEY `fldCompanyProfile` (`fldCompanyProfile`),
     FULLTEXT KEY `profile_product` (`fldCompanyProfile`,`fldProductsOffered`),
     FULLTEXT KEY `product_brand` (`fldProductsOffered`,`fldCompanyBrand`),
     FULLTEXT KEY `profile_products_brand` (`fldCompanyProfile`,`fldProductsOffered`,`fldCompanyBrand`),
     FULLTEXT KEY `fldCompanyBrand` (`fldCompanyBrand`),
     FULLTEXT KEY `fldForProductSearch` (`fldForProductSearch`),
     FULLTEXT KEY `cp_cb_po_ps` (`fldCompanyProfile`,`fldCompanyBrand`,`fldProductsOffered`,`fldForProductSearch`),
     FULLTEXT KEY `fs_cb` (`fldForSearch`,`fldCompanyBrand`),
     FULLTEXT KEY `fs_cb_ps` (`fldForSearch`,`fldCompanyBrand`,`fldForProductSearch`)
    ) ENGINE=InnoDB AUTO_INCREMENT=304194 DEFAULT CHARSET=latin1 COMMENT='store the profile of suppliers'
    

    更新2:解释查询结果:

    +------+-------------+-------+----------+------------------------------------------+----------+---------+-----------------------+------+-------------+
    | id   | select_type | table | type     | possible_keys                            | key      | key_len | ref                   | rows | Extra       |
    +------+-------------+-------+----------+------------------------------------------+----------+---------+-----------------------+------+-------------+
    |    1 | SIMPLE      | S     | fulltext | fldUserId,fldProfileStatus,DEV1,fs_cb_ps | fs_cb_ps | 0       |                       |    1 | Using where |
    |    1 | SIMPLE      | U     | eq_ref   | PRIMARY,fldMemberId,fldUserStatus        | PRIMARY  | 4       | wdeals_uk.S.fldUserId |    1 | Using where |
    +------+-------------+-------+----------+------------------------------------------+----------+---------+-----------------------+------+-------------+
    

0 个答案:

没有答案