diff --git "a/data/2.PostgreSQL\344\270\255\351\230\266/1.PostgreSQL\346\225\260\346\215\256\345\272\223\347\232\204\345\237\272\346\234\254\347\273\223\346\236\204/3.\347\224\250\346\210\267\345\222\214\346\235\203\351\231\220/config.json" "b/data/2.PostgreSQL\344\270\255\351\230\266/1.PostgreSQL\346\225\260\346\215\256\345\272\223\347\232\204\345\237\272\346\234\254\347\273\223\346\236\204/3.\347\224\250\346\210\267\345\222\214\346\235\203\351\231\220/config.json" index 104d528c0ad9002bf6ca5542daeed975b050db86..e4497eb76243cb59821f3fc44430b0ce9a1ac978 100644 --- "a/data/2.PostgreSQL\344\270\255\351\230\266/1.PostgreSQL\346\225\260\346\215\256\345\272\223\347\232\204\345\237\272\346\234\254\347\273\223\346\236\204/3.\347\224\250\346\210\267\345\222\214\346\235\203\351\231\220/config.json" +++ "b/data/2.PostgreSQL\344\270\255\351\230\266/1.PostgreSQL\346\225\260\346\215\256\345\272\223\347\232\204\345\237\272\346\234\254\347\273\223\346\236\204/3.\347\224\250\346\210\267\345\222\214\346\235\203\351\231\220/config.json" @@ -2,5 +2,9 @@ "node_id": "pg-cb942198b3a84d5398d73d558a241543", "keywords": [], "children": [], - "export": [] + "export": [ + "grant.json", + "revoke.json", + "role.json" + ] } \ No newline at end of file diff --git "a/data/2.PostgreSQL\344\270\255\351\230\266/1.PostgreSQL\346\225\260\346\215\256\345\272\223\347\232\204\345\237\272\346\234\254\347\273\223\346\236\204/3.\347\224\250\346\210\267\345\222\214\346\235\203\351\231\220/revoke.json" "b/data/2.PostgreSQL\344\270\255\351\230\266/1.PostgreSQL\346\225\260\346\215\256\345\272\223\347\232\204\345\237\272\346\234\254\347\273\223\346\236\204/3.\347\224\250\346\210\267\345\222\214\346\235\203\351\231\220/revoke.json" index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..52f14c6b8277d7079591692ae16ccb5ed2b86146 100644 --- "a/data/2.PostgreSQL\344\270\255\351\230\266/1.PostgreSQL\346\225\260\346\215\256\345\272\223\347\232\204\345\237\272\346\234\254\347\273\223\346\236\204/3.\347\224\250\346\210\267\345\222\214\346\235\203\351\231\220/revoke.json" +++ "b/data/2.PostgreSQL\344\270\255\351\230\266/1.PostgreSQL\346\225\260\346\215\256\345\272\223\347\232\204\345\237\272\346\234\254\347\273\223\346\236\204/3.\347\224\250\346\210\267\345\222\214\346\235\203\351\231\220/revoke.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "刘鑫", + "source": "revoke.md", + "notebook_enable": false +} \ No newline at end of file diff --git "a/data/2.PostgreSQL\344\270\255\351\230\266/1.PostgreSQL\346\225\260\346\215\256\345\272\223\347\232\204\345\237\272\346\234\254\347\273\223\346\236\204/3.\347\224\250\346\210\267\345\222\214\346\235\203\351\231\220/role.json" "b/data/2.PostgreSQL\344\270\255\351\230\266/1.PostgreSQL\346\225\260\346\215\256\345\272\223\347\232\204\345\237\272\346\234\254\347\273\223\346\236\204/3.\347\224\250\346\210\267\345\222\214\346\235\203\351\231\220/role.json" index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..52051d6fde055ffc296430b3327608f955c65329 100644 --- "a/data/2.PostgreSQL\344\270\255\351\230\266/1.PostgreSQL\346\225\260\346\215\256\345\272\223\347\232\204\345\237\272\346\234\254\347\273\223\346\236\204/3.\347\224\250\346\210\267\345\222\214\346\235\203\351\231\220/role.json" +++ "b/data/2.PostgreSQL\344\270\255\351\230\266/1.PostgreSQL\346\225\260\346\215\256\345\272\223\347\232\204\345\237\272\346\234\254\347\273\223\346\236\204/3.\347\224\250\346\210\267\345\222\214\346\235\203\351\231\220/role.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "刘鑫", + "source": "role.md", + "notebook_enable": false +} \ No newline at end of file diff --git "a/data/2.PostgreSQL\344\270\255\351\230\266/1.PostgreSQL\346\225\260\346\215\256\345\272\223\347\232\204\345\237\272\346\234\254\347\273\223\346\236\204/4.\347\264\242\345\274\225\345\222\214\347\272\246\346\235\237/auto_id.json" "b/data/2.PostgreSQL\344\270\255\351\230\266/1.PostgreSQL\346\225\260\346\215\256\345\272\223\347\232\204\345\237\272\346\234\254\347\273\223\346\236\204/4.\347\264\242\345\274\225\345\222\214\347\272\246\346\235\237/auto_id.json" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/data/3.PostgreSQL\351\253\230\351\230\266/3.SQL\351\253\230\347\272\247\346\212\200\345\267\247/4.\347\264\242\345\274\225\345\222\214\344\274\230\345\214\226/config.json" "b/data/3.PostgreSQL\351\253\230\351\230\266/3.SQL\351\253\230\347\272\247\346\212\200\345\267\247/4.\347\264\242\345\274\225\345\222\214\344\274\230\345\214\226/config.json" index 5d2906269ac6b4a71432d65b4a560aa9c022fd6d..ba03bfff6940ea99bfe380582963f12f836b179c 100644 --- "a/data/3.PostgreSQL\351\253\230\351\230\266/3.SQL\351\253\230\347\272\247\346\212\200\345\267\247/4.\347\264\242\345\274\225\345\222\214\344\274\230\345\214\226/config.json" +++ "b/data/3.PostgreSQL\351\253\230\351\230\266/3.SQL\351\253\230\347\272\247\346\212\200\345\267\247/4.\347\264\242\345\274\225\345\222\214\344\274\230\345\214\226/config.json" @@ -2,5 +2,8 @@ "node_id": "pg-79fe660f35f4469481db38509343c2b7", "keywords": [], "children": [], - "export": [] + "export": [ + "daily_payment.json", + "daily_payment_2.json" + ] } \ No newline at end of file diff --git "a/data/3.PostgreSQL\351\253\230\351\230\266/3.SQL\351\253\230\347\272\247\346\212\200\345\267\247/4.\347\264\242\345\274\225\345\222\214\344\274\230\345\214\226/daily_payment.json" "b/data/3.PostgreSQL\351\253\230\351\230\266/3.SQL\351\253\230\347\272\247\346\212\200\345\267\247/4.\347\264\242\345\274\225\345\222\214\344\274\230\345\214\226/daily_payment.json" new file mode 100644 index 0000000000000000000000000000000000000000..65fd37fb393a74ded608a984965ff589a9e236a0 --- /dev/null +++ "b/data/3.PostgreSQL\351\253\230\351\230\266/3.SQL\351\253\230\347\272\247\346\212\200\345\267\247/4.\347\264\242\345\274\225\345\222\214\344\274\230\345\214\226/daily_payment.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "刘鑫", + "source": "daily_payment.md", + "notebook_enable": false +} \ No newline at end of file diff --git "a/data/3.PostgreSQL\351\253\230\351\230\266/3.SQL\351\253\230\347\272\247\346\212\200\345\267\247/4.\347\264\242\345\274\225\345\222\214\344\274\230\345\214\226/daily_payment.md" "b/data/3.PostgreSQL\351\253\230\351\230\266/3.SQL\351\253\230\347\272\247\346\212\200\345\267\247/4.\347\264\242\345\274\225\345\222\214\344\274\230\345\214\226/daily_payment.md" new file mode 100644 index 0000000000000000000000000000000000000000..268803a7f9c177e068f07b978a68d5dfad310895 --- /dev/null +++ "b/data/3.PostgreSQL\351\253\230\351\230\266/3.SQL\351\253\230\347\272\247\346\212\200\345\267\247/4.\347\264\242\345\274\225\345\222\214\344\274\230\345\214\226/daily_payment.md" @@ -0,0 +1,80 @@ +# 每日报表 + +数据分析组需要经常执行以下查询生成从昨天到之前某一天的交易量统计 + +```postgresql +select payment_date::date as day, sum(amount) +from payment +where (payment_date::date) between $1 and 'yesterday'::date +group by payment_date::date; +``` + +现在这个查询很慢,payment 表的信息如下: + +```text + Table "public.payment" + Column | Type | Collation | Nullable | Default +--------------+-----------------------------+-----------+----------+--------------------------------------------- + payment_id | integer | | not null | nextval('payment_payment_id_seq'::regclass) + customer_id | smallint | | not null | + staff_id | smallint | | not null | + rental_id | integer | | not null | + amount | numeric(5,2) | | not null | + payment_date | timestamp without time zone | | not null | +Indexes: + "payment_pkey" PRIMARY KEY, btree (payment_id) +``` + +应该如何优化这个查询? + +## 答案 + +建立表达式索引 + +```postgresql +create index on payment((payment_date::date)); +``` + +## 选项 + +### 对性能优化并无帮助 + +建立视图 + +```postgresql +create view view_daily_payment as select payment_date::date as day, amount from payment; +``` + +然后在视图 view_daily_payment 上执行 + +```postgresql +select day, sum(amount) +from view_daily_payment +where day between $1 and 'yesterday'::date +group by day +``` + +### 对该查询帮助不大 + +在 payment_date 列上建立索引 + +```postgresql +create index on payment(payment_date); +``` + +### 可以简化语句,但对优化没有帮助 + +建立计算列 + +```postgresql +alter table payment add day date generated always as ( payment_date::date ) stored +``` + +然后使用它改写查询 + +```postgresql +select day as day, sum(amount) +from payment +where day between $1 and 'yesterday'::date +group by day; +``` \ No newline at end of file diff --git "a/data/3.PostgreSQL\351\253\230\351\230\266/3.SQL\351\253\230\347\272\247\346\212\200\345\267\247/4.\347\264\242\345\274\225\345\222\214\344\274\230\345\214\226/daily_payment_2.json" "b/data/3.PostgreSQL\351\253\230\351\230\266/3.SQL\351\253\230\347\272\247\346\212\200\345\267\247/4.\347\264\242\345\274\225\345\222\214\344\274\230\345\214\226/daily_payment_2.json" new file mode 100644 index 0000000000000000000000000000000000000000..3e60c8a925cb47dcf165e8ba80432bad3b03bed6 --- /dev/null +++ "b/data/3.PostgreSQL\351\253\230\351\230\266/3.SQL\351\253\230\347\272\247\346\212\200\345\267\247/4.\347\264\242\345\274\225\345\222\214\344\274\230\345\214\226/daily_payment_2.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "刘鑫", + "source": "daily_payment_2.md", + "notebook_enable": false +} \ No newline at end of file diff --git "a/data/3.PostgreSQL\351\253\230\351\230\266/3.SQL\351\253\230\347\272\247\346\212\200\345\267\247/4.\347\264\242\345\274\225\345\222\214\344\274\230\345\214\226/daily_payment_2.md" "b/data/3.PostgreSQL\351\253\230\351\230\266/3.SQL\351\253\230\347\272\247\346\212\200\345\267\247/4.\347\264\242\345\274\225\345\222\214\344\274\230\345\214\226/daily_payment_2.md" new file mode 100644 index 0000000000000000000000000000000000000000..0dd746923bc7e067ed07c50a5bfb01a1dc0be4bf --- /dev/null +++ "b/data/3.PostgreSQL\351\253\230\351\230\266/3.SQL\351\253\230\347\272\247\346\212\200\345\267\247/4.\347\264\242\345\274\225\345\222\214\344\274\230\345\214\226/daily_payment_2.md" @@ -0,0 +1,107 @@ +# 每日报表优化 + +# 每日报表 + +统计过去一段时间的数据,你发现 payment 表上每天的订单量很大,下面这个查询的统计过程占用了大多数数据库资源,查询不会 +早止当天,总是在历史上某一个日期段内查询 + +```postgresql +select payment_date::date as day, sum(amount) +from payment +where (payment_date::date) between $1 and $2 +group by payment_date::date; +``` + +payment 表的信息如下: + +```text + Table "public.payment" + Column | Type | Collation | Nullable | Default +--------------+-----------------------------+-----------+----------+--------------------------------------------- + payment_id | integer | | not null | nextval('payment_payment_id_seq'::regclass) + customer_id | smallint | | not null | + staff_id | smallint | | not null | + rental_id | integer | | not null | + amount | numeric(5,2) | | not null | + payment_date | timestamp without time zone | | not null | +Indexes: + "payment_pkey" PRIMARY KEY, btree (payment_id) +``` + +怎样做是最有效的? + +## 答案 + +建立物化视图并建立索引。 + +```postgresql +create materialized view view_daily_payment as + select payment_date::date as day, sum(amount) as amount from payment group by day; + +create index on view_daily_payment(day); + +``` + +使用 + +```postgresql +select day, amount from view_daily_payment where day between $1 and $2; +``` + +进行查询。并且每天定时执行一次刷新命令 + +```postgresql +REFRESH MATERIALIZED VIEW view_daily_payment; +``` + +## 选项 + +### 不会优化 sum 和 group by + +在 payment_date 列上建立索引 + +```postgresql +create index on payment(payment_date); +``` + +### 不会优化 sum + +建立计算列 + +```postgresql +alter table payment add day date generated always as ( payment_date::date ) stored +``` + +然后使用它改写查询 + +```postgresql +select day as day, sum(amount) +from payment +where day between $1 and 'yesterday'::date +group by day; +``` + +### 优化了日期查询,但是不会优化统计过程 + +建立表达式索引 + +```postgresql +create index on payment((payment_date::date)); +``` + +### 不做物化,对查询速度不会有显著改善 + +建立视图 + +```postgresql +create view view_daily_payment as select payment_date::date as day, amount from payment; +``` + +然后在视图 view_daily_payment 上执行 + +```postgresql +select day, sum(amount) +from view_daily_payment +where day between $1 and 'yesterday'::date +group by day +``` \ No newline at end of file