我有簡單的功能由一個SQL查詢MySQL的不使用功能指標
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
該函數的執行時間需要5秒
的select Benchmark(500 ,GetProductIdFunc('sample_product'));
平原查詢的執行時間花費0.001秒
select Benchmark(500,(select id from products where title = 'sample_product' limit 1));
「標題」字段被索引。爲什麼功能執行需要很多時間,我如何優化它?
編輯: 執行計劃
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)
可以包括執行計劃? – MJB 2010-07-16 14:32:22