Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/7.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 在where子句中使用CONCAT时提高性能查询_Mysql_Symfony - Fatal编程技术网

Mysql 在where子句中使用CONCAT时提高性能查询

Mysql 在where子句中使用CONCAT时提高性能查询,mysql,symfony,Mysql,Symfony,我的问题是: SELECT * FROM user u LEFT JOIN user_detail ud ON u.id = ud.user_id WHERE CONCAT(ud.first_name,' ',ud.last_name) LIKE 'John Smith%' 我在first_name和last_name列上创建了两个索引,但我知道在where子句中使用CONCAT时它们不起作用,因为CONCAT扫描整个表。 假设我无法更改表以创建一个新列,即full\u name。在这种

我的问题是:

SELECT * 
FROM user u 
LEFT JOIN user_detail ud ON u.id = ud.user_id 
WHERE CONCAT(ud.first_name,' ',ud.last_name) LIKE 'John Smith%'
我在first_name和last_name列上创建了两个索引,但我知道在where子句中使用CONCAT时它们不起作用,因为CONCAT扫描整个表。 假设我无法更改表以创建一个新列,即
full\u name
。在这种情况下,是否仍有提高性能的方法

注意: 因为输入可以是任何文本,如名字、姓氏或全名,所以我使用CONCAT,完整查询为:

SELECT * 
FROM user u 
LEFT JOIN user_detail ud ON u.id = ud.user_id 
WHERE (CONCAT(ud.first_name,' ',ud.last_name) LIKE 'search%') OR (CONCAT(ud.last_name,' ',ud.first_name) LIKE 'search%')

如果您的目的是始终使用concat,那么只需创建函数索引,而不是对单独的列进行索引

CREATE INDEX idx1 ON user_Details (CONCAT(ud.first_name,' ',ud.last_name));
否则,我建议您拆分而不是串联

WHERE ud.first_name = '?' AND ud.last_name LIKE '?%'

如果使用以下选项速度更快,您已尝试:

ud.first_name = 'John' AND d.last_name like 'Smith%'
第一个字符串上的比较可能已经中止,并且equals比like快


另请参见:

为什么不
其中ud.first\u name='John'和ud.last\u name像'Smith%'
?您的左连接与右表的WHERE条件相矛盾(连接类型转换为内部)。函数索引需要MySQL 8.0.13及更高版本。您的第二次查询不会返回行,例如,
first\u name='John Smith'
。除非OP明确声明,否则我认为它是版本8+。mysql 8已经很长时间没有运行了。您的查询将不会返回行,例如,
first\u name='John Smith'
。真的!对于first_name='John Smith'和last_name='',它不会返回任何项目。但这不是干净的数据。如果您希望涵盖此类情况,那么我建议在优化查询之前清理数据。