本文簡(jiǎn)單介紹了PostgreSQL的插件:auto_explain。該插件自動(dòng)explain SQL語(yǔ)句并把執(zhí)行計(jì)劃打印在日志文件中。
網(wǎng)站建設(shè)公司,為您提供網(wǎng)站建設(shè),網(wǎng)站制作,網(wǎng)頁(yè)設(shè)計(jì)及定制網(wǎng)站建設(shè)服務(wù),專注于企業(yè)網(wǎng)站建設(shè),高端網(wǎng)頁(yè)制作,對(duì)成都塑料袋等多個(gè)行業(yè)擁有豐富的網(wǎng)站建設(shè)經(jīng)驗(yàn)的網(wǎng)站建設(shè)公司。專業(yè)網(wǎng)站設(shè)計(jì),網(wǎng)站優(yōu)化推廣哪家好,專業(yè)網(wǎng)站推廣優(yōu)化,H5建站,響應(yīng)式網(wǎng)站。
安裝
編譯安裝
[pg12@localhost auto_explain]$ make
make -C ../../src/backend generated-headers
make[1]: Entering directory `/home/pg12/source/postgresql-12.1/src/backend'
make -C catalog distprep generated-header-symlinks
make[2]: Entering directory `/home/pg12/source/postgresql-12.1/src/backend/catalog'
make[2]: Nothing to be done for `distprep'.
make[2]: Nothing to be done for `generated-header-symlinks'.
make[2]: Leaving directory `/home/pg12/source/postgresql-12.1/src/backend/catalog'
make -C utils distprep generated-header-symlinks
make[2]: Entering directory `/home/pg12/source/postgresql-12.1/src/backend/utils'
make[2]: Nothing to be done for `distprep'.
make[2]: Nothing to be done for `generated-header-symlinks'.
make[2]: Leaving directory `/home/pg12/source/postgresql-12.1/src/backend/utils'
make[1]: Leaving directory `/home/pg12/source/postgresql-12.1/src/backend'
gcc -std=gnu99 -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Werror=vla -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -g -O0 -DOPTIMIZER_DEBUG -g3 -gdwarf-2 -fPIC -I. -I. -I../../src/include -D_GNU_SOURCE -I/usr/include/libxml2 -c -o auto_explain.o auto_explain.c -MMD -MP -MF .deps/auto_explain.Po
gcc -std=gnu99 -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Werror=vla -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -g -O0 -DOPTIMIZER_DEBUG -g3 -gdwarf-2 -fPIC -shared -o auto_explain.so auto_explain.o -L../../src/port -L../../src/common -Wl,--as-needed -Wl,-rpath,'/appdb/pg12/pg12.1/lib',--enable-new-dtags
[pg12@localhost auto_explain]$ make install
make -C ../../src/backend generated-headers
make[1]: Entering directory `/home/pg12/source/postgresql-12.1/src/backend'
make -C catalog distprep generated-header-symlinks
make[2]: Entering directory `/home/pg12/source/postgresql-12.1/src/backend/catalog'
make[2]: Nothing to be done for `distprep'.
make[2]: Nothing to be done for `generated-header-symlinks'.
make[2]: Leaving directory `/home/pg12/source/postgresql-12.1/src/backend/catalog'
make -C utils distprep generated-header-symlinks
make[2]: Entering directory `/home/pg12/source/postgresql-12.1/src/backend/utils'
make[2]: Nothing to be done for `distprep'.
make[2]: Nothing to be done for `generated-header-symlinks'.
make[2]: Leaving directory `/home/pg12/source/postgresql-12.1/src/backend/utils'
make[1]: Leaving directory `/home/pg12/source/postgresql-12.1/src/backend'
/bin/mkdir -p '/appdb/pg12/pg12.1/lib/postgresql'
/bin/install -c -m 755 auto_explain.so '/appdb/pg12/pg12.1/lib/postgresql/auto_explain.so'
體驗(yàn)
修改配置文件,重啟數(shù)據(jù)庫(kù)
[pg12@localhost pg121db]$ cat postgresql.auto.conf
# Do not edit this file manually!
# It will be overwritten by the ALTER SYSTEM command.
shared_preload_libraries = 'auto_explain,anon'
[pg12@localhost pg121db]$
[pg12@localhost auto_explain]$ pg_ctl restart
waiting for server to shut down.... done
server stopped
waiting for server to start....2019-11-20 16:32:21.013 CST [20847] LOG: starting PostgreSQL 12.1 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-16), 64-bit
2019-11-20 16:32:21.013 CST [20847] LOG: listening on IPv4 address "0.0.0.0", port 5432
2019-11-20 16:32:21.013 CST [20847] LOG: listening on IPv6 address "::", port 5432
2019-11-20 16:32:21.015 CST [20847] LOG: listening on Unix socket "/data/pg12/.s.PGSQL.5432"
2019-11-20 16:32:21.088 CST [20847] LOG: redirecting log output to logging collector process
2019-11-20 16:32:21.088 CST [20847] HINT: Future log output will appear in directory "pg_log".
done
server started
[pg12@localhost auto_explain]$
設(shè)置auto_explain參數(shù),并加載參數(shù)
[local:/data/pg12]:5432 pg12@testdb=# alter system set auto_explain.log_min_duration = 0;
ALTER SYSTEM
[local:/data/pg12]:5432 pg12@testdb=# alter system set auto_explain.log_analyze = on;
ALTER SYSTEM
[local:/data/pg12]:5432 pg12@testdb=#
...
[pg12@localhost pg121db]$ pg_ctl reload
server signaled
[pg12@localhost pg121db]$
在psql中執(zhí)行查詢,檢查日志輸出
[local:/data/pg12]:5432 pg12@testdb=# select * from tbl;
id
----
(0 rows)
[local:/data/pg12]:5432 pg12@testdb=#
2019-11-20 16:35:34.480 CST,"pg12","testdb",20869,"[local]",5dd4fa24.5185,62,"SELECT",2019-11-20 16:32:36 CST,3/8,0,LOG,00000,"duration: 0.019 ms plan:
Query Text: select * from tbl;
Seq Scan on tbl (cost=0.00..35.50 rows=2550 width=4) (actual time=0.009..0.009 rows=0 loops=1)",,,,,,,,,"psql"
---------------------------------
[local:/data/pg12]:5432 pg12@testdb=# select * from tbl where id = 100::float;
id
-----
100
(1 row)
[local:/data/pg12]:5432 pg12@testdb=#
2019-11-20 16:54:08.280 CST,"pg12","testdb",21506,"[local]",5dd4fcf2.5402,2,"SELECT",2019-11-20 16:44:34 CST,3/5,0,LOG,00000,"duration: 38.947 ms plan:
Query Text: select * from tbl where id = 100::float;
Seq Scan on tbl (cost=0.00..1943.00 rows=500 width=4) (actual time=0.135..38.925 rows=1 loops=1)
Filter: ((id)::double precision = '100'::double precision)
Rows Removed by Filter: 99999",,,,,,,,,"psql"
使用JDBC程序測(cè)試
/*
*
*/
package testPG;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class TestAutoExplain {
public static void main(String[] args) {
try (Connection conn = DriverManager.getConnection("jdbc:postgresql://192.168.26.28:5432/testdb", "pg12",
"root")) {
TestExplicitType(conn);
TestImplicitType(conn);
} catch (SQLException se) {
System.out.println(se.getMessage());
} catch (Exception e) {
e.printStackTrace();
} finally {
} // end try
}
public static void TestExplicitType(Connection conn) {
try (PreparedStatement pstmt = conn.prepareStatement("SELECT id from tbl where id = ?");) {
conn.setAutoCommit(true);
pstmt.setInt(1, 100);
try (ResultSet rs = pstmt.executeQuery()) {
if (rs.next()) {
int id = rs.getInt("id");
System.out.println("id:" + id);
}
}
} catch (SQLException se) {
System.out.println(se.getMessage());
} catch (Exception e) {
e.printStackTrace();
} finally {
} // end try
} // end
public static void TestImplicitType(Connection conn) {
try (PreparedStatement pstmt = conn.prepareStatement("SELECT id from tbl where id = ?");) {
conn.setAutoCommit(true);
pstmt.setObject(1, 100);
try (ResultSet rs = pstmt.executeQuery()) {
if (rs.next()) {
int id = rs.getInt("id");
System.out.println("id:" + id);
}
}
} catch (SQLException se) {
System.out.println(se.getMessage());
} catch (Exception e) {
e.printStackTrace();
} finally {
} // end try
} // end
} // end Class
數(shù)據(jù)庫(kù)后臺(tái)日志輸出
2019-11-20 16:45:55.349 CST,"pg12","testdb",21581,"192.168.119.33:54001",5dd4fd43.544d,1,"SELECT",2019-11-20 16:45:55 CST,4/22,0,LOG,00000,"duration: 28.367 ms plan:
Query Text: SELECT id from tbl where id = $1
Index Only Scan using idx_tbl_id on tbl (cost=0.29..8.31 rows=1 width=4) (actual time=28.301..28.311 rows=1 loops=1)
Index Cond: (id = 100)
Heap Fetches: 1",,,,,,,,,"PostgreSQL JDBC Driver"
2019-11-20 16:45:55.365 CST,"pg12","testdb",21581,"192.168.119.33:54001",5dd4fd43.544d,2,"SELECT",2019-11-20 16:45:55 CST,4/23,0,LOG,00000,"duration: 0.080 ms plan:
Query Text: SELECT id from tbl where id = $1
Index Only Scan using idx_tbl_id on tbl (cost=0.29..8.31 rows=1 width=4) (actual time=0.064..0.067 rows=1 loops=1)
Index Cond: (id = 100)
Heap Fetches: 1",,,,,,,,,"PostgreSQL JDBC Driver"
雖然使用setObject作為參數(shù)設(shè)定,但驅(qū)動(dòng)正確設(shè)置了參數(shù)類型為int,并沒(méi)有出現(xiàn)double -> int的轉(zhuǎn)換。
參考資料
Making Mystery-Solving Easier with auto_explain
網(wǎng)頁(yè)標(biāo)題:PostgreSQLDBA(134)-Extension(auto_explain)
本文路徑:http://jinyejixie.com/article44/jdosee.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供域名注冊(cè)、動(dòng)態(tài)網(wǎng)站、網(wǎng)站維護(hù)、手機(jī)網(wǎng)站建設(shè)、網(wǎng)站改版、小程序開(kāi)發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
營(yíng)銷型網(wǎng)站建設(shè)知識(shí)