简体   繁体   English

MySQL查询/服务器优化

[英]Mysql query/server optimization

Recently I changed MySQL version to 5.6.23 from 5.5.8 and got stuck in some query performance issue. 最近,我将MySQL版本从5.5.8更改为5.6.23,并陷入了一些查询性能问题。 In old MySQL; 在旧的MySQL中; query is executed in some reasonable time, but in new one freebsd server+mysql server becomes unresponsive. 查询是在合理的时间内执行的,但是在新的一台freebsd服务器中,mysql服务器变得无响应。 Linux command Linux命令

[linux~] top [linux〜]顶部

show mysql >100% wcpu when query is executed. 执行查询时显示mysql> 100%wcpu。 If I execute each subquery seperately, results are fetched in seconds. 如果我分别执行每个子查询,结果将在几秒钟内获取。 At this moment mysql and apache needs to be restarted to continue with work. 此时,需要重新启动mysql和apache才能继续工作。

So my problem is optimization of next query: 所以我的问题是下一个查询的优化:

SELECT concat_ws('##', pos.name, 
( SELECT COUNT(*) FROM nalogi n 
WHERE n.other_createtime >= '2015-03-01 00:00:00' 
AND n.other_createtime <= '2015-03-31 23:59:59' 
AND n.nalog_invalid = 'N' AND n.other_pos = pos.id ), 
( SELECT COUNT(*) FROM nalogi n, posiljke_nalogi pn, posiljke p 
WHERE p.other_createtime >= '2015-03-01 00:00:00' 
AND p.other_createtime <= '2015-03-31 23:59:59' 
AND pn.nalogid = n.id 
AND pn.masterId = p.id 
AND p.path_from_type = 'center' 
AND n.nalog_invalid = 'N' 
AND (p.path_to_type = 'pos' OR p.path_to_type = 'customer') 
AND n.other_pos = pos.id )) 'entry' FROM spl_pos pos ORDER BY pos.id

Currently I am using indexes and caching of tables in memory. 目前,我正在使用索引和内存中表的缓存。 Any speed optimization or other advices are welcome. 欢迎任何速度优化或其他建议。

I searched the net and found some good sources source1 , source2 and many others which I already use in existing query. 我搜索网,发现了一些很好的来源来源1源2和许多其他人,我已经在现有的查询中使用。 I also checked type of strings that I compare. 我还检查了比较的字符串类型。 Based on experiences I have some issues when selecting some rows with: 根据经验,选择以下行时会遇到一些问题:

where x.field=field (slow)
where x.field="field" (much faster)

So I also checked for that, and date type too. 所以我也检查了一下,日期类型也是如此。 Now I am almost without any more idea to try. 现在我几乎没有更多尝试的想法了。 I can provide additional data if needed. 如果需要,我可以提供其他数据。

Currently I am saving situation with executing queries seperately (which I would prefer to do in one query). 目前,我通过单独执行查询来节省情况(我希望在一个查询中这样做)。

Few of my suggestions: 我的建议很少:

  • Tables created by subqueries cannot use index and you are using 2-3 subqueries. 子查询创建的表不能使用索引,并且您正在使用2-3个子查询。 Bad Practice!! 坏习惯! Creating temporary tables might be efficient if you are using large tables. 如果使用大型表,则创建临时表可能会很有效。
  • Did you do EXPLAIN of your query and see temp table created, file sort etc? 你做EXPLAIN查询,看看创建临时表,文件排序等? Try to avoid all those things as far as possible. 尽量避免所有这些事情。
  • Regarding the WHERE clause, make sure the data types are same, if column is "123" and you are comparing col1 = 123, index wont be used. 关于WHERE子句,请确保数据类型相同,如果列为“ 123”,并且您正在比较col1 = 123,则不会使用索引。

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

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