How to redirect output in an interactive session?

In an interactive session, i want to redirect the output to a file, for example, ‘/dev/null’,
because i just care about the ‘execution time’
How to redirect output in an interactive session?
Thanks.

Hello! Thanks for your interest in CockroachDB!

Currently the SQL shell does not support redirecting just the query results to a file, however you can filter the output of cockroach sql with grep.

You can invoke as follows:

  • interactive use: type
    cockroach sql | grep '^Time'
    then type \set show_times, press enter, then type your SQL queries.
  • non-interactive use: put your SQL queries in a file test.sql then run:
    (echo '\set show_times'; cat test.sql) | cockroach sql --echo-sql | grep '^Time'

Does this help?

I also highly recommend that you set the display_format to tsv instead of the default (pretty), because the default setting is computationally expensive and will inflate the execution time.

As a hack, I think you could discard the output and still get timing information by adding OFFSET 1000000000 LIMIT 1 to your query. The OFFSET clause can’t be optimized so it will still have to execute the whole query, but there will be no output (unless your query has a billion results, in which case you could use a larger offset).

@knz @bdarnell Thanks for your replies!
these methods are helpful.

1 Like