简体   繁体   English

mysql真的很简单的查询花了很长时间

[英]mysql really simple queries took long time

I give you samples from slow-query-log. 我给你的样本来自慢查询日志。

mysql version:5.6.14, 8gb server 6gb free memory, innodb tables mysql版本:5.6.14,8gb服务器6gb可用内存,innodb表

for example: id is primary key when I type this query in phpmyadmin is very fast but its still in my slow query log 例如:当我在phpmyadmin中输入此查询时,id是主键,但是它仍然在我的慢查询日志中

# Query_time: 3.226675  Lock_time: 0.000046 Rows_sent: 0  Rows_examined: 1
SET timestamp=1385980918;
update `rsslists` set `lastTimeRead` = '1385980913', `total` = '66502', `quality` =      '0.028284863613124' where `id` = '146';

or this query: 或此查询:

 # Query_time: 2.284815  Lock_time: 0.000047 Rows_sent: 0  Rows_examined: 0
 SET timestamp=1385980935;
 select * from `articles` where `guid` = '1.2180130' limit 1;

explain: 说明: 说明

'guid' is index really simple queries I have no ideas why they taking so long time sometimes 'guid'是索引非常简单的查询,我不知道为什么有时会花费这么长时间

Follow the below key points to optimize the query. 请遵循以下要点来优化查询。

  1. Optimize Your Queries For the Query Cache 优化查询缓存的查询
  2. EXPLAIN Your SELECT Queries 解释您的选择查询
  3. LIMIT 1 When Getting a Unique Row LIMIT 1当获得唯一行时
  4. Index the Search Fields 索引搜索字段
  5. Index and Use Same Column Types for Joins 索引并为联接使用相同的列类型
  6. Do Not ORDER BY RAND() 不要按RAND()订购
  7. Avoid SELECT * 避免SELECT *
  8. Almost Always Have an id Field 几乎总是有一个id字段
  9. Use ENUM over VARCHAR 在VARCHAR上使用ENUM
  10. Get Suggestions with PROCEDURE ANALYSE() 通过PROCEDURE ANALYSE()获得建议
  11. Use NOT NULL If You Can 如果可以,请使用NOT NULL
  12. Prepared Statements 准备的陈述
  13. Unbuffered Queries 无缓冲查询
  14. Store IP Addresses as UNSIGNED INT 将IP地址存储为UNSIGNED INT
  15. Fixed-length (Static) Tables are Faster 固定长度(静态)表更快
  16. Vertical Partitioning 垂直分区
  17. Split the Big DELETE or INSERT Queries 拆分大DELETE或INSERT查询
  18. Smaller Columns Are Faster 较小的色谱柱更快
  19. Choose the Right Storage Engine 选择合适的存储引擎
  20. Use an Object Relational Mapper 使用对象关系映射器
  21. Be Careful with Persistent Connections 小心持久连接

For more details , Refer this 有关更多详细信息,请参阅

If these tables are huge and have multiple indexes, the insert can be quite slow. 如果这些表很大并且有多个索引,则插入可能会很慢。 You should log all your queries and see how other inserts perform. 您应该记录所有查询,并查看其他插入的执行情况。

As for the select, there should be an index on guid, otherwise it will be slow if it is a large table. 至于选择,应该在guid上有一个索引,否则,如果它是一个大表,它将很慢。

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

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