How many queries are too many?

前端 未结 8 1171
暗喜
暗喜 2021-01-01 12:47

I have to run one time 10 mysql queries for one person in one page. Is it very bad? I have quite good hosting, but still, can it break or something? Thank you very much.

相关标签:
8条回答
  • 2021-01-01 13:19

    Just to add a bit of a different perspective to the other good answers:

    First, to concur, the type and complexity of queries you are making will matter more 99% of the time than the number of queries.

    However, in the rare situation where there is high latency on the network path to your database server (i.e. the db server is remote or such, not saying this is a logical or sane setup, but I have seen it done) then you want to minimize the number of queries done, because every single time you talk to the database server the network transmission time will take an order of magnitude or two longer than it takes to compute the query. This situation can really kill your page loading times, and so you'd really want to minimize the number of queries (actually, you just want to change your server setup...).

    0 讨论(0)
  • 2021-01-01 13:22

    How many queries are too many?

    I will rephrase your question:

    Is my app fast enough?

    Come up with a business definition of "fast enough" for your application (based on business/user requirements), come up with a way to model all your usage scenarios and expected load, create simulations of that load and profile (trace/time) it.

    This approach amounts to an educated guess. Anything short of it is pure speculation, and worthless.

    If your application is already in production, and is working well in most cases, you can get feedback from users to determine pain points. From there, you can model those pain points and corresponding load, and profile.

    Document your results. Once you make improvements to your application, you have a tool to determine if the optimizations you made achieved your goals.

    0 讨论(0)
  • 2021-01-01 13:37

    Drupal sites typically make anywhere from 150 to 400+ queries per request. The total time spent querying the database is still under 1s - it's not the number that kills the server, but the quality/complexity of the queries (and possibly the size of the dataset they search through).

    I can't tell what queries you're talking about but on most sites 10 is not much at all.

    If you're concerned with performance, you can always see how long your queries take to execute in a database management program, such as MySQL Workbench.

    0 讨论(0)
  • 2021-01-01 13:37

    It depends on how much CPU cycles will the sum of the queries use.

    1 query can consume way more CPU cycles than 100. It all depends on their contents.

    You could begin by optimizing them following this guide: http://beginner-sql-tutorial.com/sql-query-tuning.htm

    0 讨论(0)
  • 2021-01-01 13:37

    I think its not a problem. 10 Queries are not so much for a site. Less is better no question but when you have 3000 - 5000 then you should think about your structure.

    And when you go in one query through a table with millions of rows without an index then are 10 to much.

    I have seen a Typo3 site with a lot of extensions that make 7500 requests with the cache. This happens when you install and install and don't look at what happens.

    But you can look that you make logical JOIN's over the tables that you have less queries.

    0 讨论(0)
  • 2021-01-01 13:44

    Well there are big queries and small trivial queries. Which ones are yours? Generally, you should try to fetch the data in as few queries as possible. The heavier the load is on the database server the harder it will be to serve the clients as the traffic increases.

    0 讨论(0)
提交回复
热议问题