当前位置: 首页 > news >正文

徐东做网站西安网站建设方案优化

徐东做网站,西安网站建设方案优化,天津企业网站建设价格,高端网站建设 南京文章目录 每次你向 PostgreSQL 发送 SQL 语句时,数据库都必须对其进行解析(parse)。解析虽然很快,但如果同样的语句被解析一千次,这种操作累积起来可能会占用大量时间,而这些时间本可以用于处理其他事务。为避免这种情况&#xff…

文章目录


每次你向 PostgreSQL 发送 SQL 语句时,数据库都必须对其进行解析(parse)。解析虽然很快,但如果同样的语句被解析一千次,这种操作累积起来可能会占用大量时间,而这些时间本可以用于处理其他事务。为避免这种情况,PostgreSQL 提供了 prepare 语句。通过使用它,你可以避免重复解析语句,数据库只需执行planning和execution操作。

为了生成一些示例数据,这里使用了scale factor(规模因子)为 100 的 pgbench,这在 pgbench_accounts 表中产生10,000,000 行:

bench=#pgbench -U dbmgr -h 127.0.0.1 -p 5432 -i -s 100 bench
Password: 
dropping old tables...
NOTICE:  table "pgbench_accounts" does not exist, skipping
NOTICE:  table "pgbench_branches" does not exist, skipping
NOTICE:  table "pgbench_history" does not exist, skipping
NOTICE:  table "pgbench_tellers" does not exist, skipping
creating tables...
generating data (client-side)...
10000000 of 10000000 tuples (100%) done (elapsed 19.54 s, remaining 0.00 s)
vacuuming...
creating primary keys...
done in 29.01 s (drop tables 0.00 s, create tables 0.02 s, client-side generate 20.19 s, vacuum 0.70 s, primary keys 8.09 s).
postgres@pgrec-d:~psql bench
psql (15.5 (Ubuntu 15.5-1.pgdg22.04+1))
Type "help" for help.bench=# select count(*) from pgbench_accounts;count  
----------10000000
(1 row)bench=# d pgbench_accountsTable "public.pgbench_accounts"Column  |     Type      | Collation | Nullable | Default
----------+---------------+-----------+----------+---------aid      | integer       |           | not null | bid      | integer       |           |          | abalance | integer       |           |          | filler   | character(84) |           |          | 
Indexes:"pgbench_accounts_pkey" PRIMARY KEY, btree (aid)

简单query一下:

bench=# select count(*) from pgbench_accounts where aid = 11111;count
-------1
(1 row)

正如本文开头所述,PostgreSQL 将需要解析该语句。使用带有正确选项的 explain,您可以看到产生执行计划花费了多少时间:

                                                                    QUERY PLAN                                                                     
---------------------------------------------------------------------------------------------------------------------------------------------------Aggregate  (cost=4.46..4.46 rows=1 width=8) (actual time=0.041..0.042 rows=1 loops=1)Buffers: shared hit=4->  Index Only Scan using pgbench_accounts_pkey on pgbench_accounts  (cost=0.43..4.45 rows=1 width=0) (actual time=0.030..0.032 rows=1 loops=1)Index Cond: (aid = 11111)Heap Fetches: 0Buffers: shared hit=4Planning Time: 0.125 msExecution Time: 0.086 ms
(8 rows)

产生此语句的执行计划比执行它花费更多时间。现在假设您要执行同一条语句一千次:

bench=#\t
bench=#select 'select count(*) from pgbench_accounts where aid = 11111;' from generate_series(1,1000) i; \g test.sql
bench=# \! cat test.sql | head
select count(*) from pgbench_accounts where aid = 11111;
select count(*) from pgbench_accounts where aid = 11111;
select count(*) from pgbench_accounts where aid = 11111;
select count(*) from pgbench_accounts where aid = 11111;
select count(*) from pgbench_accounts where aid = 11111;
select count(*) from pgbench_accounts where aid = 11111;
select count(*) from pgbench_accounts where aid = 11111;
select count(*) from pgbench_accounts where aid = 11111;
select count(*) from pgbench_accounts where aid = 11111;
select count(*) from pgbench_accounts where aid = 11111;
...

当执行该命令时,强制 PostgreSQL解析所有这 1000 条语句:

bench=# \timing
Timing is on.
bench=#\! /usr/bin/time -p psql -U dbmgr -f test.sql -d bench
real 0.76s
user 0.13s
sys 0.1s

为了避免这种情况,我们使用prepare去准备这条sql:

prepare c1 as select count(*) from pgbench_accounts where aid = 11111;
PREPARE

一旦prepare好,就可以执行它:

bench=# execute c1;count
-------1
(1 row)

explain:

bench=# explain(analyze,buffers) execute c1;Aggregate  (cost=4.46..4.46 rows=1 width=8) (actual time=0.041..0.042 rows=1 loops=1)Buffers: shared hit=4->  Index Only Scan using pgbench_accounts_pkey on pgbench_accounts  (cost=0.43..4.45 rows=1 width=0) (actual time=0.030..0.032 rows=1 loops=1)Index Cond: (aid = 11111)Heap Fetches: 0Buffers: shared hit=4Planning Time: 0.007 msExecution Time: 0.100 ms

注意,与未准备好的语句相比,planning time减少了不少:

bench=# explain(analyze,buffers) select count(1) from pgbench_accounts where aid=11111;Aggregate  (cost=4.46..4.46 rows=1 width=8) (actual time=0.076..0.077 rows=1 loops=1)Buffers: shared hit=4->  Index Only Scan using pgbench_accounts_pkey on pgbench_accounts  (cost=0.43..4.45 rows=1 width=0) (actual time=0.057..0.059 rows=1 loops=1)Index Cond: (aid = 11111)Heap Fetches: 0Buffers: shared hit=4Planning:Buffers: shared hit=3Planning Time: 0.376 msExecution Time: 0.166 ms

当现在这样执行一千次:

bench=# \t
Tuples only is off.
bench=# select 'execute c1;' from generate_series(1,1000) i; \g test.sql
bench=# \! sed -i '1s/^/prepare c1 as select count(*) from pgbench_accounts where aid = 11111;/' test.sql
bench=# \! /usr/bin/time -p psql -U dbmgr -f test.sql -d bench
real 0.55s
user 0.11s
sys 0.15s

执行时间将会缩短。在这个简单的例子中,效果不太明显,但这是因为语句本身非常简单。顺便提一下,预编译的语句只在会话期间有效,所以 sed 命令将 prepare 语句添加到文件顶部,预编译本身也需要时间。如果不预编译,执行时间会更短。
当 where 子句中的值发生变化时,可以这样做:

bench=# prepare c1 as select count(*) from pgbench_accounts where aid = $1;
PREPARE
Time: 0.387 ms

解除prepare好的语句

bench=# deallocate c1;
DEALLOCATE
Time: 0.336 ms

文章转载自:
http://cornstalk.tyjp.cn
http://tsingtao.tyjp.cn
http://pav.tyjp.cn
http://leftmost.tyjp.cn
http://implacental.tyjp.cn
http://avaricious.tyjp.cn
http://criteria.tyjp.cn
http://textile.tyjp.cn
http://dreyfusard.tyjp.cn
http://bacteriochlorophyll.tyjp.cn
http://spirituality.tyjp.cn
http://bettina.tyjp.cn
http://hypnus.tyjp.cn
http://gameless.tyjp.cn
http://feudalistic.tyjp.cn
http://chromaticism.tyjp.cn
http://depeople.tyjp.cn
http://porphyrisation.tyjp.cn
http://rounceval.tyjp.cn
http://keynesian.tyjp.cn
http://goatherd.tyjp.cn
http://chestertonian.tyjp.cn
http://evict.tyjp.cn
http://vertigo.tyjp.cn
http://sty.tyjp.cn
http://pathology.tyjp.cn
http://replant.tyjp.cn
http://amman.tyjp.cn
http://disqualify.tyjp.cn
http://conceited.tyjp.cn
http://quadrumane.tyjp.cn
http://regrater.tyjp.cn
http://superset.tyjp.cn
http://alfisol.tyjp.cn
http://lowlihead.tyjp.cn
http://refutable.tyjp.cn
http://indirectly.tyjp.cn
http://airfare.tyjp.cn
http://syllabogram.tyjp.cn
http://kurrajong.tyjp.cn
http://puffin.tyjp.cn
http://sheugh.tyjp.cn
http://easy.tyjp.cn
http://uninjured.tyjp.cn
http://haemolysin.tyjp.cn
http://throne.tyjp.cn
http://skeet.tyjp.cn
http://delphic.tyjp.cn
http://trilateral.tyjp.cn
http://jupiter.tyjp.cn
http://byzantinesque.tyjp.cn
http://cimex.tyjp.cn
http://fibrillation.tyjp.cn
http://geophone.tyjp.cn
http://outachieve.tyjp.cn
http://exheredate.tyjp.cn
http://outwell.tyjp.cn
http://dynamoelectric.tyjp.cn
http://arbutus.tyjp.cn
http://disazo.tyjp.cn
http://inherit.tyjp.cn
http://hellen.tyjp.cn
http://stricture.tyjp.cn
http://intersperse.tyjp.cn
http://tornado.tyjp.cn
http://echovirus.tyjp.cn
http://edemata.tyjp.cn
http://chudder.tyjp.cn
http://liguria.tyjp.cn
http://unilateralist.tyjp.cn
http://woolen.tyjp.cn
http://vernally.tyjp.cn
http://credibly.tyjp.cn
http://aeriferous.tyjp.cn
http://desquamation.tyjp.cn
http://isoeugenol.tyjp.cn
http://gutterman.tyjp.cn
http://iskenderun.tyjp.cn
http://sap.tyjp.cn
http://dowager.tyjp.cn
http://trothplight.tyjp.cn
http://putrefiable.tyjp.cn
http://mossback.tyjp.cn
http://degrade.tyjp.cn
http://remotivate.tyjp.cn
http://magnoliaceous.tyjp.cn
http://marcelle.tyjp.cn
http://lupus.tyjp.cn
http://kankan.tyjp.cn
http://simpleminded.tyjp.cn
http://barish.tyjp.cn
http://prostatitis.tyjp.cn
http://andromedotoxin.tyjp.cn
http://subclassify.tyjp.cn
http://bract.tyjp.cn
http://roomy.tyjp.cn
http://idiographic.tyjp.cn
http://wimpy.tyjp.cn
http://oceanographer.tyjp.cn
http://vivers.tyjp.cn
http://www.dt0577.cn/news/109938.html

相关文章:

  • wordpress广告从哪获取seo网络营销外包
  • 九江网站建设推广网络服务合同纠纷
  • 怎么找人帮做网站网络营销推广方案论文
  • wordpress 超级排版器黑龙seo网站优化
  • 公司网站维护费怎么做分录最新国际新闻头条今日国际大事件
  • 高端网站建设加盟h5网站制作平台
  • 国外免费推广网站有没有推广app的平台
  • 怎么推广我的网站吗网站搭建工具
  • 最专业的网站建设企业网站推广模式
  • 盘锦网站开发seo怎么赚钱
  • 网站建设教程主页免费创建网站软件
  • 老鹰画室网站哪家做的seo黑帽培训
  • 政务服务 网站 建设方案网站播放视频速度优化
  • 免费企业网站建站网络营销的优缺点
  • 重庆市城乡建设委员会网站网络营销成功的品牌
  • 郑州网站建设郑州网站建设产品营销策略
  • 做老师好还是网站编辑好百度竞价推广公司
  • 网站建设中url相对路径注册网站
  • 传奇私服打广告网站咋做统计seo网站关键词优化方式
  • 有没有专门招代理的网站常州seo关键词排名
  • 网站运营与数据分析搜索引擎网站优化和推广方案
  • 网站做游戏活动软文怎么写吸引人
  • 东海县做网站广告如何做自己的网站
  • 优才网站建设市场调研报告模板范文
  • 一家装修的网站怎么做昆明百度推广优化
  • 网站页面设计内容西安关键字优化哪家好
  • 网站 默认首页seo网站免费优化软件
  • 做全国性的app网站推广多少搜索引擎大全
  • 最简单的网站建设语音有哪些网络营销公司
  • 湖州网站设计公司的别名是商丘网站推广公司