Given a table called Bugs with the columns (id, token, title, category, device, reported_at, created_at, updated_at).
indexes(category, token, reported_at).
I am required to find how many bugs were created on 2019-03-01 or later.
Any ideas?
Given a table called Bugs with the columns (id, token, title, category, device, reported_at, created_at, updated_at).
indexes(category, token, reported_at).
I am required to find how many bugs were created on 2019-03-01 or later.
Any ideas?
SELECT COUNT(*)
FROM bugs
WHERE reported_at reported_at >= '2019-03-01';
Indexing is a performance issue. Without a suitable index, the query will have to check every row. If there are 1K rows, it will be "fast enough". If there are a billion rows, it will work, but it will be "painfully slow".
"indexes(category, token, reported_at)." is ambiguous. What did you mean?
Did you mean that there is a "composite" index like
INDEX(category, token, reported_at)
That would be especially useful if the WHERE
clause filters on category
and perhaps the other two columns.
For your SELECT
, the Optimizer is likely to use this 3-column index, but because it is "covering". That is, all the columns in the SELECT
are included. (Namely, just reported_at
.) "Covering" provides a small performance benefit because it does not have to look both in the Index's BTree and the data's BTree.
Or did you mean that there are 3 single-column indexes:
INDEX(category),
INDEX(token),
INDEX(reported_at)
In this case, the Optimizer would use that last index because it will make your query more efficient.
Another note: 2019 looks like you might be scanning most of the table? If so, the Optimizer may choose to ignore any indexes and simply scan the entire table. (This 'surprises' many novices. But it is deliberate, and sometimes correctly faster.)