简体   繁体   中英

Mysql query taking too much time

I have problem related to mysql database. i am linux webserver admin and i am facing a problem with a mysql query. The database is very small. I tried to track in logs and found that a query is taking minimum 5 sec to respond . The first page of site is coming from the database. Client are using cms. when the server gets some number of hits database server starts to give response very slowly and wait time increases from 5 sec to several seconds.

I checked slow query logs

{
Query_time: 11.480138  Lock_time: 0.003837 Rows_sent: 921  Rows_examined: 3333

SET timestamp=1346656767;
SELECT `Tender`.`id`,
    `Tender`.`department_id`,
    `Tender`.`title_english`,
    `Tender`.`content_english`,
    `Tender`.`title_hindi`,
    `Tender`.`content_hindi`,
    `Tender`.`file_name`,
    `Tender`.`start_publish`,
    `Tender`.`end_publish`,
    `Tender`.`publish`,
    `Tender`.`status`,
    `Tender`.`createdBy`,
    `Tender`.`created`,
    `Tender`.`modifyBy`,
    `Tender`.`modified`
FROM `mcms_tenders` AS `Tender`
WHERE `Tender`.`department_id` IN ( 31, 33, 32, 30 );
}

Every line in the log is same only there is diff in Query time. Is there any way tweak the performance?

Update: Here is the EXPLAIN result:

+----+-------------+--------+------+---------------+------+---------+------+-‌-----+-------------+
| id | select_type | table  | type | possible_keys | key  | key_len | ref  | rows | Extra       |
+----+-------------+--------+------+---------------+------+---------+------+----‌​--+-------------+
| 1  | SIMPLE      | Tender | ALL  | NULL          | NULL | NULL    | NULL | 3542 | Using where |
+----+-------------+--------+------+---------------+------+---------+------+----‌​--+-------------+
1 row in set, 1 warning (0.00 sec) 

client is saying they are using Index so i run the command to check the indexing.

I got following output. Does It means they are using Indexing.

+--------------+------------+----------+--------------+-------------+-----------+------------+----------+--------+------+------------+---------+ | Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | +--------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+ | mcms_tenders | 0 | PRIMARY | 1 | id | A | 4264 | NULL | NULL | | BTREE | | +--------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+

The normal way to tweak the performance of a query like this is to create an index on department_id.

However, this assumes that Tenders is actually a table and not a view. You should confirm this, since the problem may be in a view.

Also, from what you describe the issue may be the connection from the server to the end users. I would try running the query locally on the server (or checking the execute time strictly on the server) to see if the query is really taking that long.

" when the server gets some number of hits "

Define 'some number'. It makes sense that reading the database is slower when it is more heavily used. Also, MySQL has a query cache that is fully invalidated when changes are made to the data. So every time someone inserts, deletes or modifies a record in this table, the next queries will be slower because the table date is still uncached.

But 11 seconds for a query like this is very slow, so either the load is way too high, the hardware insufficient or broken or your database lacks indexes (I always forget to mention that at first, because I assume adding indexes to be a second nature for anyone working with databases).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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