This is a pretty amazing piece of automation that we've been building at Facebook for the past few years, and I'm excited to be able to speak about it in public.
Update: It is now also available at the new Facebook Code site.
SELECT ROUND(numeric_value, -2) AS bucket,
COUNT(*) AS COUNT,
RPAD('', LN(COUNT(*)), '*') AS bar
FROM my_table
GROUP BY bucket;
Just change numeric_value to whatever your column is, change the rounding increment, and that's it. I've made the bars to be in logarithmic scale, so that they don't grow too much when you have large values.And it is achieved using a simple query such as this one:+-----------------+-----------------+----------+ | host_short | users | count(*) | +-----------------+-----------------+----------+ | slave1 | repl | 1 | | slave2 | repl | 1 | | localhost | event_scheduler | 1 | | 111.111.222.111 | root, foo | 2 | | 111.111.222.222 | appuser, bar | 3 | | 111.111.222.333 | appuser, moshe | 9 | +-----------------+-----------------+----------+
In the background, MongoDB will dump and restore it automatically. There is no metadata "magic" for such a rename since databases reside in different files on disk. It just saves a bit of work, and it's worth knowing about.db.runCommand({renameCollection:"sourcedb.mycol",to:"targetdb.mycol"})
Collection names should begin with letters or an underscore and may include numbers; $ is reserved. Collections can be organized in namespaces; these are named groups of collections defined using a dot notation.The keyword here is "should".
This is because the javascript console tries to subtract "col" from "test". I've tried different escaping methods for the rogue hyphen, but couldn't find one that works.>db.test-col.find() Sat Aug 6 14:54:06 ReferenceError: col is not defined (shell):1
Which solves the problem completely.>db.runCommand({renameCollection:"test.test-col",to:"test.test_col"})
I can only assume drivers use this same command to create the collections, which was how it was created in the first place.>db.runCommand({"create":"test-col"}); { "ok" : 1 }
CREATE TABLE `t` (Run this:
`a` INT(11) NOT NULL,
`b` INT(11) NOT NULL,
PRIMARY KEY (`a`,`b`)
) ENGINE=INNODB;
mysql> EXPLAIN SELECT COUNT(*) FROM t GROUP BY b,a\G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: t
type: index
possible_keys: NULL
key: PRIMARY
key_len: 8
ref: NULL
rows: 1
Extra: Using index; Using temporary; Using filesort
mysql> EXPLAIN SELECT COUNT(*) FROM t GROUP BY b,a ORDER BY NULL\GWhat you get now is: Using index; Using temporary
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: t
type: index
possible_keys: NULL
key: PRIMARY
key_len: 8
ref: NULL
rows: 1
Extra: Using index; Using temporary