工做中遇到個狀況sql
sql以下:函數
SELECT org.id orgid,org."path" FROM ( SELECT * FROM A INNER JOIN t_org org ON ... INNER JOIN C org ON ... INNER JOIN D b ON ... INNER JOIN E bsns ON ... LEFT JOIN F logopttp ON ... LEFT JOIN G sysopttp ON ... WHERE 1 = 1 -- AND org.PATH ~ '(/177412/)';-- 加上這一行,時間12s,去掉以後0.3s,懷疑是由於模糊查詢致使了全表掃描
解決方式:1.給須要查詢快的字段加上gin索引spa
2. 用position函數,替代模糊查詢code
SELECT org.id orgid,org."path" FROM ( SELECT * FROM A INNER JOIN t_org org ON ... INNER JOIN C org ON ... INNER JOIN D b ON ... INNER JOIN E bsns ON ... LEFT JOIN F logopttp ON ... LEFT JOIN G sysopttp ON ... WHERE 1 = 1 AND position('/177412/' in org.path)>0;-- 這樣的寫法不致使全表掃描