The answer depends a great deal on how well organized your data is and the query itself.
For example, look at the query you have in the question:
SELECT rank, COUNT(id) FROM tablename GROUP BY rank
The first thing I think about with this query is whether the table is properly indexed.
OBSERVATION #1
If tablename had no indexes, a full table scan would be required.
OBSERVATION #2
If tablename had an index on rank, you still get a full table scan because of the MySQL Query Optimizer ruling out the use of the index because of factors such as key distribution and the possibility of having to lookup each id for every rank during a full index scan.
OBSERVATION #3
If the table had a compound index of (rank,id), then you can a full index scan. In most cases, a full index scan that never references the table for non-indexed columns would be faster than a full index scan that does (See OBSERVATION #2)
OBSERVATION #4
If the query was written slightly different
SELECT rank, COUNT(1) FROM tablename GROUP BY rank
then an index on just the rank column would suffice and produce a full index scan.
CONCLUSION
In light of these observtions, it is definitely a thing of beauty to present to the MySQL Query Optimizer two things:
- a good query
- proper indexes for all tables in the query
In retrospect, it is also good to give the MySQL Query Optimizer as much of an advantage upfront as possible.
Unless I am missing something, your query would be something like this:
select created, count(*) CreatedCount
from yourtable
group by created
order by created;
See SQL Fiddle with Demo
Or if you have a time associated with the date, you can use TRUNC
:
select trunc(created), count(*) CreatedCount
from yourtable
group by trunc(created)
order by trunc(created);
See SQL Fiddle with Demo
Best Answer
To display the given format, you can use the following queries instead: