0

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?

3
  • indexes(category, token, reported_at). Dores this means three separate single-column indices or one composite index? Provide complete CREATE TABLE instead of your approximate and partial description.
    – Akina
    Commented Jun 2, 2022 at 12:54
  • I am required to find how many bugs were created on 2019-03-01 or later. The task is not relative to indices presence.
    – Akina
    Commented Jun 2, 2022 at 12:54
  • Not sure what this has got to do with indices? This is a trivially simple SQL query, so what issue are you facing? You need to show what you’ve managed to write for yourself and ask a question about a specific issue you are facing
    – NickW
    Commented Jun 2, 2022 at 15:58

1 Answer 1

0
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.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.