Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/56.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 使用LIKE';%进行查询期限%';速度很慢_Mysql_Innodb_Sql Like - Fatal编程技术网

Mysql 使用LIKE';%进行查询期限%';速度很慢

Mysql 使用LIKE';%进行查询期限%';速度很慢,mysql,innodb,sql-like,Mysql,Innodb,Sql Like,我有一个InnoDB表,大约1000000行: Data 164.7 MiB Index 250.1 MiB Overhead 168.0 MiB Effective 246.8 MiB Total 414.8 MiB 表结构: CREATE TABLE IF NOT EXISTS `gift` ( `tm` varchar(15) NOT NULL, `col` smallin

我有一个InnoDB表,大约1000000行:

Data            164.7   MiB
Index           250.1   MiB
Overhead        168.0   MiB
Effective       246.8   MiB
Total           414.8   MiB
表结构:

CREATE TABLE IF NOT EXISTS `gift` (
  `tm` varchar(15) NOT NULL,
  `col` smallint(2) NOT NULL,
  `myindex` varchar(255) NOT NULL,
  `date` int(10) NOT NULL,
  KEY `tm` (`tm`),
  KEY `date` (`date`),
  KEY `myindex` (`myindex`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
tm
是由PHP(产品id)生成的不重复字符串

date
存储使用
strotime('now')

myindex
存储搜索关键字

由于某些产品具有长描述(超过255个字符),因此
col
是具有多个部分的独立长描述(相同的产品,关键字第1部分,关键字第2部分…)

这样的查询将产生(总共10个,查询耗时51.0829秒)

做一个解释,得到:

id  select_type table   type    possible_keys   key key_len ref rows    Extra
1   SIMPLE      gift    index   NULL            date    4   NULL 10 Using where; Using temporary
可能的_键为空,我是否制作了错误的索引?如何使它最快

这里是
my.cnf
,我的服务器有4GB内存,mysql也支持全文搜索

我尝试将
key\u buffer\u size
innodb\u buffer\u pool\u size
设置为512MB,但服务器很容易崩溃,并发出警告
apache子进程仍然没有退出发送sigterm
。我必须为系统和apache保留至少1GB的ram吗?(my.cnf的容量仅为2.5-3GB?)


首先,您使用的是非法分组。检查第一段

第二,似乎根本不需要分组,这会导致你的“临时”出现在解释中

第三,(不要引用我的话,不是100%确定)您的临时tmp_table_size=64M对于这个查询来说可能太小了,因为它在分组之前将过滤结果存储在内存中

第四,如果您确实需要分组,请为该字符串指定一个整数,然后按整数分组

第五,如果您有关键字,请将它们存储在item关键字表中,并将它们与联接一起使用。由于加入,将给您带来一些性能上的影响,但它将允许您使用索引。索引仅在从一开始就比较字符串时起作用(因此,在varchar上使用长度为6-10的索引可能与使用长度为100的索引一样有效)


愉快的优化:)

您的实际问题是,MySQL不可能对像“%keywords1%”这样的条件使用索引。这个主题在网络上有广泛的介绍,所以我建议您寻找全文索引解决方案。您可以使用内部索引(MySQL中的索引)或外部索引(Sphinx、Lucene等)。

在开始时使用
%
将消除键的使用。@Bart Friederichs,那么如何进行这样的搜索并使索引生效呢?不要像
那样使用
。将搜索词放在另一个表中,并进行引用或类似的操作。@Bart Friederichs,不太明白,你能给我一些简单的查询代码吗?对不起,不,但你可以检查以下内容:
id  select_type table   type    possible_keys   key key_len ref rows    Extra
1   SIMPLE      gift    index   NULL            date    4   NULL 10 Using where; Using temporary
[mysqld]
character_set_server=utf8
port        = 3306
socket      = /var/lib/mysql/mysql.sock
skip-external-locking
skip-networking
key_buffer = 256M
tmp_table_size = 64M
max_connections = 300
wait_timeout=15
back_log = 2048
key_buffer_size = 384M
max_allowed_packet = 2M
table_cache = 2048
table_open_cache = 2048
sort_buffer_size = 6M
read_buffer_size = 4M
net_buffer_length = 92K
read_rnd_buffer_size = 4M
myisam_sort_buffer_size = 256M
thread_cache = 384
query_cache_size= 256M
bulk_insert_buffer_size = 192M
ft_min_word_len=2
skip-networking

binlog_format=mixed

innodb_buffer_pool_size = 384M
innodb_buffer_pool_instances=4

innodb_use_sys_malloc = 0

[mysqldump]
quick
max_allowed_packet = 8M

[mysql]
no-auto-rehash

[myisamchk]
key_buffer_size = 96M
sort_buffer_size = 96M
read_buffer = 3M
write_buffer = 3M