简体   繁体   English

mySQL - 增加页面浏览量的更快方法?

[英]mySQL - A faster way to increment page views?

Currently im using UPDATE pages SET hits = hits+1 WHERE id=:id to increment page hits with PDO, but while profiling the script its taking on average between 30ms & 65ms to update. 目前我正在使用UPDATE pages SET hits = hits+1 WHERE id=:id来增加PDO的页面命中率,但是在分析脚本时,它平均需要在30ms和65ms之间进行更新。

Is there a faster way to increment, hits is also a INDEX within the table: Here is an example dump: 有没有更快的增量方式, hits也是表中的一个INDEX:这是一个示例转储:

[queries] => Array
(
 [0] => Array
   (
    [sql] => SELECT * FROM `settings`
    [time] => 0.22602081298828
   )

 [1] => Array
    (
    [sql] => SELECT * FROM `menu_links` WHERE `active`="1" ORDER BY position ASC
    [time] => 0.2291202545166
    )

 [2] => Array
    (
    [sql] => SELECT * FROM `pages` WHERE `url`=:field AND active='1'
    [time] => 0.27203559875488
    )

 [3] => Array
   (
    [sql] => SELECT * FROM `pages` WHERE `menu_link`=:field AND active='1'
    [time] => 0.24008750915527
   )

 [4] => Array
  (
   [sql] => UPDATE pages SET hits = hits+1 WHERE id=:id
   [time] => 31.989107131958
  )
)

UPDATE UPDATE

It seems that using a LIMIT 1 clause has sped it up by 10-15ms 似乎使用LIMIT 1条款加速了10-15ms

[4] => Array
  (
   [sql] => UPDATE `pages` SET hits = hits+1 WHERE id=1 LIMIT 1
   [time] => Between 15 & 25ms
  )

with LOW_PRIORITY LOW_PRIORITY

[4] => Array
  (
   [sql] => UPDATE LOW_PRIORITY `pages` SET hits = hits+1 WHERE id=1 LIMIT 1
   [time] => Between 17 & 30ms
  )

An index on a column will actually make the column slower to update , because the index needs to be modified too. 列上的索引实际上会使列更新更慢 ,因为索引也需要修改。

Using pure MySQL, there is not much you can do, except tweak the server settings: hold more operations in temporary memory, write less often to disk. 使用纯MySQL,除了调整服务器设置外,你无能为力:在临时内存中保存更多操作,不经常写入磁盘。 But still, the server will need to log the operation to one logfile. 但是,服务器仍需要将操作记录到一个日志文件中。 ACID does not come cheaply. ACID并不便宜。

Most servers therefore use another kind of temporary storage, like a memcached or something. 因此,大多数服务器使用另一种临时存储,如memcached或其他东西。 This way, the values are stored in RAM, and only when the value reaches a certain threshold, it is written to the database. 这样,值就存储在RAM中,并且只有当值达到某个阈值时,才会将其写入数据库。

It is the best way of doing that. 这是最好的方式。

And obviously update takes more time than select because it implies storage modifications, while selects usually use various caches and buffers. 显然更新需要的时间比select更多,因为它意味着存储修改,而选择通常使用各种缓存和缓冲区。

If the speed is very important to you - you can set up any queue software and add incrementing task to the queue and refresh the data in a bulk manner by some backgound worker. 如果速度对您非常重要 - 您可以设置任何队列软件并将增量任务添加到队列中,并由某个背景工作者以批量方式刷新数据。 This solution would be much more efficient but only applicable if you don't need your changes to be available immediately. 此解决方案效率会更高,但只有在您不需要立即更改时才适用。

In general, updates are much slower than reads. 通常,更新比读取慢得多。 Given that id is indexed and the data set that you are working with is large, there's not much more that you can do. 鉴于id已编入索引并且您正在使用的数据集很大,因此您可以做的更多。 The following page describes ways that you can improve your performance or by adding a LIMIT clause. 以下页面介绍了可以提高性能或添加LIMIT子句的方法。

Another way to get fast updates is to delay updates and then do many updates in a row later. 获得快速更新的另一种方法是延迟更新,然后在以后连续执行许多更新。 Performing multiple updates together is much quicker than doing one at a time if you lock the table. 如果锁定表,一次执行多个更新比一次执行多个更新要快得多。

http://dev.mysql.com/doc/refman/5.0/en/update-speed.html http://dev.mysql.com/doc/refman/5.0/en/update-speed.html

If you are only after reducing page load times and your server is set up with php-fpm/FastCGI you can call fastcgi_finish_request() and after that you update the hit counter, see http://php-fpm.org/wiki/Features#fastcgi_finish_request.28.29 如果您只是在减少页面加载时间并且您的服务器设置为php-fpm / FastCGI后,您可以调用fastcgi_finish_request(),然后更新命中计数器,请参阅http://php-fpm.org/wiki/Features #fastcgi_finish_request.28.29

Basically this flushes the output to client and closes the connection, and then continues to execute the PHP script, so the client doesn't have to wait for the counter update operation. 基本上,这会将输出刷新到客户端并关闭连接,然后继续执行PHP脚本,因此客户端不必等待计数器更新操作。 This doesn't offload the server though, just the access times for the clients... 这不会卸载服务器,只是客户端的访问时间......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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