2

以下查询不使用索引,解释计划中的类型为“ALL”

解释扩展

SELECT 
  GROUP_CONCAT(CONCAT(fname,' ',lname)) 
FROM employee WHERE FIND_IN_SET(emp_id,'111582,111624')>0;

有什么方法可以更改查询以使用索引。

解释计划输出:

 id : 1
 select_type :SIMPLE
 table  : employee 
 type   : ALL
 possible_keys  : {null}
 key    : {null}
 key_len    : {null}
 ref    : {null}
 rows  : 546
 filtered : 100
 Extra: Using where

请建议我改进查询以使用索引。

提前致谢,

4

2 回答 2

1

FIND_IN_SET是一个字符串函数,并且(在大多数情况下)将导致全表扫描......

利用:

WHERE emp_id IN (111582,111624)
于 2013-04-25T07:46:48.573 回答
1

Since the answer comes way too late, I was also in the same requirement and found that FIND_IN_SET still works for column as well. Example (1 is the emp_id)

SELECT * FROM employee WHERE FIND_IN_SET(1,comma_separated_column_name)

Here is my example enter image description here

The query selects all trips that have 1 in the weekend column

于 2020-12-31T11:05:11.747 回答