Alex
Alex

Reputation: 68396

MySQL database queries - speed

note that I can't make my own db query. I can only use a set of functions that do the queries above...

which option is more efficient / faster?

Upvotes: 0

Views: 120

Answers (3)

Nands
Nands

Reputation: 1531

Test yourself. Use EXPLAIN you can get an overview of what's happending

Upvotes: 1

Zaki Saadeh
Zaki Saadeh

Reputation: 652

This is really tough to answer, but here's an outline to consider to get better performance:

  1. You have not mentioned which columns you are indexing (if you are). Indexing the columns that you want to select and the ones that you use conditions on can help you get a better performance. Keep in mind that indexing will incur heavier write costs, among other things. If your common case for these columns is lookup, I would say go for indexing.

  2. Make sure that you select the columns that you actually need, instead of selecting all. That is another factor that will get you a better performance.

  3. Keep in mind: Each query has its own overhead. Adding more queries will incur more cost.

  4. Keep in mind: You need to actually thing about the average performance. For example, I see that one of your queries is dependent on a certain user. Ask yourself, who are these users, are they just random website users that will have a couple of records associated with them? or are these users a tight community that keep posting and have a lot of comments associated with them? These questions will help you determine and approximate the number of returned results (average) and that should be used to make a good decision about which way you should go with.

Hope that helps!

Upvotes: 2

ysth
ysth

Reputation: 98378

Try it and see?

Assuming these are in a huge table with appropriate indexes, this may be fastest:

SELECT * FROM boo WHERE id = %d AND approved = '1' LIMIT 10
UNION ALL
SELECT * FROM boo WHERE user = %d AND approved = '0'

Upvotes: 1

Related Questions