I have a MySQL query that I am trying to optimize. thought it would be a good time to at least learn some sort of decent query optimization.....
This table currently has 2 million rows.
We will most likely archive data older than a month, but monthly data would still be +4-5 million rows
"event_name" has 5 distinct values ("open", "close", "download", etc), and shouldnt grow much more.
"asset_identifier" could have 10s of thousands of distinct values. 99% of these are product EANs, eg "9780636188617" (not guaranteed to be an number).
This is a typical query, Top 10 Assets by Event in the last 7 days
I have created an index as follows:
The `EXPLAIN` on the above outputs the following:
Read performance is all I care about. I could change the DDL of the table, but I dont really want to unless it will improve performance in a significant way.
PHP:
cre8 tbl foo_event
(
event_name VARCAR(20) NOT NULL,
event_date DATETIME NOT NULL,
app_id VARCAR(100),
user_id BIGINT(20),
username VARCAR(100),
first_name VARCAR(100),
last_name VARCAR(100),
email VARCAR(100),
asset_identifier VARCAR(100),
asset_title VARCAR(255)
); TYPE INNODB
This table currently has 2 million rows.
We will most likely archive data older than a month, but monthly data would still be +4-5 million rows
"event_name" has 5 distinct values ("open", "close", "download", etc), and shouldnt grow much more.
"asset_identifier" could have 10s of thousands of distinct values. 99% of these are product EANs, eg "9780636188617" (not guaranteed to be an number).
This is a typical query, Top 10 Assets by Event in the last 7 days
PHP:
SEL
event_name,
asset_identifier,
asset_title,
count(*) AS `eventCount`
FROM `foo_event`
WHERE (event_name = 'close' AND
event_date BETWEEN '2017-03-07 00:00:00.0' AND '2017-03-13 23:59:59.999')
GROUP BY asset_identifier
ORDER BY eventCount DESC
LIMIT 10;
I have created an index as follows:
PHP:
CREATE INDEX foo_event_asset_by_event_date_range_index ON foo_event (event_name, event_date, asset_identifier);
The `EXPLAIN` on the above outputs the following:
PHP:
id, select_type, table, type, possible_keys, key, key_len, ref, rows, Extra
1, SIMPLE, foo_event, range, foo_event_name_index,foo_event_event_date_index,foo_event_asset_by_event_date_range_index,foo_event_active_users_date_range_index, foo_event_asset_by_event_date_range_index, 27, NULL, 102228, Using index condition; Using temporary; Using filesort
.
Read performance is all I care about. I could change the DDL of the table, but I dont really want to unless it will improve performance in a significant way.
Last edited: