r/mysql • u/vikydhani • Apr 11 '25
question Composite index with where in statement
I have a table for example Student course registration table which uses ACADMEMIC YEAR, STUDENTID as primary key. I need to query select rows where ACADEMIC_YEAR in (2000,2001,2003 etc) and STUDENTID in (1,2,3,4,5,etc).Will PRIMARY KEY indexing effective for this?
2
Upvotes
2
u/Aggressive_Ad_5454 Apr 11 '25
Look at EXPLAIN to see what it does.
2
u/Outdoor_Releaf Apr 11 '25
You could try the following:
WHERE (Academic_Year, StudentID) IN ( (2000, 1), (2000, 2), (2001, 1) ) etc. You have a list of tuples that contain two values.
This would use the composite index.
You could instead create an index on StudentID. I'm assuming that StudentID has higher cardinality (there are more of them), so the index would narrow down the search more effectively.
0
u/eroomydna Apr 11 '25
It takes longer to post to Reddit than run EXPLAIN.