Hello,
We have the following 2 tables (and indexes) in our database:
Table 1
CREATE TABLE linkentityarticle(id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, article_id INT UNSIGNED NOT NULL, entity_id INT UNSIGNED NOT NULL, score TINYINT UNSIGNED NOT NULL) Engine = InnoDB;
Indexes
- entity_id
- entity_id + article_id
- article_id + entity_id
- score
Total # rows in table: 10 million
Total space used by table: 1.5 GB
Table 2
CREATE TABLE entity(id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, node VARCHAR(255)) Engine = InnoDB;
Indexes
- node
Total # rows in table: 600,000
Total space used by table: 88 MB
When I run the following query against these tables, it runs for over 30 seconds:
SELECT DISTINCT article_id
FROM linkentityarticle d
INNER JOIN
(
SELECT id FROM entity
WHERE node IN(‘sports’)
) AS T1
ON T1.id = d.entity_id
ORDER BY d.article_id DESC
LIMIT 50;
If I remove ORDER BY d.article_id DESC, it takes 0.3 seconds (i.e. 100 fold improvement).
Below is the EXPLAIN PLAN WITH ORDER BY
id: 1
select_type: PRIMARY
table:
type: system
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: 1
Extra: Using temporary; Using filesort
*************************** 2. row ***************************
id: 1
select_type: PRIMARY
table: d
type: ref
possible_keys: linkentityarticle_entity_id,index_linkentityarticle_entity_i d_article_id
key: linkentityarticle_entity_id
key_len: 4
ref: const
rows: 6708
Extra: Using where
*************************** 3. row ***************************
id: 2
select_type: DERIVED
table: drift_entity
type: const
possible_keys: unique_entity_node
key: unique_entity_node
key_len: 257
ref:
rows: 1
Extra: Using index
Below is the EXPLAIN WITHOUT ORDER BY:
id: 1
select_type: PRIMARY
table:
type: system
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: 1
Extra: Using temporary
*************************** 2. row ***************************
id: 1
select_type: PRIMARY
table: d
type: ref
possible_keys: linkentityarticle_entity_id,index_linkentityarticle_entity_i d_article_id
key: linkentityarticle_entity_id
key_len: 4
ref: const
rows: 6708
Extra:
*************************** 3. row ***************************
id: 2
select_type: DERIVED
table: drift_entity
type: const
possible_keys: unique_entity_node
key: unique_entity_node
key_len: 257
ref:
rows: 1
Extra: Using index
We are using MySQL version 5.0.44 and our InnoDB Buffer Pool is set to 2GB.
Is there anything that can be done to speed up this query when ORDER BY is used (as we need to use it to get the latest data).
Thank you in advance!