Tim
Tim

Reputation: 7780

Are multiple indexes involving the same column useful or not?

I have a table called clicks which records mouse clicks around a website. The schema looks like this:

enter image description here

There are 3 main queries run on this data:

In the future I might use maintestid in queries too. So this would look like:

So I've added 2 indexes to the table for both these queries along with the primary key like so:

enter image description here

I have 3 questions:

  1. Is this primary key useful at all being like this? Or should I make it the ID column only?

  2. Is the pageid index useful at all? Or will it be just as fast if all queries for pageid are be handled by the pageid_visid index?

  3. When I want to search via maintestid in the future, should I just add an index on the maintestid column, or something different?

Upvotes: 2

Views: 1230

Answers (2)

Bohemian
Bohemian

Reputation: 425358

  1. The primary key is NOT useful like you have it - the other columns of that index will never be used. Just make it on the id column only
  2. The pageid only index is slightly useful in that its entries will fit on less index pages, but I would drop it and let mysql use the other pageid-based index (do a benchmark test to confirm)
  3. Yes - define your indexes to match your important queries

Upvotes: 4

prukuhkoo
prukuhkoo

Reputation: 105

Why would you want to do this?

Anyway, if you want to have them pageid, pageid_visid and maintestid as indexes then it's all fine.. but it will be better if you have only one id as primary since it is more appropriate. anyway, you could query them all using the other id's

Upvotes: 0

Related Questions