简体   繁体   中英

MySQL query optimization with LIMIT, ORDER BY, bool clause and IN clause

I need some advice. To be more general I explain on following example: I have MySQL InnoDB table 'posts' with fields:

`id` - INT UNSIGNED AUTOINCREMENT
`title` - VARCHAR(100)
`abstract` - TEXT #can be converted to VARCHAR(2048)
`content` - TEXT #text + thml tags
`category` - SMALLINT
`created_on` - DATETIME
`modified_on` - DATETIME
`enabled` - TINYINT(1)
`deleted` - TINYINT(1)

With inexes on

`id` - PRIMARY
`category` - INDEX
`modified_on` - INDEX
(`enabled`,`deleted`) - INDEX

table contain 10-15 million of records. I'm doing query

SELECT id, title, abstract FROM posts 
WHERE enabled = 1 AND deleted = 0 
    AND category IN ( {id's} )
ORDER BY modified_on DESC 
LIMIT {offset}, {limit}

With 50-100 concurrent requests to MySQL server it goes down. Each query executes in 0.1-0.5s, it depends on limit clause.

Do I need separate index for 'category' as sometimes I'm browsing for all posts from all categories or I can add 'category' to ( enabled , deleted ) index and pass id's of all categories (~200) every time I need posts from all of them?

What solutions can lead to improve speed of such query?

Thanks in advance.

I think you should add category in index containing 'enabled' and 'deleted'.

Hoping it will reduce your query time

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