1 | 'use strict'
|
2 |
|
3 | const co = require('co')
|
4 | const cli = require('heroku-cli-util')
|
5 | const pg = require('heroku-pg')
|
6 |
|
7 | const query = `
|
8 | SELECT relname,
|
9 | CASE idx_scan
|
10 | WHEN 0 THEN 'Insufficient data'
|
11 | ELSE (100 * idx_scan / (seq_scan + idx_scan))::text
|
12 | END percent_of_times_index_used,
|
13 | n_live_tup rows_in_table
|
14 | FROM
|
15 | pg_stat_user_tables
|
16 | ORDER BY
|
17 | n_live_tup DESC;
|
18 | `
|
19 |
|
20 | function * run (context, heroku) {
|
21 | let db = yield pg.fetcher(heroku).database(context.app, context.args.database)
|
22 | let output = yield pg.psql.exec(db, query)
|
23 | process.stdout.write(output)
|
24 | }
|
25 |
|
26 | const cmd = {
|
27 | topic: 'pg',
|
28 | description: 'calculates your index hit rate (effective databases are at 99% and up)',
|
29 | needsApp: true,
|
30 | needsAuth: true,
|
31 | args: [{name: 'database', optional: true}],
|
32 | run: cli.command(co.wrap(run))
|
33 | }
|
34 |
|
35 | module.exports = [
|
36 | Object.assign({command: 'index-usage'}, cmd),
|
37 | Object.assign({command: 'index_usage', hidden: true}, cmd)
|
38 | ]
|