我试图找出以下是否是用于减少应用程序延迟的有据可查的模式(或反模式)。我已经尝试过这种技术,从表面上看,这似乎为我节省了大约 20% 的延迟。我想知道是否有任何我应该注意的副作用
语境:
你有一个方法/函数/过程,它对数据库进行多次 SELECT 调用,你需要优化它。
假设您的方法流程是:
getDBConnection()
execute("Select a,b from tableA");
bind a with varA
bind b with varB
---SOME Business Logic-----
execute("Select c,d from tableB");
bind c with varC
bind d with varD
---SOME more Business Logic-----
execute("Select e,f from tableC");
bind e with varE
bind f with varF
---SOME more Business Logic-----
releaseConnection()
解决方案:使用 Union ALL 对数据库进行一次调用
getDBConnection()
execute("Select a,b,'sqlA' from tableA"+
" UNION ALL "+
" Select c,d,'sqlB' from tableB"+
" UNION ALL "+
"Select e,f,'sqlC' from tableC");
bind a,b where records have "sqlA"
bind c,d where records have "sqlB"
bind e,f where records have "sqlC"
releaseConnection()
--------Do all Business Logic here-----