J'ai fonction simple consiste d'une requête SQLfonction MySql ne pas utiliser les index
CREATE FUNCTION `GetProductIDFunc`(in_title char (14))
RETURNS bigint(20)
BEGIN
declare out_id bigint;
select id into out_id from products where title = in_title limit 1;
RETURN out_id;
END
Temps d'exécution de cette fonction prend 5 secondes
select Benchmark(500 ,GetProductIdFunc('sample_product'));
Temps d'exécution de la requête simple Takes 0,001 secondes
select Benchmark(500,(select id from products where title = 'sample_product' limit 1));
Le champ "Titre" est indexé. Pourquoi l'exécution de fonction prend tellement de temps et comment puis-je l'optimiser?
modifier: Plan d'exécution
mysql> EXPLAIN EXTENDED select id from products where title = 'sample_product' limit 1;
+----+-------------+----------+-------+---------------+------------+---------+-------+------+----------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+----------+-------+---------------+------------+---------+-------+------+----------+-------------+
| 1 | SIMPLE | products | const | Index_title | Index_title | 14 | const | 1 | 100.00 | Using index |
+----+-------------+----------+-------+---------------+------------+---------+-------+------+----------+-------------+
1 row in set, 1 warning (0.00 sec)
mysql> EXPLAIN select GetProductIdFunc('sample_product');
+----+-------------+-------+------+---------------+------+---------+------+------+----------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+------+---------------+------+---------+------+------+----------------+
| 1 | SIMPLE | NULL | NULL | NULL | NULL | NULL | NULL | NULL | No tables used |
+----+-------------+-------+------+---------------+------+---------+------+------+----------------+
1 row in set (0.00 sec)
peut vous inclure le plan d'exécution? – MJB