mysql really simple queries took long time

887 views Asked by At

I give you samples from slow-query-log.

mysql version:5.6.14, 8gb server 6gb free memory, innodb tables

for example: id is primary key when I type this query in phpmyadmin is very fast but its still in my slow query log

# 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: explain

'guid' is index really simple queries I have no ideas why they taking so long time sometimes

2

There are 2 answers

4
douwe On

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.

2
Kumar V On

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
  4. Index the Search Fields
  5. Index and Use Same Column Types for Joins
  6. Do Not ORDER BY RAND()
  7. Avoid SELECT *
  8. Almost Always Have an id Field
  9. Use ENUM over VARCHAR
  10. Get Suggestions with PROCEDURE ANALYSE()
  11. Use NOT NULL If You Can
  12. Prepared Statements
  13. Unbuffered Queries
  14. Store IP Addresses as UNSIGNED INT
  15. Fixed-length (Static) Tables are Faster
  16. Vertical Partitioning
  17. Split the Big DELETE or INSERT Queries
  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