使用的是SQLSERVER数据库
一、自定义函数F2Y,在对在查询自定义函数时的性能影响,REPORT_PAY_TOTAL_TRADE为自定义函数
CREATE FUNCTION [dbo].[F2Y] ( @n_fen numeric(15,2) ) RETURNS numeric(15,2) AS BEGIN Declare @n_yuan numeric(15,2) select @n_yuan = convert(numeric(15,2),round(@n_fen/100,2)) return @n_yuan END
查询sql
SELECT ISNULL(SUM(W_3000_C), 0) W_3000_C, ISNULL(SUM(W_3000_M), 0) W_3000_M FROM REPORT_PAY_TOTAL_TRADE ( '2022-07-26 00:00:00', '2022-07-28 23:59:59' ) WHERE 1 = 1
不加自定义函数F2Y,查询时间为0.258s
加上自定义函数之后查询时间为78.021秒,明显慢了太多倍
二、自定义函数F2Y,在对查询视图时的影响,VIEW_REPORT_LOCAL_TEMP为视图
select ISNULL(SUM(W_3000_C), 0) W_3000_C,dbo.f2y(ISNULL(SUM(W_3000_M), 0)) W_3000_M from VIEW_REPORT_LOCAL_TEMP where tradeDate >= CONVERT (DATETIME,'2022-07-26 00:00:00',120) and tradeDate<=CONVERT (DATETIME,'2022-07-28 23:59:59',120)
不加F2Y函数时查询时间为0.494s
加上F2Y函数时查询时间为1.282s,影响不是太大