Reputation: 796
I have a table which contains information about time, So the table has columns like year, month, day, hour and so on.
Table has data across years and quite big so i decided to make partition on this table and started learning about Mysql partitioning but caught up by few questions.
I will really appreciate, if someone can help me understand how partition and indexes will work together.
Ex. Sql: Select month, day, hour ... from time_table where year = '2017';
Ex Sql: Select year, month, day .... from time_table where month = '05';
Upvotes: 7
Views: 11095
Reputation: 142316
Partitioning splits a table up into, shall we say, "sub-tables". Each sub-table is essentially a table, with data and index(es).
When SELECTing
from the table, the first thing done is to decide which partition(s) may contain the desired data. This is "partition pruning" and uses the "partition key" (which is apparently to be year
). Then the select is applied to the subtables that are relevant, using whatever index is appropriate. In that case it is a waste to have INDEX(year, ...)
, since you are already pruned down to the year.
Your sample select cannot do partition pruning since you did not specify year
in the WHERE
clause. Hence, it will look in all partitions, and will be slower than if you did not partition the table.
YEAR(date)
, MONTH(date)
, etc.month='05'
)If you would like to back up a step and explain what you are trying to accomplish, perhaps we can discuss another approach.
Upvotes: 12