MySQL 調教

查看Query Performance

SET PROFILING=1;
SHOW PROFILES;
SHOW PROFILE CPU FOR QUERY 2;

參考

觀看即時process

SHOW FULL PROCESSLIST;

Buffer Size

如果MySQL使用的CPU過高,加大buffer可能有幫助,以下是innodb引擎的調教方式

查看當前buffer size

SHOW VARIABLES LIKE 'innodb_buffer_pool_size';

查看建議buffer size,參考神回答

SELECT CEILING(Total_InnoDB_Bytes*1.6/POWER(1024,3)) RIBPS FROM
(SELECT SUM(data_length+index_length) Total_InnoDB_Bytes
FROM information_schema.tables WHERE engine='InnoDB') A;

如果出現數字1,表示建議buffer size 1GB,可在/etc/mysql/conf.d/my.cnf設定

[mysqld]
innodb_buffer_pool_size = 1G

[mysql][mysqld]的差別:前者是mysql command line client套用的設定,後者是mysql server。詳情見此

重啟mysql server後,再次查看buffer size確認無誤。

Query Cache

另外可以再加兩項,query_cache_size是所有query的cache大小,query_cache_limit是單個query所佔的極限。

query_cache_size=518M
query_cache_limit=10M

Example

select SQL_NO_CACHE P.* from product P left join 
  (
    select distinct product_id, sort  from product_type_relation where type_id in (16)
  ) AS A 
ON P.id=A.product_id
ORDER BY A.sort, P.id desc
LIMIT 0,28;

=> 耗時0.1
SELECT SQL_NO_CACHE distinct P.id, P.*
FROM product_type_relation A
LEFT JOIN product P ON A.product_id=P.id
WHERE A.type_id in (16) 
ORDER BY A.sort asc, P.id desc
LIMIT 0, 28;

=> 耗時0.04
SELECT SQL_NO_CACHE P.*
FROM product P 
LEFT JOIN product_type_relation A ON A.product_id=P.id
WHERE P.id in 
  (
     select distinct product_id from product_type_relation where type_id in (16)
  ) 
ORDER BY A.sort
LIMIT 0,28;

=> 耗時0.022

參考

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容