简体   繁体   中英

MySQL database queries - speed

  • a database call wich gets unlimited entries (can be 10 or 10.000), like this:

    SELECT * FROM boo WHERE id = %d AND (approved = '1' OR (user = %d AND approved = '0'))

  • 2 database calls, one which gets exactly 10 entries, and another one which gets a single entry:

    SELECT * FROM boo WHERE id = %d AND approved = '1' LIMIT 10

    SELECT * FROM boo WHERE user = %d AND approved = '0'

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?

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!

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'

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM