UNPKG

942 BJavaScriptView Raw
1'use strict'
2
3const co = require('co')
4const cli = require('heroku-cli-util')
5const pg = require('heroku-pg')
6
7const query = `
8SELECT 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
20function * 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
26const 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
35module.exports = [
36 Object.assign({command: 'index-usage'}, cmd),
37 Object.assign({command: 'index_usage', hidden: true}, cmd)
38]