Mysql 搜索查询很慢怎么优化?
我得到了一个搜索查询,它使用一些连接在不同的相关表中进行搜索。但最近我在contactpersonen表中添加了大约3000个联系人。它变得非常慢 表格如下: 借方:1445项 联系人姓名:3711条 debiteuren_toegang:3008条分录Mysql 搜索查询很慢怎么优化?,mysql,search,indexing,Mysql,Search,Indexing,我得到了一个搜索查询,它使用一些连接在不同的相关表中进行搜索。但最近我在contactpersonen表中添加了大约3000个联系人。它变得非常慢 表格如下: 借方:1445项 联系人姓名:3711条 debiteuren_toegang:3008条分录 SELECT contactpersonen.id, contactpersonen.voornaam, contactpersonen.achternaam, debiteuren.bedrijfsn
SELECT
contactpersonen.id,
contactpersonen.voornaam,
contactpersonen.achternaam,
debiteuren.bedrijfsnaam,
debiteuren.id as debid
FROM
debiteuren
LEFT JOIN
contactpersonen ON contactpersonen.bedrijf = debiteuren.id
LEFT JOIN
debiteuren_toegang ON debiteuren_toegang.bedrijf = debiteuren.id
WHERE
(contactpersonen.voornaam LIKE '%henk%'
OR contactpersonen.achternaam LIKE '%henk%'
OR debiteuren.id LIKE '%henk%'
OR debiteuren.bedrijfsnaam LIKE '%henk%'
OR contactpersonen.id LIKE '%henk%')
AND debiteuren_toegang.website = 'web1'
LIMIT 10
当我删除通过类似于“%henk%”的contactpersonen.voornaam或类似于“%henk%”的contactpersonen.achternaam进行搜索的部分时查询再次变得非常快速
我在voornaam和achternaam的phpmyadmin中添加了一个索引,但这没有任何帮助
有什么办法能让这更快吗?我不认为这是太多的争吵,对吗?查询有时甚至持续5秒钟
谢谢
完整查询说明:
+----+-------------+--------------------+--------+---------------+---------+---------+---------------------------------------+------+-------------+--+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | |
+----+-------------+--------------------+--------+---------------+---------+---------+---------------------------------------+------+-------------+--+
| 1 | SIMPLE | debiteuren_toegang | ALL | NULL | NULL | NULL | NULL | 3008 | Using where | |
| 1 | SIMPLE | debiteuren | eq_ref | PRIMARY | PRIMARY | 4 | deb12311_1.debiteuren_toegang.bedrijf | 1 | | |
| 1 | SIMPLE | contactpersonen | ALL | NULL | NULL | NULL | NULL | 4169 | Using where | |
+----+-------------+--------------------+--------+---------------+---------+---------+---------------------------------------+------+-------------+--+
+----+-------------+--------------------+--------+---------------+---------+---------+---------------------------------------+------+-------------+--+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | |
+----+-------------+--------------------+--------+---------------+---------+---------+---------------------------------------+------+-------------+--+
| 1 | SIMPLE | debiteuren_toegang | ALL | NULL | NULL | NULL | NULL | 3008 | Using where | |
| 1 | SIMPLE | debiteuren | eq_ref | PRIMARY | PRIMARY | 4 | deb12311_1.debiteuren_toegang.bedrijf | 1 | Using where | |
| 1 | SIMPLE | contactpersonen | ALL | NULL | NULL | NULL | NULL | 4098 | | |
+----+-------------+--------------------+--------+---------------+---------+---------+---------------------------------------+------+-------------+--+
部分查询解释:
+----+-------------+--------------------+--------+---------------+---------+---------+---------------------------------------+------+-------------+--+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | |
+----+-------------+--------------------+--------+---------------+---------+---------+---------------------------------------+------+-------------+--+
| 1 | SIMPLE | debiteuren_toegang | ALL | NULL | NULL | NULL | NULL | 3008 | Using where | |
| 1 | SIMPLE | debiteuren | eq_ref | PRIMARY | PRIMARY | 4 | deb12311_1.debiteuren_toegang.bedrijf | 1 | | |
| 1 | SIMPLE | contactpersonen | ALL | NULL | NULL | NULL | NULL | 4169 | Using where | |
+----+-------------+--------------------+--------+---------------+---------+---------+---------------------------------------+------+-------------+--+
+----+-------------+--------------------+--------+---------------+---------+---------+---------------------------------------+------+-------------+--+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | |
+----+-------------+--------------------+--------+---------------+---------+---------+---------------------------------------+------+-------------+--+
| 1 | SIMPLE | debiteuren_toegang | ALL | NULL | NULL | NULL | NULL | 3008 | Using where | |
| 1 | SIMPLE | debiteuren | eq_ref | PRIMARY | PRIMARY | 4 | deb12311_1.debiteuren_toegang.bedrijf | 1 | Using where | |
| 1 | SIMPLE | contactpersonen | ALL | NULL | NULL | NULL | NULL | 4098 | | |
+----+-------------+--------------------+--------+---------------+---------+---------+---------------------------------------+------+-------------+--+
试试这个,` SELECT contactpersonen.id, contactpersonen.voornaam, contactpersonen.achternaam, debiteuren.bedrijfsnaam, debiteuren.id as debid FROM debiteuren LEFT JOIN contactpersonen ON contactpersonen.bedrijf = debiteuren.id LEFT JOIN debiteuren_toegang ON debiteuren_toegang.bedrijf = debiteuren.id WHERE debiteuren_toegang.website = 'web1' AND instr ( concat(contactpersonen.voornaam, contactpersonen.achternaam, debiteuren.id, debiteuren.bedrijfsnaam,contactpersonen.id) , 'henk' )>0 LIMIT 10` `挑选 联系人姓名:, 联系人:voornaam, 联系人姓名:achternaam, debiteuren.bedrijfsnaam, debiteuren.id作为debid 从…起 借方 左连接 contactpersonen.bedrijf=debiteuren.id上的contactpersonen 左连接 debiteuren\u toegang上的debiteuren\u toegang.bedrijf=debiteuren.id 哪里 debiteuren_toegang.website='web1' 及 仪器(concat(contactpersonen.voornaam,contactpersonen.achternaam,debiteuren.id,debiteuren.bedrijfsnaam,contactpersonen.id),'henk' )>0 限制10`
请为这两种情况添加
EXPLAIN
output one,它们没有太大区别。速度明显更快,但仍为1.3秒。原因是您有两个左连接。如果您可以在一个子查询中联接这些表,并且只使用一个左联接,则只需查看ur数据即可。还可以检查联接中使用的列的索引。还可以尝试在where子句中添加索引列,并限制select语句中的记录。Krishna您是指用于联接的列的索引?那么id是?是的,如果它不是主键,那么在这些列上建立索引将明确显示性能改进。。