我们只是在我们的数据库中引入了一个新的plpgsql函数,为我们提供了一种方法来确定一个对我们来说至关重要的业务流中的索引。但是这段代码有很多行(48行),很难阅读。我的合作伙伴建议我把这个长函数分成小函数。对我来说这是一个很好的建议,为了使它更具可读性,我们在所有代码中都这样做。但我不确定这是否会影响数据库性能。
提前致谢,
顺便说一下这个功能,就是不要笑......但我会欣赏任何建议
CREATE OR REPLACE FUNCTION select_base_in_business_flow(line_uniq_code TEXT, line_customer_code TEXT, exception_name TEXT, filter TEXT,scope_published BOOLEAN) RETURNS INT AS $$
DECLARE
process_id int := 0;
count int := 0;
request_filter TEXT;
BEGIN
IF filter = 'ALL' THEN
request_filter := '%';
ELSE
request_filter := '%'|| filter || '%';
END IF;
SELECT special_request.process_id INTO process_id FROM special_request JOIN request ON request.id = special_request.process_id WHERE special_request.supplier_db_ref = line_customer_code AND request.published=scope_published AND request.name LIKE request_filter;
GET DIAGNOSTICS count = ROW_COUNT;
IF count != 1 THEN
SELECT request.id INTO process_id FROM request WHERE request.name LIKE request_filter AND request.published=scope_published AND request.db_ref = (SELECT replace(line_customer_code, reference_replacements.name_used_by_buyer, reference_replacements.name_used_by_supplier) FROM reference_replacements WHERE line_customer_code ~ reference_replacements.name_used_by_buyer);
GET DIAGNOSTICS count = ROW_COUNT;
IF count != 1 THEN
SELECT uniq_code_request_relations.process_id INTO process_id FROM uniq_code_request_relations JOIN request ON request.id = uniq_code_request_relations.process_id where uniq_code_request_relations.uniq_code = line_uniq_code AND request.published=scope_published AND request.name LIKE request_filter;
GET DIAGNOSTICS count = ROW_COUNT;
IF count != 1 THEN
SELECT request.id INTO process_id from request where request.name LIKE request_filter AND REGEXP_REPLACE(request.db_ref,'[^0-9A-Za-z]','') = REGEXP_REPLACE(line_customer_code,'[^0-9A-Za-z]','') AND request.published=scope_published;
GET DIAGNOSTICS count = ROW_COUNT;
IF count != 1 THEN
SELECT request.id INTO process_id from request where request.name LIKE request_filter AND REGEXP_REPLACE(request.db_ref,'^0+','') = REGEXP_REPLACE(line_customer_code,'^0+','') AND request.published=scope_published;
GET DIAGNOSTICS count = ROW_COUNT;
IF count != 1 THEN
IF exception_name = 'USE_REGEX' THEN
SELECT request.id INTO process_id from request WHERE request.name LIKE request_filter AND REGEXP_REPLACE(request.uniq_code,'^0+','') LIKE (REGEXP_REPLACE(line_uniq_code,'^0+','') || '%') AND request.published=scope_published;
ELSE
SELECT request.id INTO process_id from request WHERE request.name LIKE request_filter AND REGEXP_REPLACE(request.uniq_code,'^0+','') = REGEXP_REPLACE(line_uniq_code,'^0+','') AND request.published=scope_published;
END IF;
GET DIAGNOSTICS count = ROW_COUNT;
IF count != 1 THEN
process_id := 0;
END IF;
END IF;
END IF;
END IF;
END IF;
END IF;
RETURN process_id;
END;
$$ LANGUAGE plpgsql;