简体   繁体   English

基本插入/更新性能问题

[英]Basic Insert / Update Performance Issue

Running MySQL Database and all tables are InnoDB 运行MySQL数据库,所有表都是InnoDB

Every evening we have a list of the top offending slow queries emailed to us via a batch script. 每天晚上,我们都会列出通过批处理脚本发送给我们的最严重的慢速查询列表。

In this list are two queries an insert and an update. 此列表中有两个查询,即插入和更新。

The insert script logs page views for a user. 插入脚本记录用户的页面视图。 To make this more efficient we will only write an insert once for a user in a session. 为了提高效率,我们将只为会话中的用户编写一次插入。 So we store the pages viewed in a session and do an in_array on page load. 因此,我们存储在会话中查看的页面,并在页面加载时执行in_array。 If the page is not in the array than we write an insert. 如果页面不在数组中,那么我们将写一个插入。

This table has 700,000+ records with the only index being the primary key (id). 该表有700,000多个记录,唯一的索引是主键(id)。

The insert into this table looks like this: 该表中的插入内容如下所示:

insert into page_views (user_id,page_name,created)
values ('4','Test Page','2013-08-13 10:44:21')

This query shows up on our slow query log as taking a while. 此查询需要一段时间才能显示在我们的慢速查询日志中。 My question is how can this be made more efficient. 我的问题是,如何才能提高效率。 I feel as if this is part of the reason our database is running slower. 我觉得这似乎是我们数据库运行缓慢的部分原因。


We also have an update query that runs on an InnoDB table with about 14,000+ records. 我们还有一个更新查询,该查询在具有大约14,000多个记录的InnoDB表上运行。 The update happens when a user views a specific record for the first time and we set a field titled viewed='YES'. 当用户首次查看特定记录并且我们设置标题为“ viewed ='YES'”的字段时,将进行更新。

update test_table set viewed='YES' where id=55

This update is showing up on the slow query log as well. 此更新也显示在慢查询日志中。 This table "test_table" has about (5) different indexes on it. 该表“ test_table”具有大约(5)个不同的索引。


#

Output from slow query log 慢查询日志的输出

Count : 165 (0.35%) Time : 2252.210041 s total, 13.649758 s avg, 2.026723 s to 313.311842 s max (8.96%) 95% of Time : 1181.480258 s total, 7.573591 s avg, 2.026723 s to 49.99961 s max Lock Time (s) : 12.722 ms total, 77 avg, 38 to 233 max (0.04%) 95% of Lock : 11.681 ms total, 75 avg, 38 to 84 max Rows sent : 0 avg, 0 to 0 max (0.00%) Rows examined : 0 avg, 0 to 0 max (0.00%) Database : Users : XXXXX@localhost : 100.00% (165) of query, 99.76% (46921) of all users 计数:165(0.35%)时间:总计2252.210041 s,平均13.649758 s,最大2.026723 s至313.311842 s(8.96%)95%的时间:总计1181.480258 s,平均7.573591 s平均,2.026723 s至49.99961 s最大锁定时间(s ):总计12.722 ms,平均77,最大38至233(0.04%)锁定的95%:总计11.681 ms,75平均,38至84最大发送的行:0平均,0至0最大(0.00%)已检查的行:平均0个,最大0至0(0.00%)数据库:用户:XXXXX @ localhost:查询的100.00%(165),所有用户的99.76%(46921)

Query abstract: SET timestamp=N; 查询摘要:SET timestamp = N; INSERT INTO page_views (user_id, page_name, created) VALUES ('S', 'S', 'S')1; INSERT INTO page_views(user_id,page_name,创建)VALUES('S','S','S')1;

The table structure for page_views page_views的表结构

id (int) primary key page_name (varchar(255)) created datetime id(int)主键page_name(varchar(255))创建的日期时间

What are my options for speeding up simple insert and updates on very large tables? 在大型表上加快简单插入和更新速度的方法有哪些?

Based on the huge number of rows in your table and rows should be added/modified regularly in those tables. 根据表中大量的行,应定期在这些表中添加/修改行。 I'd suggest to execute following query first and see the execution time for the queries you have mentioned: 我建议先执行以下查询,并查看您提到的查询的执行时间:

analyze table page_views; 分析表page_views;

analyze table test_table; 分析表test_table;

Above commands will not take much time, should be executed with 5/10 secs. 上面的命令不会花费很多时间,应该以5/10秒的时间执行。

If you can afford little longer maintenance time(depends on the size of table): 如果您负担不起更长的维护时间(取决于表的大小):

optimize table page_views; 优化表page_views;

optimize table test_table; 优化表test_table;

If you see performance gain, I also would like to inform that you can configure cron job which executes analyze daily and optimize weekly/monthly. 如果您看到性能提高,我也想通知您,您可以配置cron作业,该作业每天执行分析并每周/每月进行优化。

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

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