admin管理员组文章数量:1125786
I have mysql query that looks kinda like this
SELECT * FROM table1
WHERE table1.column_1 = 'a' AND table1.column_2 = 'b'
AND :idList IN
(SELECT id FROM table1 WHERE table1.column_3 = 'c' AND table1.column_4 = 'd')
:idList is a parameter passed in a function where this query is written
I need to add indexes to table1
to speedup this operation, because table1
is huge and doesn't have any indexes on used columns. If I understand this correct, after adding two indexes (column_1, column_2)
and (column_3, column_4)
, only one of them will be used.
What would be correct approach for adding indexes here? Options I tried so far:
- Add index on
(column_1, column_2)
. Run query. Drop index and add another on(column_3, column_4)
. Run query. Stop on fastest variant - Add index on
(column_1, column_2, column_3, column_4)
I am aware of EXPLAIN
but it hasn't been too helpful in this particular case so far
I have mysql query that looks kinda like this
SELECT * FROM table1
WHERE table1.column_1 = 'a' AND table1.column_2 = 'b'
AND :idList IN
(SELECT id FROM table1 WHERE table1.column_3 = 'c' AND table1.column_4 = 'd')
:idList is a parameter passed in a function where this query is written
I need to add indexes to table1
to speedup this operation, because table1
is huge and doesn't have any indexes on used columns. If I understand this correct, after adding two indexes (column_1, column_2)
and (column_3, column_4)
, only one of them will be used.
What would be correct approach for adding indexes here? Options I tried so far:
- Add index on
(column_1, column_2)
. Run query. Drop index and add another on(column_3, column_4)
. Run query. Stop on fastest variant - Add index on
(column_1, column_2, column_3, column_4)
I am aware of EXPLAIN
but it hasn't been too helpful in this particular case so far
1 Answer
Reset to default 0You said:
If I understand this correct, after adding two indexes (column_1, column_2) and (column_3, column_4), only one of them will be used.
In fact, two separate indices on (column_1, column_2)
and (column_3, column_4)
should help the performance. The first index can be used in the main query, while the second one can be used in the subquery. It should be noted that in MySQL, running on InnoDB, the id
column would effectively be added to the end of the two indices, meaning the first index would really behave as (column_1, column_2, id)
and the same for the second index.
Given that the main query and subquery are separate steps in the query pipeline, there is no reason why MySQL cannot both indices in the same query. So, the tuning you have now is already good, and you should verify this by running:
EXPLAIN ANALYZE
SELECT *
FROM table1 t1
WHERE column_1 = 'a' AND column_2 = 'b' AND
id IN (
SELECT id
FROM table1 t2
WHERE t2.column_3 = 'c' AND t2.column_4 = 'd
);
The output from the above will reveal to you if the indices you defined are being used.
本文标签: sqlMysql multicolumn indexes approach when table used twice in one queryStack Overflow
版权声明:本文标题:sql - Mysql multi-column indexes approach when table used twice in one query - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736665414a1946635.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
table2.id IN
makes no sense, because there is no second table mentioned there. Please clarify your code. – Tim Biegeleisen Commented 2 days ago(column_3, column_4, id)
will optimize the subquery. And the index by(column_1, column_2)
will optimize outer query. You tell that this is a code within the function - transform it toIF EXISTS (your subquery) THEN (your outer query)
. – Akina Commented 2 days ago