简体   繁体   English

Ruby on Rails中的MySQL性能

[英]MySQL performance in Ruby on Rails

I'm currently experiencing some performance issues with MySQL and trying to come up with a solution. 我目前在MySQL中遇到一些性能问题,并试图提出一个解决方案。 I've already added some indexes to various tables and it seems to have shaven off several hundreds of ms from the query length, but I was wondering if the following could be optimized: 我已经在各种表中添加了一些索引,这似乎使查询长度减少了数百毫秒,但是我想知道是否可以优化以下内容:

The code responsible for this is pretty big to post below, but in general: 负责此操作的代码可以在下面发布,但总的来说:

  • A CV has many desired_occupation (= Occupation model) 一个简历有很多desire_occupation(=职业模型)
  • A CV has many past_occupations (= Occupation model) 简历有很多过去的职业(=职业模型)
  • A CV has many occupational_skills (= Skill model) 简历具有很多职业技能(=技能模型)
  • A CV has many educational_skills (= Skill model) 简历具有很多教育技能(=技能模型)
  • An Occupation has many skills 职业有很多技能
  • An Occupation belongs to a Concept 职业属于概念
  • A Skill belongs to a Concept 技能属于概念
  • A Concept has many and belongs to a Concept 一个概念有很多并属于一个概念

I know it's a bit tricky to do this without the models, but the post has a limit on the amount of characters. 我知道在没有模型的情况下执行此操作有些棘手,但是帖子中的字符数有所限制。 Most of the queries in the log look like this: 日志中的大多数查询如下所示:

  Language Load (0.0ms)
  SELECT `languages`.* FROM `languages` WHERE `languages`.`code` = 'en' LIMIT 1   Skill Load (1.0ms)  SELECT `skills`.* FROM `skills` INNER JOIN `occupation_skills` ON `skills`.id = `occupation_skills`.skill_id WHERE ((`occupation_skills`.occupation_id = 156))

  Concept Load (1.0ms)
  SELECT `concepts`.* FROM `concepts` WHERE `concepts`.`id` = 10 LIMIT 1 ConceptLabel Load (1.0ms)  SELECT `concept_labels`.* FROM `concept_labels` INNER JOIN `labels` ON `labels`.`id` = `concept_labels`.`label_id` WHERE `concept_labels`.`concept_id` = 10 AND `labels`.`language_id` = 1 LIMIT 1

  Label Load (1.0ms)
  SELECT `labels`.* FROM `labels` WHERE `labels`.`id` = 5432 LIMIT 1

Some of these come directly from the cache like so: 其中一些直接来自缓存,如下所示:

  CACHE (0.0ms)
  SELECT `concept_labels`.* FROM `concept_labels` INNER JOIN `labels` ON `labels`.`id` = `concept_labels`.`label_id` WHERE `concept_labels`.`concept_id` = 10 AND `labels`.`language_id` = 1 LIMIT 1

  CACHE (0.0ms)
  SELECT `labels`.* FROM `labels` WHERE `labels`.`id` = 5432 LIMIT 1

  CACHE (0.0ms)
  SELECT `concept_labels`.* FROM `concept_labels` INNER JOIN `labels` ON `labels`.`id` = `concept_labels`.`label_id` WHERE `concept_labels`.`concept_id` = 10 AND `labels`.`language_id` = 1 LIMIT 1   CACHE (0.0ms)  SELECT `labels`.* FROM `labels` WHERE `labels`.`id` = 5432 LIMIT 1

  CACHE (0.0ms)
  SELECT `concept_labels`.* FROM `concept_labels` INNER JOIN `labels` ON `labels`.`id` = `concept_labels`.`label_id` WHERE `concept_labels`.`concept_id` = 10 AND `labels`.`language_id` = 1 LIMIT 1

  CACHE (0.0ms)
  SELECT `labels`.* FROM `labels` WHERE `labels`.`id` = 5432 LIMIT 1

The heaviest query amongst them is 其中最重的查询是

Label Load (56.0ms)
SELECT `labels`.* FROM `labels` WHERE (`labels`.`id` IN (9909,9888,9855,9822,9900,9867,9834,9912,9891,9879,9846,9813,9870,9858,9825,9903,9882,9837,9804,9894,9861,9849,9816,9873,9840,9828,9796,9906,9885,9852,9807,9897,9864,9831,9819,9876,9843,9810))

Yet the output still takes too long to load to my liking: 但是输出仍然需要很长时间才能加载到我喜欢的位置:

Rendered static/categorize.html.haml within layouts/application (515.1ms)
Completed 200 OK in 1651ms (Views: 424.0ms | ActiveRecord: 188.0ms)

Is there something else missing because, last time I checked 188 + 424ms != 1651ms... When running performance tests it takes up to 8 seconds according JMeter to receive a proper response... 是否还有其他遗漏,因为上一次我检查了188 + 424ms!= 1651ms ...运行性能测试时,根据JMeter可能需要8秒钟才能收到正确的响应...

Consider eager loading your associations (if you aren't already) to try and optimise your queries further. 考虑急于加载关联 (如果尚未加载 ),以尝试进一步优化查询。 If not, you might want to consider utilising an intermediary store such as Redis and perform your queries against that, and possibly utilise Resque to 'sync up' whenever required. 如果不是这样,您可能要考虑利用Redis之类的中间存储并针对它进行查询,并可能在需要时利用Resque进行“同步”。

Remember Redis is a NoSQL datastore and runs completely in RAM, so it's quick to say the least. 请记住,Redis是一个NoSQL数据存储,并且完全在RAM中运行,因此至少可以说是很快。

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

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