indexing - MySQL: make a compound index of 3 fields, or make 3 separate indices? -
i have mysql table has, among other attributes, timestamp, type , user_id.
of them searchable and/or sortable.
better create index each one, or create single compound index three, or both?
pablo's answer correct, maybe you'll fail realize compound index might justified.
you can have multiple indexes , having idx1(tstamp, user_id)
not exclude having indx2(tstamp, type)
or idx1reverse(user_id, tstamp)
, on...
compound indexes useful when cover of conditions in query, index propose useful for
select * my_table tstamp = @ts1 , user_id = @uid , type = @type
if want improve performance of such queries can consider adding composite index.
the downside of indexes slows down update operations. however, general applications many more selects updates (both in terms transactions i.e. number of statements , in terms of of records affected/retrieved) , @ same time more tolerant of slower updates (users judge speed of system not time necessary update record, time necessary retrieve records; again ymmv , there applications don't play such rules).
the best if had way test database performance in terms of typical workloads (create typical sql scripts; independent , repeatable, or create unit tests @ application level) , can objectively tune database.
edit realize indexes can added , dropped without affecting system in terms of functionality. therefore, can tune indexes later, during actual usage of system - , collect , profile slow sql queries looking conditions benefit adding indexes.
Comments
Post a Comment