Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MySQL关于优化大型数据库查询的帮助/建议_Mysql_Full Text Search_Query Optimization_Large Data - Fatal编程技术网

MySQL关于优化大型数据库查询的帮助/建议

MySQL关于优化大型数据库查询的帮助/建议,mysql,full-text-search,query-optimization,large-data,Mysql,Full Text Search,Query Optimization,Large Data,我有一个MyISAM mysql表,其中包含: CREATE TABLE IF NOT EXISTS `songs` ( `rid` int(11) NOT NULL auto_increment, `aid` int(11) NOT NULL, `song_title` varchar(256) NOT NULL, `download_url` varchar(256) NOT NULL, PRIMARY KEY (`rid`), UNIQUE KEY `downlo

我有一个MyISAM mysql表,其中包含:

CREATE TABLE IF NOT EXISTS `songs` (
  `rid` int(11) NOT NULL auto_increment,
  `aid` int(11) NOT NULL,
  `song_title` varchar(256) NOT NULL,
  `download_url` varchar(256) NOT NULL,
  PRIMARY KEY  (`rid`),
  UNIQUE KEY `download_url` (`download_url`),
  KEY `song_title` (`song_title`),
  FULLTEXT KEY `song_title_2` (`song_title`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1
它大约有1400万行。这是我第一次处理这么大的数据库,之前我并不真正关心优化。我一直在尝试各种方法来测试速度和准确性

1)全文

select song_title from songs 
where match (song_title) againt ('search term') limit 0, 50
-- This gives me very unreliable results but speed is good.
2)喜欢

select song_title from songs 
where song_title LIKE '%search term%' limit 0, 50
-- Moderate matching results, speed is good when the query is 
-- easily able to fetch the first 50 results... but when i 
-- search for a term that does not exist then... here is the result..
-- MySQL returned an empty result set (i.e. zero rows). ( Query took 107.1371 sec )
select song_title from songs 
where song_title like '%word_1%' and 
      song_title like '%word_2%' and 
      song_title like '%word_3%' and 
      song_title like '%word_N%' LIMIT 0, 50;
-- It takes about 0.2 seconds when the search terms are easily found.
-- Ran this exact above query just now to find the execution time when 
-- no results are found.
-- MySQL returned an empty result set (i.e. zero rows). ( Query took 30.8625 sec )
3)多重相似

select song_title from songs 
where song_title LIKE '%search term%' limit 0, 50
-- Moderate matching results, speed is good when the query is 
-- easily able to fetch the first 50 results... but when i 
-- search for a term that does not exist then... here is the result..
-- MySQL returned an empty result set (i.e. zero rows). ( Query took 107.1371 sec )
select song_title from songs 
where song_title like '%word_1%' and 
      song_title like '%word_2%' and 
      song_title like '%word_3%' and 
      song_title like '%word_N%' LIMIT 0, 50;
-- It takes about 0.2 seconds when the search terms are easily found.
-- Ran this exact above query just now to find the execution time when 
-- no results are found.
-- MySQL returned an empty result set (i.e. zero rows). ( Query took 30.8625 sec )
我想要的是关于优化数据库/查询的技巧和建议,包括速度和准确性


我不能使用其他搜索引擎,如sphinx,因为我在网站根目录之外没有访问权限,也不能要求处理服务器的人员进行设置。

使用类似“%text%”的查询不使用索引。 如果您希望获得良好的性能,请使用全文版,即使它不能准确返回结果。 如果可以使用命令
explain select…
,查看查询中使用的索引


您可以在此处查看更多信息:

MySQL无法创建包含的索引,例如“%word%”带有前导百分号的查询,因为用于该类型索引的B树索引的性质。它将使用索引进行前缀搜索,如如'word%'。请注意,MySQL全文索引根本不包含LIKE查询。MyISAM全文索引包含的唯一查询是匹配。。。反对…

假设您的数据集大小,您确实需要一个外部搜索引擎,特别是如果您计划增加搜索的数据量

关于您的托管环境,我没有太多的详细信息,但是如果您有对托管服务器的SSH访问权,我相信您可以作为非特权用户安装和运行Sphinx。使用./configure script将位置前缀设置为您的主目录(但请确保无法从web访问),如下所示:

./configure --prefix=/path/to/your/home
然后表演

make && make install
然后按照中所述创建sphinx配置,最后从命令行运行searchd启动守护程序:

/path/to/your/home/bin/searchd

希望有帮助。

哦,差点忘了解释。。我读过但从未尝试过。。我们可以从现在查询的实际工作方式中学到一些东西。。好的..刚刚尝试了like搜索,它确实使用了歌曲标题索引。。我猜你的意思是它没有使用全文索引歌曲的标题_2@PrathameshGharat你能不能提供
解释
结果,因为像“%word%”这样的
不应该使用索引,它可以在
explain
可能键列中,但不在
键列中。@piotrekkr:
选择歌曲标题,歌曲标题如“%word%”
歌曲标题
上使用索引(索引包含查询覆盖索引的所有字段,因此mysql扫描索引,因为它比表扫描快)。索引还将用于mysql 5.1手册中表1中的
SELECT song\u title
@a1ex07:以下SELECT语句不使用索引:
SELECT*FROM tbl\u name WHERE key\u col LIKE“%Patrick%”不了解5.5,因为在5.5手册中找不到有关索引和
的任何信息。很抱歉编辑了这么多。。这里的第一个问题是学习如何缩进代码块。。但是在一行的左边加了4个空格之后,我似乎不能正确地理解它。要缩进代码块-高亮显示并按ctrl+K什么是您的服务器端语言?