PostgreSQL和JDBC挤榨了所有果汁。弗拉基米尔·西特尼科夫

我建议您熟悉Vladimir Sitnikov在2016年初的报告中所写的文字:“ PostgreSQL和JDBC挤占了所有果汁”




! . 10 NetCracker. . , Java, , SQL – , .


, , PostgreSQL . Java. , , Java. , .



:


  • .
  • .
  • .
  • , .


. .



. 20 .



20 – . 100 , , , . . .


, . .



– . ? , , .



https://github.com/pgjdbc/pgjdbc/pull/478


, , . , , . .


Super extended query – , . , , - , - , . . , , . , .



, , – simple query extended query.


?


. . , , . . - .



Extended query – . , . - . . . .



. . Java . .


statement. . close. ? ? . . . , .



, . ? «close». , – . «PARSE EXECUTE DEALLOCATE».


statements? . PreparedStatement , , . , .



, , . statement, . – . REST statement id. .



?



– statements. : «prepare» «execute».




, , - - . , . , . statement. - , , .


, . . , . , .



? ?


statements. , , .


PostgreSQL . , .


.



.


– , PgSQL. . . . . 100500 . . – . -.


– . , . -. , .



https://github.com/pgjdbc/pgjdbc/pull/319


2015 . . . , . PgSQL, . . , .


Server-prepared statements 5- , .



– ? ? , .


, OLTP- - 20 . 0,5 , 20 . – 10 , 170 . OLTP . 1, 5, 10 , .


20 . 0 . .


? Java, .


- , – ? , , PL 8 LibPQ, , , . ? .



, , - . . , , , .



. . - , SQL-.


? , .


hashCode. CPU – hash’ . – . - . .



. . ORM, , NULL, -. Int, setInt. NULL, VARCHAR . NULL? . .


. , , , VARCHAR, Server-prepared statements. statement.



, , . NULL. , , PreparedStatements



, . , , . . .


? ? , – . . . . , , . , , - , . .



https://gist.github.com/vlsi/df08cbef370b2e86a5c1


, , . NULL . 20 . , .


, . . «?» «$1» , ?



https://gist.github.com/vlsi/df08cbef370b2e86a5c1


– . – . - . --. – - . , . , , , ?



? ? . generic . , , - generic , , , , . , . - , .


? , , - . , . +0, OFFSET 0. , . «+0» . .


– . , 6 «explain analyze». 5, .


– pgsql-hackers . , , – .



https://gist.github.com/vlsi/df08cbef370b2e86a5c1


– , . «+0». . , . . . «+0» , , .



6- explain’. 6 , . , . . .


, ? , . .



. , . . – . ? . . – , .



: «?». , , , , «search_path», , . , .


? , server-prepared statements , search_path - . . - .



, , . , . 9.1 . , .



Set search_path + server-prepared statements =
cached plan must not change result type


? – . search_path . , .


, . . , , . , , , - , : «, . , statements , ?». , - statements .


– , Java. PL/ pgSQL . .



. -. . . . Java- 128 .


, , . . . resultSet . ? ? - ? , Postgres . . OutOFMemory? OutOfMemory? ? - .


, . OFFSET/LIMIT. ? , autoCommit’ ?


, , . autoCommit, . ? .



, Postgres, . PgJDBC , .


FetchSize, . . statement , , , 10, 50. , autoCommit. autoCommit – .


setFetchSize – . , .



. . ? , , , 10 , . .



, , , : defaultRowFetchSize .



. – , . , INSERT, VALUES. . «INSERT SELECT». . .


, Batch statement, , . Postgres – COPY , . . .



, . , ? .



TCP . , , . , , , .



. , .


, . , 200 , . .



https://github.com/pgjdbc/pgjdbc/pull/380


, 10 . . ? , - . «128» – batching.



Java microbenchmark harness


, . , . , , .



. InsertBatch . InsertBatch , . . , values . . , , , COPY.



COPY.



. User default type, INSERT .


: pgjdbc/ubenchmsrk/InsertBatch.java, GitHub. , . .



. , , batch – . batching , . . .



. . . ? , . COPY, , .



. , VALUES, VALUES, VALUES 10 . . 1, 2, 4, 128. , Batch Insert, , . . . , , , VALUES . EXECUTE.


COPY – . . , . . COPY.


COPY , . . , , . Batch , COPY – .


接下来我们要做什么?他们测量了它。我们知道,我们必须使用结构或巧妙的方法,结合多种价值。



需要从今天的报告中学到什么?


  • PreparedStatement是我们的一切。它为性能提供了很多。它产生了一大桶焦油。
  • 并且您需要做EXPLAIN ANALYZE 6次。
  • 而且,您需要稀释OFFSET 0和+0之类的技巧,以编辑保留在那里的有问题查询的百分比。

All Articles