2

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:

  • "SELECT * WHERE pageid = xxxx"
  • "SELECT * WHERE visitorid = xxx AND pageid = xxxx"
  • "SELECT visitorid, MIN(time) AS mintime WHERE pageid = xxxx GROUP BY visitorid"

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

  • "SELECT * WHERE maintestid = xxxx"

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?

2 Answers 2

4
  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
Sign up to request clarification or add additional context in comments.

Comments

0

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

1 Comment

"it will be better if you have only one id as primary since it is more appropriate" -- what do you mean by "more appropriate"?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.