MySQL匹配查询调优

时间:2011-06-27 11:19:51

标签: mysql qt

我们的一个应用程序查询(书籍搜索)执行时间超过3分钟。下面我给出了查询,表格描述和配置细节。

查询

Select  identifier,sku_id,vol_num,TITLE,TITLE_OPT,AUTHOR_TITLE,AUTHOR_FNAME,AUTHOR_MNAME,AUTHOR_LNAME, AUTHOR_ALIAS,AUTHORS, ORIG_PUBLISH_DATE, match(TITLE,AUTHOR_TITLE,AUTHOR_FNAME,AUTHOR_MNAME,AUTHOR_LNAME,AUTHOR_ALIAS, AUTHORS,SUBTITLE,ORIG_PUBLISH_DATE,DESCRIPTION,OTHER_INFO,CATEGORY,REMARKS)  against ('+ history' in boolean mode) as score  
from bp_search as bp 
where  match(TITLE,AUTHOR_TITLE,AUTHOR_FNAME,AUTHOR_MNAME,AUTHOR_LNAME,AUTHOR_ALIAS,AUTHORS,SUBTITLE,ORIG_PUBLISH_DATE,DESCRIPTION, OTHER_INFO,CATEGORY,REMARKS) against ('+ history'  in boolean mode)  group by group_id  order by score desc limit 0,10;

表格描述

CREATE TABLE `bp_search` (
  `BOOK_ID` int(11) NOT NULL default '0',
  `GROUP_ID` varchar(128) default NULL,
  `IDENTIFIER` varchar(64) default NULL,
  `SKU_ID` varchar(15) default NULL,
  `TITLE` varchar(1600) default NULL,
  `AUTHOR_TITLE` varchar(100) default NULL,
  `AUTHOR_FNAME` varchar(256) default NULL,
  `AUTHOR_MNAME` varchar(256) default NULL,
  `AUTHOR_LNAME` varchar(2048) default NULL,
  `AUTHOR_ALIAS` varchar(128) default NULL,
  `AUTHORS` varchar(512) default NULL,
  `SUBTITLE` varchar(1600) default NULL,
  `ORIG_PUBLISH_DATE` varchar(64) default NULL,
  `DESCRIPTION` varchar(1024) default NULL,
  `OTHER_INFO` varchar(1024) default NULL,
  `CATEGORY` varchar(3072) default NULL,
  `REMARKS` varchar(1024) default NULL,
  `VOL_NUM` varchar(40) default NULL,
  `SOURCE` varchar(20) NOT NULL,
  `TITLE_OPT` varchar(1600) default NULL,
  `LAST_UPDATE` timestamp NOT NULL default '0000-00-00 00:00:00',
  KEY `GROUP_ID_idx` (`GROUP_ID`),
  FULLTEXT KEY `book_semantics` (`TITLE`,`AUTHOR_TITLE`,`AUTHOR_FNAME`,`AUTHOR_MNAME`,`AUTHOR_LNAME`,`AUTHOR_ALIAS`,`AUTHORS`,`SUBTITLE`,`ORIG_PUBLISH_DATE`,`DESCRIPTION`,`OTHER_INFO`,`CATEGORY`,`REMARKS`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8

配置

[mysqld]
socket          = /var/lib/mysql/mysql.sock
skip-locking
key_buffer = 384M
max_allowed_packet = 256M
table_cache = 512M
sort_buffer_size = 2M
read_buffer_size = 2M
read_rnd_buffer_size = 1M
myisam_sort_buffer_size = 64M
thread_cache_size = 80
query_cache_size = 32M
thread_concurrency = 8
query_cache_type = 1
query_cache_limit = 32M
thread_stack=256K
myisam_data_pointer_size=6
net_buffer_length = 8K
log-slow-queries=/var/lib/mysql/astdb01.as.osti.local-slow.log
log-warnings=2
max_connect_errors=1000
max_connections=1000
max_heap_table_size=1073741824
local_infile=0
ft_min_word_len=3
tmpdir =  /opt/
key_buffer_size=256M
connect_timeout=180
wait_timeout=360
tmp_table_size=851443712

请帮帮我吗?

1 个答案:

答案 0 :(得分:0)

我的第一个建议是尝试嵌套选择,因为MySQL通常不够智能,无法在查询中捕获重复的子句,因此match可能会运行两次。但是,您确定需要where条款吗?

Select * from (
Select  identifier,sku_id,vol_num,
TITLE,TITLE_OPT,AUTHOR_TITLE,
AUTHOR_FNAME,AUTHOR_MNAME,AUTHOR_LNAME,
AUTHOR_ALIAS,AUTHORS, ORIG_PUBLISH_DATE, match(TITLE,AUTHOR_TITLE,AUTHOR_FNAME,AUTHOR_MNAME,AUTHOR_LNAME,AUTHOR_ALIAS, AUTHORS,SUBTITLE,ORIG_PUBLISH_DATE,DESCRIPTION,OTHER_INFO,CATEGORY,REMARKS)
against ('+ history' in boolean mode) as score
from bp_search as bp group by group_id) as x where score  order by score desc limit 0,10;

修改

您可以使用HAVING子句替换WHERE子句。

Select  identifier,sku_id,vol_num,TITLE,TITLE_OPT,AUTHOR_TITLE,AUTHOR_FNAME,AUTHOR_MNAME,AUTHOR_LNAME, AUTHOR_ALIAS,AUTHORS, ORIG_PUBLISH_DATE, match(TITLE,AUTHOR_TITLE,AUTHOR_FNAME,AUTHOR_MNAME,AUTHOR_LNAME,AUTHOR_ALIAS, AUTHORS,SUBTITLE,ORIG_PUBLISH_DATE,DESCRIPTION,OTHER_INFO,CATEGORY,REMARKS)  against ('+ history' in boolean mode) as score  
from bp_search as bp 
where group by group_id having score order by score desc limit 0,10;

您可能还希望考虑SphinxLucene等替代方案。