Pergunta

I have a system that's similar to the Q&A feature of stackoverflow. The main difference is hat each question has an expiration datetime:

CREATE TABLE questions (
     id INT NOT NULL AUTO_INCREMENT,
     title CHAR(100) NOT NULL,
     details TEXT
     PRIMARY KEY (id)
) 

CREATE TABLE answers (
     id INT NOT NULL AUTO_INCREMENT,
     question_id INT NOT NULL
     details TEXT
     expiration_datetime DATETIME
     score INT
     PRIMARY KEY (id)
) 

I want to display a question together with the top N non-expired answers ordered by score. I'm thinking something like this:

SELECT * FROM questions, answers 
WHERE questions.id=1 AND questions.id=answers.question_id AND answers.expiration_datetime > NOW() 
ORDER BY answers.score DESC LIMIT 10

A few questions:

1) Is the query above the most efficient way to do what I want? How is it different from explicitly using JOIN such as:

SELECT * FROM questions JOIN answers ON questions.id=answers.question_id
WHERE questions.id=1 AND answers.expiration_datetime > NOW() 
ORDER BY answers.score DESC LIMIT 10

2) How to make my query use index? I'm thinking adding this index to TABLE answers:

INDEX (question_id, expiration_datetime, score)

Would the above index work for my query? It doesn't seems right because expiration_datetime is ascending while I need score to be descending. What can I do here?

Foi útil?

Solução

Joining tables with INNER JOIN or in the FROM clause gives the same execution plan. However, the former is much more easy to read (and that's what standard suggest since 1992).

Your RDBMS will use an index whenever it's possible. It's usually a good idea to print the execution plan (EXPLAIN SELECT * FROM -- ...). However, note that creating multiple columns index can be tricky. In your case, you can use the index on question_id but not on expiration_datetime or score because they aren't the first columns of the index. You would have to make three different index.

P.S. SELECT * isn't recommanded. Always type the columns you need (easy to read and re-read) and only mention the columns you will use. No point to select, for instance, initial_date if you never use this column in your script!

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top